首页 > 代码库 > 完成对数据库的CRUD操作

完成对数据库的CRUD操作

PS:查询相对复杂,要处理结果集,增删改则不用。

 1 package it.cast.jdbc; 2  3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7  8 public class CRUD { 9 10     /**11      * @param args12      * @throws ClassNotFoundException 13      * @throws SQLException 14      */15     public static void main(String[] args) throws SQLException, ClassNotFoundException {16         delete();17 18     }19     20     //删除21     static void delete() throws SQLException, ClassNotFoundException {22 23         Connection conn = null;24         Statement st = null;25         ResultSet rs = null;26         // 2.建立连接27         conn = jdbcUtils.getConnection();28 29         // 3.创建语句30         st = conn.createStatement();31         32         String sql = "delete from user where name=‘name1‘";33 34         // 4.执行语句35         int i = st.executeUpdate(sql);36 37         System.out.println("I="+i);38 39         // 6.释放资源40         jdbcUtils.free(rs, st, conn);41     }42     43     //修改44     static void update() throws SQLException, ClassNotFoundException {45 46         Connection conn = null;47         Statement st = null;48         ResultSet rs = null;49         // 2.建立连接50         conn = jdbcUtils.getConnection();51 52         // 3.创建语句53         st = conn.createStatement();54         55         String sql = "update user set money=money+10";56 57         // 4.执行语句58         int i = st.executeUpdate(sql);59 60         System.out.println("I="+i);61 62         // 6.释放资源63         jdbcUtils.free(rs, st, conn);64     }65 66 67     //新增68     static void create() throws SQLException, ClassNotFoundException {69 70         Connection conn = null;71         Statement st = null;72         ResultSet rs = null;73         // 2.建立连接74         conn = jdbcUtils.getConnection();75 76         // 3.创建语句77         st = conn.createStatement();78         79         String sql = "insert into user(name,birthday,money) values(‘name1‘,‘1997-01-01‘,4000)";80 81         // 4.执行语句82         int i = st.executeUpdate(sql);83 84         System.out.println("I="+i);85 86         // 6.释放资源87         jdbcUtils.free(rs, st, conn);88     }89 90 }
CRUD