【JDBC】CRUD操作
程序员文章站
2023-11-11 13:50:16
JDBC的CRUD操作 向数据库中保存记录 修改数据库中的记录 删除数据库中的记录 查询数据库中的记录 保存代码的实现 修改代码的实现 删除代码的实现 查询代码的实现 ......
jdbc的crud操作
- 向数据库中保存记录
- 修改数据库中的记录
- 删除数据库中的记录
- 查询数据库中的记录
保存代码的实现
package demo1; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.sql.statement; import org.junit.test; public class jdbcdemo3 { @test /** * 保存代码的实现 */ public void demo3() { connection conn = null; statement stmt = null; try { //注册驱动 class.forname("com.mysql.cj.jdbc.driver"); //获得连接 conn = drivermanager.getconnection("jdbc:mysql:///jdbctest?servertimezone=hongkong", "root", "1234"); //获得执行sql语句的对象 stmt = conn.createstatement(); //编写sql string sql = "insert user values(null,'ddd','444','赵六')"; //执行sql int i = stmt.executeupdate(sql); if(i > 0) { system.out.println("保存成功!"); } }catch(exception e) { e.printstacktrace(); }finally{ //释放资源 if(stmt != null) { try { stmt.close(); } catch (sqlexception e) { e.printstacktrace(); } stmt = null; } if(conn != null) { try { conn.close(); } catch (sqlexception e) { e.printstacktrace(); } conn = null; } } } }
修改代码的实现
package demo1; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.sql.statement; import org.junit.test; public class jdbcdemo3 { @test /** * 修改代码的实现 */ public void demo2() { connection conn = null; statement stmt = null; try { //注册驱动 class.forname("com.mysql.cj.jdbc.driver"); //获得连接 conn = drivermanager.getconnection("jdbc:mysql:///jdbctest?servertimezone=hongkong", "root", "1234"); //获得执行sql语句的对象 stmt = conn.createstatement(); //编写sql string sql = "update user set username='qqq',password='1234',name='或无言' where uid=4"; //执行sql int i = stmt.executeupdate(sql); if(i > 0) { system.out.println("修改成功!"); } }catch(exception e) { e.printstacktrace(); }finally{ //释放资源 if(stmt != null) { try { stmt.close(); } catch (sqlexception e) { e.printstacktrace(); } stmt = null; } if(conn != null) { try { conn.close(); } catch (sqlexception e) { e.printstacktrace(); } conn = null; } } } }
删除代码的实现
package demo1; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.sql.statement; import org.junit.test; public class jdbcdemo3 { @test /** * 删除代码的实现 */ public void demo1() { connection conn = null; statement stmt = null; try { //注册驱动 class.forname("com.mysql.cj.jdbc.driver"); //获得连接 conn = drivermanager.getconnection("jdbc:mysql:///jdbctest?servertimezone=hongkong", "root", "1234"); //获得执行sql语句的对象 stmt = conn.createstatement(); //编写sql string sql = "delete from user where uid=4"; //执行sql int i = stmt.executeupdate(sql); if(i > 0) { system.out.println("删除成功!"); } }catch(exception e) { e.printstacktrace(); }finally{ //释放资源 if(stmt != null) { try { stmt.close(); } catch (sqlexception e) { e.printstacktrace(); } stmt = null; } if(conn != null) { try { conn.close(); } catch (sqlexception e) { e.printstacktrace(); } conn = null; } } } }
查询代码的实现
package demo1; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.sqlexception; import java.sql.statement; import org.junit.test; public class jdbcdemo1 { @test /** * 查询代码的实现 */ public void demo1() { connection conn = null; statement stmt = null; resultset rs = null; try { //1.加载驱动 class.forname("com.mysql.cj.jdbc.driver"); //2.获得连接 conn = drivermanager.getconnection("jdbc:mysql:///jdbctest?servertimezone=hongkong", "root", "1234"); //3创建执行sql语句的对象,并且执行sql //3.1创建执行sql语句的对象 string sql = "select * from user"; stmt = conn.createstatement(); //3.2执行sql rs = stmt.executequery(sql); while(rs.next()) { int uid = rs.getint("uid"); string username = rs.getstring("username"); string password = rs.getstring("password"); string name = rs.getstring("name"); system.out.println(uid + "-" + username + "-" + password + "-" + name); } } catch (exception e) { e.printstacktrace(); } finally { //释放资源 if(rs != null) { try { rs.close(); } catch (sqlexception e) { e.printstacktrace(); } rs = null; } if(stmt != null) { try { stmt.close(); } catch (sqlexception e) { e.printstacktrace(); } stmt = null; } if(conn != null) { try { conn.close(); } catch (sqlexception e) { e.printstacktrace(); } conn = null; } } } }