JDBC Statment 使用属性池连接数据库

//测试类 

插入 删除 更新 基本一致

 1 public class TestInsert {
 2     public static void main(String[] args) {
 3         Connection conn = null;
 4         Statement st =null;
 5         ResultSet rs =null;
 6         try{
 7             conn = JdbcUtils.getConnection();
 8             st = conn.createStatement();
 9             String sql ="insert into users(id,name,password,email,birthday) values(4,'zxy','123456','16033673@qq.com','2002-3-13');";
10             int i = st.executeUpdate(sql);
11             if(i>0){
12                 System.out.println("插入成功");
13             }
14         }catch (SQLException e){
15             e.printStackTrace();
16         }finally {
17             JdbcUtils.release(conn,st,rs);
18         }
19     }
20 }

查找

 1 public class TestSelect {
 2     public static void main(String[] args) throws SQLException {
 3         Connection conn = null;
 4         Statement st = null;
 5         ResultSet rs = null;
 6         try {
 7             conn = JdbcUtils.getConnection();
 8             st = conn.createStatement();
 9             //SQL
10             String sql = "select * from users where id =1";
11             rs = st.executeQuery(sql);
12             while(rs.next()){
13                 System.out.println(rs.getString("name"));
14             }
15 
16         } catch (SQLException e) {
17             e.printStackTrace();
18         }finally {
19             JdbcUtils.release(conn,st,rs);
20         }
21     }
22 }

注意! executeQuery 返回的时ResultSet的结果类 sql运行的结果

    executeUpdate返回的时一个int类型的数 意思时 受影响的行数

//工具类

 1 public class JdbcUtils {
 2     private static String driver = null;
 3     private static String url = null;
 4     private static String username = null;
 5     private static String password = null;
 6     static {
 7         try{
 8             InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
 9             Properties properties = new Properties();
10             properties.load(in);
11             driver = properties.getProperty("driver");
12             url = properties.getProperty("url");
13             username = properties.getProperty("username");
14             password = properties.getProperty("password");
15 
16             //1.加载驱动
17             Class.forName(driver);
18         }catch (Exception e){
19             e.printStackTrace();
20         }
21         
22     }
23     //获取连接
24     public static Connection getConnection() throws SQLException {
25          return DriverManager.getConnection(url,username,password);
26     }
27     //释放资源
28     public  static void release(Connection conn, Statement st, ResultSet rs){
29         if(rs!=null){
30             try{
31                 rs.close();
32             } catch (SQLException e) {
33                 e.printStackTrace();
34             }
35         }
36         if(st!=null){
37             try {
38                 st.close();
39             } catch (SQLException e) {
40                 e.printStackTrace();
41             }
42         }
43         if(conn!=null){
44             try {
45                 conn.close();
46             } catch (SQLException e) {
47                 e.printStackTrace();
48             }
49         }
50 
51     }
52 
53 }

//属性池

1 driver=com.mysql.cj.jdbc.Driver
2 url=jdbc:mysql://localhost:3306/jdbcstudy?serverTimezone=GMT%2B8
3 username=root
4 password=123456789

 

posted @ 2022-10-14 17:02  西东怪  阅读(14)  评论(0编辑  收藏  举报
返回顶端