欢迎来到刘认真的博客

It's not me that's wrong. It's the whole world

mysql 第三课 jdbc基础操作

jdbc连接可以大致分为5步:

1.注册驱动

2.获取连接

3.编写语句

4.执行语句

5.关闭连接

其中可以设置参数等等。

1.我们先建一个项目目录:

其中com.etc.dao为数据访问对象

com.etc.config 为配置文件的获取键值

com.etc.domain是实现包

com.etc.util是工具包

db.properties是配置文件

2.先写一个配置文件 db.properties 和相应的使用文件 JDBCConfig.java

 

1 mysql.Driver=com.mysql.jdbc.Driver
2 mysql.url=jdbc:mysql://127.0.0.1:3310/day02
3 mysql.username=root
4 mysql.password=123456
View Code

 

 1 package com.etc.config;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.IOException;
 7 import java.util.Properties;
 8 
 9 public class JDBCConfig {
10     public static String driver;
11     public static String url;
12     public static String username;
13     public static String password;
14     //必须使用静态代码块 优先加载 不然可能会报空指针异常
15     static {
16         //配置文件获取值
17         Properties pro = new Properties();
18         try {    
19             //通过FileInputStream读文件
20             pro.load(new FileInputStream(new File("src/db.properties")));
21             //通过键获取值
22             driver = pro.getProperty("mysql.Driver");
23             url = pro.getProperty("mysql.url");
24             username = pro.getProperty("mysql.username");
25             password = pro.getProperty("mysql.password");
26         } catch (FileNotFoundException e) {
27             e.printStackTrace();
28         } catch (IOException e) {
29             e.printStackTrace();
30         }
31     }
32 }

2.编写用户类 UserDao.java

 1 package com.ect.dao;
 2 
 3 public class UserDao {
 4     private int id;
 5     private String name;
 6     
 7     
 8     public UserDao() {
 9         super();
10     }
11 
12 
13     public UserDao(int id, String name) {
14         super();
15         this.id = id;
16         this.name = name;
17     }
18 
19 
20     @Override
21     public String toString() {
22         return "UserDao [id=" + id + ", name=" + name + "]";
23     }
24 
25 
26     public int getId() {
27         return id;
28     }
29 
30 
31     public void setId(int id) {
32         this.id = id;
33     }
34 
35 
36     public String getName() {
37         return name;
38     }
39 
40 
41     public void setName(String name) {
42         this.name = name;
43     }
44     
45     
46 }
View Code

3.编写工具类 JDBCUtil.java

 

 1 package com.etc.Util;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 
 9 import com.etc.config.JDBCConfig;
10 
11 public class JDBCUtil {
12 
13     static Connection conn = null;
14     static PreparedStatement st = null;
15 
16     /**
17      * 获取连接
18      * 
19      * @return
20      */
21     public static Connection getConnection() {
22         try {
23             // 注册驱动
24             Class.forName(JDBCConfig.driver);
25             // 连接数据库,传入库,账号,密码
26             conn = DriverManager.getConnection(JDBCConfig.url, JDBCConfig.username, JDBCConfig.password);
27         } catch (ClassNotFoundException e) {
28             e.printStackTrace();
29         } catch (SQLException e) {
30             e.printStackTrace();
31         }
32         return conn;
33     }
34 
35     /**
36      * 关闭连接
37      * 
38      * @param st
39      */
40     public static void close(Connection conn, PreparedStatement st, ResultSet rs) {
41         closeRs(rs);
42         closeSt(st);
43         closeConn(conn);
44     }
45 
46     public static void closeConn(Connection conn) {
47         if (conn != null) {
48             try {
49                 conn.close();
50             } catch (SQLException e) {
51                 e.printStackTrace();
52             }
53         }
54     }
55 
56     public static void closeSt(PreparedStatement st) {
57 
58         if (st != null) {
59             try {
60                 st.close();
61             } catch (SQLException e) {
62                 e.printStackTrace();
63             }
64         }
65     }
66 
67     public static void closeRs(ResultSet rs) {
68         if (rs != null) {
69             try {
70                 rs.close();
71             } catch (SQLException e) {
72                 e.printStackTrace();
73             }
74         }
75 
76     }
77 }

 

4.编写CURD语句 JDBCDml.java

 

  1 package com.etc.Util;
  2 
  3 import java.sql.Connection;
  4 import java.sql.PreparedStatement;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 import java.util.ArrayList;
  8 import java.util.List;
  9 
 10 import com.ect.dao.UserDao;
 11 
 12 public class JDBCDml {
 13     /**
 14      * 查询语句
 15      * 
 16      * @param sql
 17      * @return
 18      */
 19     // 查询语句 因为我们建立的是User表 传出来的值是对象 所以我们用一个集合来存储
 20     public static List<UserDao> select(String sql) {
 21         List<UserDao> list = new ArrayList<>();
 22         Connection conn = null;
 23         PreparedStatement st = null;
 24         ResultSet rs=null;
 25         try {
 26             // 注册驱动与获取连接
 27             conn = JDBCUtil.getConnection();
 28             // 执行语句
 29             st = conn.prepareStatement(sql);
 30             // 结果集
 31             rs = st.executeQuery();
 32             // 获取每一个对象和相对应的值
 33             while (rs.next()) {
 34                 int userid = rs.getInt("sid");
 35                 String username = rs.getString("name");
 36                 // 将值传给ud
 37                 UserDao ud = new UserDao(userid, username);
 38                 // 添加到集合
 39                 list.add(ud);
 40             }
 41         } catch (SQLException e) {
 42             e.printStackTrace();
 43         } finally {
 44             // 关闭连接
 45             JDBCUtil.close(conn,st,rs);
 46         }
 47         return list;
 48 
 49     }
 50 
 51     // 传一个对象用此方法
 52     // public static ResultSet insert(UserDao ud) {
 53     /**
 54      * 插入语句
 55      * 
 56      * @param sql
 57      * @param pramas
 58      * @return
 59      */
 60     public static ResultSet insert(String sql, Object... pramas) {
 61         Connection conn = null;
 62         PreparedStatement st = null;
 63         ResultSet rs = null;
 64         try {
 65             conn = JDBCUtil.getConnection();
 66             // 创建语句
 67             st = conn.prepareStatement(sql);
 68             // 设置参数
 69             for (int i = 0; i < pramas.length; i++) {
 70                 st.setObject(i + 1, pramas[i]);
 71             }
 72             // 设置参数,这是如果你需要传的是user对象则直接可以用get方法获取其相对应的值
 73             // st.setInt(1,ud.getId());
 74             // st.setString(2,ud.getName());
 75             // 执行语句
 76             st.executeUpdate();
 77         } catch (Exception e) {
 78             e.printStackTrace();
 79         } finally {
 80             // 关闭连接
 81             JDBCUtil.close(conn,st,rs);
 82         }
 83         return rs;
 84 
 85     }
 86     // 传一个对象用此方法
 87     // public static void update(UserDao ud){
 88 
 89     /**
 90      * 更新语句
 91      * 
 92      * @param sql
 93      * @param pramas
 94      */
 95     public static void update(String sql, Object... pramas) {
 96         Connection conn = null;
 97         PreparedStatement st = null;
 98         try {
 99             // 注册驱动与获取连接
100             conn = JDBCUtil.getConnection();
101             // 执行语句
102             st = conn.prepareStatement(sql);
103             // 设置参数
104             for (int i = 0; i < pramas.length; i++) {
105                 st.setObject(i + 1, pramas[i]);
106             }
107             // 设置参数
108             // st.setString(1,ud.getName());
109             // st.setInt(2,ud.getId());
110             st.executeUpdate();
111         } catch (SQLException e) {
112             e.printStackTrace();
113         } finally {
114             // 关闭连接
115             JDBCUtil.closeSt(st);
116             JDBCUtil.closeConn(conn);
117         }
118     }
119 
120     /**
121      * 删除语句
122      * 
123      * @param sql
124      * @param pramas
125      */
126     // public static void delete(UserDao ud) {
127     public static void delete(String sql, Object... pramas) {
128         Connection conn = null;
129         PreparedStatement st = null;
130         try {
131             // 注册驱动与获取连接
132             conn = JDBCUtil.getConnection();
133             // 执行语句
134             st = conn.prepareStatement(sql);
135             // 设置参数
136             for (int i = 0; i < pramas.length; i++) {
137                 st.setObject(i + 1, pramas[i]);
138             }
139             // 设置参数
140             // st.setInt(1, ud.getId());
141             st.executeUpdate();
142         } catch (SQLException e) {
143             e.printStackTrace();
144         } finally {
145             // 关闭连接
146             JDBCUtil.closeSt(st);
147             JDBCUtil.closeConn(conn);
148         }
149     }
150 }

 

5.编写测试类 JDBCDemo.java

 1 package com.etc.domain;
 2 
 3 import java.util.List;
 4 
 5 import com.ect.dao.UserDao;
 6 import com.etc.Util.JDBCDml;
 7 
 8 
 9 public class JDBCDemo {
10 
11     public static void main(String[] args) {
12         //JDBCDml.insert("insert into sanguo (sid,name) values (?,?)","2","adaasd");
13         //JDBCDml.update("update sanguo set name=(?) where sid=(?)","awerq",2);
14         //JDBCDml.delete("delete from sanguo where sid=?",2);
15         
16         List<UserDao> list = JDBCDml.select("select * from sanguo");
17         for (UserDao sanGuo : list) {
18             System.out.println(sanGuo);
19         }
20             
21         //User();
22     }
23 
24     private static void User() {
25         UserDao ud = new UserDao();
26         ud.setId(4);
27         ud.setName("aa5453a");
28         
29         //JDBCDml.insert(ud);
30         //JDBCDml.update(ud);
31         //JDBCDml.delete(ud);
32     }
33 
34 }

完成。现在只是自己写的连接 一些连接池啊 什么的都没有完成  等以后再更新。

 

需要把时间字段类型设置为“timestamp”,然后再在默认值中填写“CURRENT_TIMESTAMP”

posted @ 2019-06-06 15:42  刘认真  阅读(240)  评论(0编辑  收藏  举报