day17 10.jdbc的crud操作
Posted 绿茵好莱坞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day17 10.jdbc的crud操作相关的知识,希望对你有一定的参考价值。
每次都是注册驱动,获取连接,然后执行。每次都写很累,肯定能抽取出来一些东西。Java里面是这样的,相同的东西可以抽取做成一个方法。用的时候调这方法就OK了。这方法抽取到什么程度呢?
package cn.itcast.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.junit.Test; import cn.itcast.utils.JdbcUtils; //jdbc的crud操作 public class JdbcDemo6 { @Test public void findByIdTest(){ //1.定义sql String sql = "select * from user where id= 1"; Connection con = null; Statement st = null; ResultSet rs = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取连接 try { con = DriverManager.getConnection("jdbc:mysql:///day17", "root", ""); //3.获取操作sql语句对象Statement st = con.createStatement(); //4.执行sql rs = st.executeQuery(sql); //5.遍历结果集 while(rs.next()){ int id = rs.getInt("id"); //String id = rs.getString("id");//虽然用getString()行,但是用getInt()比较合适 String username = rs.getString("username"); String password = rs.getString("password"); String email = rs.getString("email"); System.out.println(id+" "+username+" "+password+" "+email); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //6.释放资源 try { if(rs !=null ){ rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(st!=null){ st.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(con!=null){ con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //添加操作 @Test public void addTest(){ //定义sql String sql = "insert into user values(null,\'张三\',\'123\',\'zs@163.com\')"; Connection con = null; Statement st = null; ResultSet rs = null; try { //1.注册驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取连接 try { con = DriverManager.getConnection("jdbc:mysql:///day17", "root", ""); //3.获取操作sql语句对象Statement st = con.createStatement(); //4.执行sql int row = st.executeUpdate(sql); System.out.println(row); if(row!=0){ System.out.println("添加成功"); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //6.释放资源 try { if(rs !=null ){ rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(st!=null){ st.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(con!=null){ con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //update操作 @Test public void updateTest(){ //将id=3的人的password修改为456 String password = "456"; String sql = "update user set password=\'"+password+"\' where id=3"; //1.得到Connection Connection con = null; Statement st = null; try { con = JdbcUtils.getConnectin(); //3.获取操作sql语句对象Statement st = con.createStatement(); //4.执行sql int row = st.executeUpdate(sql); System.out.println(row); if(row!=0){ System.out.println("添加成功"); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //关闭资源 try { if(st!=null){ st.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(con!=null){ con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
package cn.itcast.utils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JdbcUtils { public static Connection getConnectin() throws ClassNotFoundException, SQLException{ Class.forName("com.mysql.jdbc.Driver");//在开发中用哪个Statement人家有选择的权利,你不能给它抽取 //2.获取连接 Connection con = DriverManager.getConnection("jdbc:mysql:///day17", "root", ""); return con; } }
以上是关于day17 10.jdbc的crud操作的主要内容,如果未能解决你的问题,请参考以下文章
[Angularjs]asp.net mvc+angularjs+web api单页应用之CRUD操作
11SpringBoot-CRUD-thymeleaf公共页面元素抽取