您的位置:首页 > 数据库

jdbc基础 (二) 通过properties配置文件连接数据库

2017-09-24 17:27 716 查看
本文为转载

因为jdbc对数据库的驱动加载、连接获取、释放资源的代码都是相同的,为了提高代码的复用性,我们可以写一个工具类,将数据库驱动加载、获取连接、资源释放的代码封装起来。同时,为了提高工具类的灵活性,可以将数据库的驱动、url、用户名、密码等信息以键值对的形式存放在properties文件中,工具类初始化时从配置文件中读取所要连接数据库的信息。当需要更改连接的数据库时,只需要更改配置文件即可,而不必改写工具类的代码。

下面是工具类代码的实现:

1 package com.cream.ice.jdbc;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.sql.Connection;
6 import java.sql.DriverManager;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
9 import java.sql.Statement;
10 import java.util.Properties;
11
12 public class JdbcUtils {
13
14     private static
4000
String driverName;
15     private static String url;
16     private static String user;
17     private static String password;
18
19     /*
20      * 静态代码块,类初始化时加载数据库驱动
21      */
22     static {
23         try {
24             // 加载dbinfo.properties配置文件
25             InputStream in = JdbcUtils.class.getClassLoader()
26                     .getResourceAsStream("dbinfo.properties");
27             Properties properties = new Properties();
28             properties.load(in);
29
30             // 获取驱动名称、url、用户名以及密码
31             driverName = properties.getProperty("driverName");
32             url = properties.getProperty("url");
33             user = properties.getProperty("user");
34             password = properties.getProperty("password");
35
36             // 加载驱动
37             Class.forName(driverName);
38
39         } catch (IOException e) {
40             e.printStackTrace();
41         } catch (ClassNotFoundException e) {
42             e.printStackTrace();
43         }
44     }
45
46     /*
47      * 获取连接
48      */
49     public static Connection getConnection() throws SQLException {
50
51         return DriverManager.getConnection(url, user, password);
52
53     }
54
55     /*
56      * 释放资源
57      */
58     public static void releaseResources(ResultSet resultSet,
59             Statement statement, Connection connection) {
60
61         try {
62             if (resultSet != null)
63                 resultSet.close();
64         } catch (SQLException e) {
65             e.printStackTrace();
66         } finally {
67             resultSet = null;
68             try {
69                 if (statement != null)
70                     statement.close();
71             } catch (SQLException e) {
72                 e.printStackTrace();
73             } finally {
74                 statement = null;
75                 try {
76                     if (connection != null)
77                         connection.close();
78                 } catch (SQLException e) {
79                     e.printStackTrace();
80                 } finally {
81                     connection = null;
82                 }
83             }
84         }
85
86     }
87
88 }


 

这里dbinfo.properties文件中信息如下,读者可自行更改:

1 driverName=com.mysql.jdbc.Driver
2 url=jdbc:mysql://localhost:3306/jdbc
3 user=root
4 password=123456


 

这里我们来举个例子使用工具类。我们写一个类JdbcCURD实现对特定数据库的增删改查操作,并在main函数中使用。

JdbcCURD.java代码如下:

1 package com.cream.ice.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 JdbcCURD {
9
10     private Connection connection;
11     private Statement statement;
12     private ResultSet resultSet;
13
14     //更新操作
15     public void update(String sql) {
16         try {
17             connection = JdbcUtils.getConnection();
18             statement = connection.createStatement();
19             //可执行创建、修改、删除表,添加、删除、修改元组以及查询sql语句
20             statement.execute(sql);
21         } catch (SQLException e) {
22             e.printStackTrace();
23         } finally {
24             JdbcUtils.releaseResources(resultSet, statement, connection);
25         }
26     }
27
28     //查询操作
29     public void Query(String sql) {
30         try {
31             connection = JdbcUtils.getConnection();
32             statement = connection.createStatement();
33             resultSet = statement.executeQuery(sql);
34
35             while(resultSet.next()){
36                 System.out.println("name:"+resultSet.getString("name"));
37                 System.out.println("id:"+resultSet.getString("Tid"));
38             }
39
40         } catch (SQLException e) {
41             e.printStackTrace();
42         } finally {
43             JdbcUtils.releaseResources(resultSet, statement, connection);
44         }
45     }
46
47     //添加操作
48     public void addElement(String sql) {
49         update(sql);
50     }
51
52     //删除操作
53     public void removeElement(String sql) {
54         update(sql);
55     }
56
57     //创建一个表
58     public void createTable(String sql){
59         update(sql);
60     }
61
62     //删除一个表
63     public void dropTable(String sql){
64         update(sql);
65     }
66
67 }


 

我们来写一个main函数来测试:

1 package com.cream.ice.jdbc;
2
3 import java.sql.SQLException;
4
5 public class JdbcTest {
6
7     public static void main(String[] args) throws ClassNotFoundException,
8             SQLException {
9
10         JdbcCURD curd=new JdbcCURD();
11
12         String sql = null;
13
14         //添加表Teacher
15         sql="create table Teacher (Tid char(9) primary key,name char(9) unique)";
16         curd.createTable(sql);
17
18         //添加元组
19         sql = "insert into Teacher (Tid,name) values ('0001','Tom')";
20         curd.addElement(sql);
21
22         //查询Teacher表
23         sql="select * from Teacher";
24         curd.Query(sql);
25
26         //删除元组
27         sql="delete from Teacher where Tid='0001'";
28         curd.removeElement(sql);
29
30         //删除表Teacher
31         sql="drop table Teacher";
32         curd.dropTable(sql);
33     }
34
35 }


 

经测试,将在控制台输出下列信息:

1 name:Tom
2 id:0001
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: