JDBC——架构层、驱动

JDBC(java Datebase Connector)

jdbc驱动程序

四种类型:

  jdbc-odbc桥接驱动程序

  Native-API

  JDBC-Net

  Native-Protocol  (常见)

 1 public class DBToolOld
 2 {
 3     public static void main(String[] args)
 4     {
 5         // 1、注册驱动
 6         // DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
 7         // new驱动类的时候会注册一份,DriverManager.registerDriver又会去注册一份驱动
 8         // 硬编码在类中,不方便修改
 9         // System.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
10         // 因为key和value都是字符串,可以改到配置文件中
11         // 需要约定好key的值,注册起来不太方便
12         try
13         {
14             Class.forName("oracle.jdbc.driver.OracleDriver");
15             // 2、获得连接对象
16             String oracleUrl = "jdbc:oracle:thin:@192.168.6.100:1521:NIIT";
17             Connection conn = DriverManager.getConnection(oracleUrl, "scott",
18                     "tiger");
19             // 3、开始执行SQL语句,先创建Statement对象
20             Statement stmt = conn.createStatement();
21             // 4、执行SQL语句,返回一个结果集,只有是SELECT语句、executeQuery才会返回结果集
22             ResultSet rs = stmt
23                     .executeQuery("SELECT empNo, ename, hireDate, sal FROM Emp");
24             // 5、处理结果集
25             while (rs.next())
26             {
27                 int empNo = rs.getInt(1); //根据列下标获得值
28                 System.out.println(empNo);
29                 String ename = rs.getString("ename"); //根据列名获得值,推荐这种方式
30                 System.out.println(ename);
31                 Date joinDate = rs.getDate("hireDate");
32                 System.out.println(joinDate);
33                 Double sal = rs.getDouble("sal");
34                 System.out.println(sal);
35                 System.out.println("============");
36             }
37             //6、关闭资源
38             rs.close();
39             stmt.close();
40             conn.close();
41         }
42         catch (ClassNotFoundException e)
43         {
44             // TODO Auto-generated catch block
45             e.printStackTrace();
46         }
47         catch (SQLException e)
48         {
49             // TODO Auto-generated catch block
50             e.printStackTrace();
51         }
52 
53     }
54 }

实际上常用的是:

 1 public class DBTool
 2 {
 3     private static DBTool instance;
 4 
 5     private DBTool()
 6     {
 7 
 8     }
 9 
10     public static DBTool getInstance()
11     {
12         if (instance == null)
13         {
14             instance = new DBTool(); // 如果该实例为空,表示没实例化,在自己这里实例化,否则直接返回已经实例化好的对象
15         }
16         return instance;
17     }
18 
19     public Connection getConnection()
20     {
21         Connection conn = null;
22         try
23         {
24             Properties p = new Properties();
25             p.load(new FileReader("db.properties"));
26             String driverName = p.getProperty("driverName");
27             String url = p.getProperty("url");
28             String userName = p.getProperty("username");
29             String pwd = p.getProperty("password");
30             Class.forName(driverName);
31             conn = DriverManager.getConnection(url, userName, pwd);
32         }
33         catch (IOException | SQLException | ClassNotFoundException e)
34         {
35             // TODO Auto-generated catch block
36             e.printStackTrace();
37         }
38         return conn;
39     }
40 
41     public static void closeAll(Connection conn, Statement stmt, ResultSet rs)
42     {
43         close(rs);
44         close(stmt);
45         close(conn);
46     }
47 
48     private static void close(AutoCloseable ac)
49     {
50         try
51         {
52             if (ac != null)
53             {
54                 ac.close();
55                 ac = null;
56             }
57         }
58         catch (Exception e)
59         {
60             // TODO Auto-generated catch block
61             e.printStackTrace();
62         }
63     }
64 }

此时驱动配置文件使用映射:

1 driverName = com.mysql.jdbc.Driver
2 url = jdbc:mysql://192.168.6.66:3307/hn
3 username = zxj
4 password = wxniit

 

你可能感兴趣的:(JDBC——架构层、驱动)