JDBC

JDBC编程步骤

 

1.加载驱动

Class.forName("com.mysql.cj.jdbc.Driver");

2.创建连接

String url="jdbc:mysql://localhost:3306/hhr_data?useSSL=false&characterEncoding=utf8&serverTimezone=UTC";

//Connection 关联
con = DriverManager.getConnection(url,"root","41978632");

3.执行对象

sta = con.createStatement();

4.执行语句

String sql = "select s_id,s_name,s_sex from student";
rs = sta.executeQuery(sql);//数据集

5.解析执行结果

6.释放资源

 1             String s_id = null;
 2             String s_name = null;
 3             String s_sex = null;
 4             while(rs.next()) {//一行一行的从数据集里面找数据
 5                 s_id = rs.getString(1);
 6                 s_name = rs.getString("s_name");
 7                 s_sex = rs.getString("s_sex");
 8                 System.out.println(s_id + "---" + s_name + "---" + s_sex);
 9             }
10         } catch (Exception e) {
11             e.printStackTrace();
12         } finally {
13             //6.释放资源
14             if(rs!=null) {
15                 try {
16                     rs.close();
17                 } catch (SQLException e) {
18                     e.printStackTrace();
19                 }
20             }
21             if(sta!=null) {
22                 try {
23                     sta.close();
24                 } catch (Exception e) {
25                     e.printStackTrace();
26                 }
27             }
28             if(con!=null) {
29                 try {
30                     con.close();
31                 } catch (Exception e) {
32                     e.printStackTrace();
33                 }
34             }
35         }

 

可执行的代码块:

 1 package com.easy.jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 
 9 public class Easy {
10     public static void main(String[] args) throws ClassNotFoundException, SQLException {
11         jdbc();
12     }
13     public static void jdbc() {
14         //声明出对象来
15         Connection con = null;
16         Statement sta = null;
17         ResultSet rs = null;
18         try {
19             //1.加载驱动
20             Class.forName("com.mysql.cj.jdbc.Driver");
21             //2.创建连接
22             String url="jdbc:mysql://localhost:3306/hhr_data?useSSL=false&characterEncoding=utf8&serverTimezone=UTC";
23             con = DriverManager.getConnection(url,"root","41978632");
24             //3.执行对象
25             sta = con.createStatement();
26             //4.执行语句
27             String sql = "select s_id,s_name,s_sex from student";
28             rs = sta.executeQuery(sql);//数据集
29             //5.解析执行结果
30             String s_id = null;
31             String s_name = null;
32             String s_sex = null;
33             while(rs.next()) {//一行一行的从数据集里面找数据
34                 s_id = rs.getString(1);
35                 s_name = rs.getString("s_name");
36                 s_sex = rs.getString("s_sex");
37                 System.out.println(s_id + "---" + s_name + "---" + s_sex);
38             }
39         } catch (Exception e) {
40             e.printStackTrace();
41         } finally {
42             //6.释放资源
43             if(rs!=null) {
44                 try {
45                     rs.close();
46                 } catch (SQLException e) {
47                     e.printStackTrace();
48                 }
49             }
50             if(sta!=null) {
51                 try {
52                     sta.close();
53                 } catch (Exception e) {
54                     e.printStackTrace();
55                 }
56             }
57             if(con!=null) {
58                 try {
59                     con.close();
60                 } catch (Exception e) {
61                     e.printStackTrace();
62                 }
63             }
64         }
65     }
66 }

 

JDBC的封装:

  1 package com.easy.jdbc;
  2 
  3 import java.io.FileInputStream;
  4 import java.io.IOException;
  5 import java.lang.reflect.Field;
  6 import java.sql.Connection;
  7 import java.sql.DriverManager;
  8 import java.sql.ResultSet;
  9 import java.sql.SQLException;
 10 import java.sql.Statement;
 11 import java.util.Properties;
 12 
 13 public class JDBCUtil {
 14     private Connection con = null;
 15     private Statement sta = null;
 16     private ResultSet rs = null;
 17     private static JDBCUtil util;
 18     
 19     private String driverClassName = null;
 20     private String url = null;
 21     private String username = null;
 22     private String password = null;
 23     
 24     static {
 25         try {
 26             util = new JDBCUtil();
 27         } catch (IOException e) {
 28             // TODO Auto-generated catch block
 29             e.printStackTrace();
 30         }
 31     }
 32     
 33     //获取当前类的实例
 34     public static JDBCUtil getInstance() {
 35         return util;
 36     }
 37     
 38     //确保con里面有东西
 39     public Connection getConnection() throws SQLException, ClassNotFoundException {
 40         Class.forName(driverClassName);
 41         if(con == null) {
 42             synchronized (driverClassName) {
 43                 if(con == null){
 44                     con = DriverManager.getConnection(url , username , password);
 45                 }
 46             }
 47         }
 48         return con;
 49     }
 50     
 51     //获取Statement
 52     public Statement getStatement() throws ClassNotFoundException, SQLException {
 53         if(con == null) {
 54             getConnection();
 55         }
 56         sta = con.createStatement();
 57         return sta;
 58     }
 59     
 60     //query查询
 61     public Student queryStudent(String sql) throws ClassNotFoundException, SQLException {
 62         if(sta == null) {
 63             getStatement();
 64         }
 65         rs = sta.executeQuery(sql);
 66         boolean hasNext = rs.next();
 67         Student stu = new Student();
 68         if(hasNext) {
 69             stu.setS_id(rs.getString(1));
 70             stu.setS_name(rs.getString(2));
 71             stu.setS_birth(rs.getString(3));
 72             stu.setS_sex(rs.getString(4));
 73         }
 74         return stu;
 75     }
 76     
 77     public <T> T executeQuery(String sql , Class<T> clazz) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
 78         if(sta == null) {
 79             getStatement();
 80         }
 81         rs = sta.executeQuery(sql);
 82         //解析
 83         //获取类中的属性
 84         Field[] fis = clazz.getDeclaredFields();
 85         String fileName = null;
 86         String value = null;
 87         //从结果集中拿取指定的数据设置到一个对象中,把对象返回
 88         boolean hasnext = rs.next();
 89         T t = clazz.newInstance();
 90         if(hasnext) {
 91             for(Field f:fis) {
 92                 String fieldName = f.getName();
 93                 f.setAccessible(true);
 94                 f.set(t, rs.getString(fieldName));
 95             }
 96         }
 97         return t;
 98     }
 99     
100     private JDBCUtil() throws IOException {
101         // 获得文件路径
102         String fileName = System.getProperty("user.dir") + "\\jdbc.properties";
103         FileInputStream fis = new FileInputStream(fileName);
104         //Properties继承自HashTable,里面存的是键值对
105         Properties p = new Properties();//文件内容就在p对象里面
106         //通过配置文件读取到了这个文件
107         //把这些文件里的内容读取到p对象里面
108         p.load(fis);
109         driverClassName = p.getProperty("driver_class_name");
110         url = p.getProperty("url");
111         username = p.getProperty("username");
112         password = p.getProperty("password");
113     }
114     @Override
115     public String toString() {
116         return "JDBCUtil [con=" + con + ", sta=" + sta + ", rs=" + rs + ", driverClassName=" + driverClassName
117                 + ", url=" + url + ", username=" + username + ", password=" + password + "]";
118     }
119     public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
120         JDBCUtil util = JDBCUtil.getInstance();
121         Student stu = util.executeQuery("select * from Student where s_id='01'", Student.class);
122         System.out.println(stu.getS_name()+"----");
123     }
124 }

 modifer

 1     public static void modifier () throws Exception{
 2         Class clazz = Student.class;
 3         int mo = clazz.getModifiers();
 4         boolean b = Modifier.isPublic(mo);
 5         System.out.println(b);//true
 6         Method m = clazz.getDeclaredMethod("Study");
 7         mo=m.getModifiers();
 8         b = Modifier.isPublic(mo);
 9         boolean s = Modifier.isPublic(mo);
10         System.out.println(s);//true
11     }

 

posted @ 2022-02-18 17:29  Nickeyhu  阅读(27)  评论(0编辑  收藏  举报