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 args 12 * @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
时间: 2024-10-12 08:12:14