您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java链接mySQL数据库进行增删改查_MySQL

2025/11/15 0:28:56发布27次查看
java链接mysql数据库代码
改和查对于增加是一样的
public class jdbctest { /** * resultset封装了jdbc结果集,进行查询的结果 */ @test public void testresultset() { connection connection = null; statement statement = null; resultset rs = null; try { connection = jdbctools.getconnection(); statement = (statement) connection.createstatement(); string sql = select * from jdbc_1; rs = statement.executequery(sql); while (rs.next()) { system.out.print(rs.getint(1) + /t); system.out.print(rs.getstring(2) + /t); system.out.print(rs.getstring(3)); system.out.println(); } } catch (exception e) { e.printstacktrace(); } finally { jdbctools.release(rs, statement, connection); } } /** * 通用更新方法 * @throws sqlexception */ public void updata(string sql) throws sqlexception { connection connection = null; statement statement = null; try { connection = getconnection(); // 获取statement 对象 statement = (statement) connection.createstatement(); // 执行sql语句 statement.executeupdate(sql); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } finally { try { // 关闭statement对象 if (statement != null) { statement.close(); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } finally { // 关闭数据库连接 if (connection != null) { connection.close(); } } } } /** * 1.通过jdbc向指定表中插入数据 * * @throws exception * */ @test public void teststatement() throws exception { // 获取数据库链接 connection connection = null; statement statement = null; try { connection = getconnection(); // 要执行的sql语句 string sql = insert into jdbc_1 (jname,addr) value ('李力','浑江市'); // 获取statement 对象 statement = (statement) connection.createstatement(); // 执行sql语句 statement.executeupdate(sql); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } finally { try { // 关闭statement对象 if (statement != null) { statement.close(); } } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } finally { // 关闭数据库连接 if (connection != null) { connection.close(); } } } } /** * drivermanager类是驱动的管理类 利用drivermanager连接数据库,进行数据库驱动注册 * * @throws exception */ @test public void testdrivermanager() throws exception { string driverclass = null; string jdbcurl = null; string user = null; string password = null; // 读取properties文件 inputstream in = getclass().getclassloader().getresourceasstream( jdbc.properties); properties properties = new properties(); properties.load(in); driverclass = properties.getproperty(driver); jdbcurl = properties.getproperty(url); user = properties.getproperty(user); password = properties.getproperty(password); class.forname(driverclass); connection connection = (connection) drivermanager.getconnection( jdbcurl, user, password); system.out.println(connection); } /** * 驱动测试,对链接驱动进行测试 * * @throws sqlexception * */ @test public void testdriver() throws sqlexception { // 创建drivers实现类 driver driver = new com.mysql.jdbc.driver(); string url = jdbc:mysql://localhost:3306/jdbc; properties info = new properties(); info.put(user, root); info.put(password, root); // 调用driver接口的connection connection connection = (connection) driver.connect(url, info); system.out.println(connection); } /** * 编写通用方法获取任意数据库链接,不用修改源程序 * * @throws classnotfoundexception * @throws illegalaccessexception * @throws instantiationexception * @throws sqlexception * @throws ioexception */ public connection getconnection() throws instantiationexception, illegalaccessexception, classnotfoundexception, sqlexception, ioexception { string driverclass = null; string jdbcurl = null; string user = null; string password = null; // 读取properties文件 inputstream in = getclass().getclassloader().getresourceasstream( jdbc.properties); properties properties = new properties(); properties.load(in); driverclass = properties.getproperty(driver); jdbcurl = properties.getproperty(url); user = properties.getproperty(user); password = properties.getproperty(password); driver driver = (driver) class.forname(driverclass).newinstance(); properties info = new properties(); info.put(user, user); info.put(password, password); connection connection = (connection) driver.connect(jdbcurl, info); return connection; } @test public void testconnection() throws instantiationexception, illegalaccessexception, classnotfoundexception, sqlexception, ioexception { system.out.println(getconnection()); }}
public class jdbctools { /** * 结果查询关闭 * @param rs * @param statement * @param conn */ public static void release(resultset rs,statement statement, connection conn) { if (rs != null) { try { rs.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } if (statement != null) { try { statement.close(); } catch (exception e2) { e2.printstacktrace(); } } if (conn != null) { try { conn.close(); } catch (exception e2) { e2.printstacktrace(); } } } /** * 数据库更新方法 * @param sql */ public void uodate(string sql) { connection connection = null; statement statement = null; try { connection = jdbctools.getconnection(); statement = (statement) connection.createstatement(); statement.executeupdate(sql); } catch (exception e) { e.printstacktrace(); } finally { jdbctools.release(statement, connection); } } /** * 关闭数据库连接的方法 * @param statement * @param conn */ public static void release(statement statement, connection conn) { if (statement != null) { try { statement.close(); } catch (exception e2) { e2.printstacktrace(); } } if (conn != null) { try { conn.close(); } catch (exception e2) { e2.printstacktrace(); } } } /** * 编写通用方法获取任意数据库链接,不用修改源程序 * * @return * @throws classnotfoundexception * @throws illegalaccessexception * @throws instantiationexception * @throws sqlexception * @throws ioexception */ public static connection getconnection() throws instantiationexception, illegalaccessexception, classnotfoundexception, sqlexception, ioexception { string driverclass = null; string jdbcurl = null; string user = null; string password = null; // 读取properties文件 inputstream in = jdbctools.class.getclassloader().getresourceasstream( jdbc.properties); properties properties = new properties(); properties.load(in); driverclass = properties.getproperty(driver); jdbcurl = properties.getproperty(url); user = properties.getproperty(user); password = properties.getproperty(password); driver driver = (driver) class.forname(driverclass).newinstance(); properties info = new properties(); info.put(user, user); info.put(password, password); connection connection = (connection) driver.connect(jdbcurl, info); return connection; }}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product