1. 程式人生 > >web項目與數據庫連接方法

web項目與數據庫連接方法

ade trac 用戶 pre oid 執行 執行sql 讀取配置 連接

一、直接連接,不封裝到工具類中,主要步驟:

先導包:mysql-connector-java-5.1.46-bin.jar(點擊跳轉到下載界面),放在WebRoot/WEB-INF/lib/下

1.加載驅動//com.mysql.jdbc.Driver

2.獲取連接 Connection對象

3.獲取用於向數據庫發送SQL的Statement對象

4.執行sql,獲取數據,解析數據

5.關閉連接,釋放資源

 1            /* 協議:子協議://主機:端口/數據庫名 */  
 2 String url = "jdbc:mysql://localhost:3306/jdbctest";  
3 4 // mysql數據庫的用戶名與密碼,安裝時自己設置,一般默認為root 5 String user = "root"; 6 String password = "root"; 7 8 Connection connection = null; 9 Statement statement = null; 10 ResultSet resultSet = null; 11 try { 12 // 1.加載驅動//com.mysql.jdbc.Driver 13 /* 14 * DriverManager.registerDriver(new
15 * Driver());用這種方法會加載兩次驅動,也就是說會創建兩個drive對象 16 */ 17 Class.forName("com.mysql.jdbc.Driver"); 18 // 2.獲取連接 19 connection = DriverManager.getConnection(url, user, password); 20 21 // 3.獲取用於向數據庫發送SQL的Statement對象 22 statement = connection.createStatement(); 23 24
// 4.執行sql,獲取數據 25 resultSet = statement.executeQuery("SELECT * FROM users;"); 26 27 // 解析數據 28 while (resultSet.next()) { 29 int id = resultSet.getInt("id"); 30 String name = resultSet.getString("name"); 31 String psd = resultSet.getString("password"); 32 String email = resultSet.getString("email"); 33 String birthday = resultSet.getString("birthday"); 34 35 System.out.println(id + " " + name + " " + psd + " " + email 36 + " " + birthday); 37 } 38 } catch (ClassNotFoundException e) { 39 e.printStackTrace(); 40 } catch (SQLException e) { 41 e.printStackTrace(); 42 } finally { 43 44 //5.關閉連接,釋放資源 45 if (resultSet != null) { 46 try { 47 resultSet.close(); 48 } catch (SQLException e) { 49 // TODO Auto-generated catch block 50 e.printStackTrace(); 51 } 52 resultSet = null; 53 } 54 55 if (statement != null) { 56 try { 57 statement.close(); 58 } catch (SQLException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 } 62 statement = null; 63 } 64 65 if (connection != null) { 66 try { 67 connection.close(); 68 } catch (SQLException e) { 69 // TODO Auto-generated catch block 70 e.printStackTrace(); 71 } 72 connection = null; 73 } 74 }

二、將數據庫連接封裝成一個工具類

這樣做的好處是,在實際開發中,就能做到,改一處即可修改全局。

1.建一個名為db.properties的配置文件,放於src/

1 url=jdbc:mysql://localhost:3306/jdbctest  
2 username=root  
3 password=root  
4 driver=com.mysql.jdbc.Driver

2.工具類:

 1 import java.io.IOException;
 2 import java.sql.Connection;
 3 import java.sql.DriverManager;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 import java.sql.Statement;
 7 import java.util.Properties;
 8 
 9 public class JdbcUtil {
10     
11     //私有靜態變量,用以讀取配置文件
12     private static Properties config=new Properties();
13     
14     static{
15         try {
16             //配置資源文件
17             config.load(JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"));
18             
19             //加載驅動
20             Class.forName(config.getProperty("driver"));
21         } catch (IOException e) {
22             e.printStackTrace();
23         } catch (ClassNotFoundException e) {
24             e.printStackTrace();
25         }
26     }
27     
28     public static  Connection getConnection(){
29         Connection connection=null;
30         try {
31             connection=DriverManager.getConnection(config.getProperty("url"),config.getProperty("username"),config.getProperty("password"));
32         } catch (SQLException e) {
33             e.printStackTrace();
34         }
35         
36         return connection;
37     }
38     //用以關閉連接,釋放資源
39     public static void releaseConn(Connection connection, Statement statement,
40             ResultSet resultSet) {
41         if(resultSet!=null){
42             try {
43                 resultSet.close();
44             } catch (SQLException e) {
45                 e.printStackTrace();
46             }
47             resultSet=null;
48         }
49         
50         if(statement!=null){
51             try {
52                 statement.close();
53             } catch (SQLException e) {
54                 e.printStackTrace();
55             }
56             statement=null;
57         }
58         
59         if(connection!=null){
60             try {
61                 connection.close();
62             } catch (SQLException e) {
63                 e.printStackTrace();
64             }
65             connection=null;
66         }
67     }
68     
69 
70 }

3.使用實例:

 1         Connection connection = null;
 2         Statement statement = null;
 3         ResultSet resultSet = null;
 4         try {
 5             // 調用工具類中的靜態方法來獲取連接
 6             connection = JdbcUtil.getConnection();
 7             statement = connection.createStatement();
 8             resultSet = statement.executeQuery("select * from users");
 9             while (resultSet.next()) {
10                 int id = resultSet.getInt("id");
11                 String name = resultSet.getString("name");
12                 String psd = resultSet.getString("password");
13                 String email = resultSet.getString("email");
14                 String birthday = resultSet.getString("birthday");
15 
16                 System.out.println(id + " " + name + " " + psd + " " + email
17                         + " " + birthday);
18 
19             }
20         } catch (Exception e) {
21             e.printStackTrace();
22         } finally {
23             // 調用工具類中的靜態方法來關閉連接,釋放資源
24             JdbcUtil.releaseConn(connection, statement, resultSet);
25         }
26     }

web項目與數據庫連接方法