j2ee 使用db.properties连接mysql数据库

转自: http://blog.csdn.net/u013815546/article/details/50808493

注: 下面的方法是未安装构架的写法,需要自己加载驱动并建立连接. 若引入了ActiveJDBC框架 ,可以直接用Base.open()方法连接数据库.

 

j2ee 使用db.properties连接mysql数据库

 分类:

db.properties保存数据库信息,使用JdbcUtil.java作为连接数据库的工具类,是初学java+mysql的常见连接方式

   
 1 package cn.xujingfeng;  
 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   
13 public class JdbcUtil {  
14       
15       
16     static String url = null;  
17     static String user = null;  
18     static String password = null;  
19     static String driverClass = null;  
20       
21     static{  
22         try {  
23               
24             Properties properties = new Properties();  
25             InputStream in = JdbcUtil.class.getResourceAsStream("/db.properties");  
26             properties.load(in);  
27               
28             url = properties.getProperty("url");  
29             user = properties.getProperty("user");  
30             password = properties.getProperty("password");  
31             driverClass = properties.getProperty("driverClass");  
32               
33               
34             Class.forName(driverClass);  //register JDBC driver35               
36         } catch (Exception e) {  
37             e.printStackTrace();  
38             System.out.println("驱动加载失败!");  
39             throw new RuntimeException(e);  
40         }  
41           
42     }  
43       
44     public static Connection getConnection(){  
45         try {  
46             Connection conn = DriverManager.getConnection(url, user, password);  
47             return conn;  
48         } catch (SQLException e) {  
49             e.printStackTrace();  
50             throw new RuntimeException(e);  
51         }  
52     }   
53       
54     public static void close(Connection conn,Statement st,ResultSet rs){  
55         if(conn!=null){  
56             try{  
57                 conn.close();  
58             }catch (Exception e) {  
59                 throw new RuntimeException(e);  
60             }  
61         }  
62           
63         if(st!=null){  
64             try{  
65                 st.close();  
66             }catch (Exception e) {  
67                 throw new RuntimeException(e);  
68             }  
69         }  
70           
71         if(rs!=null){  
72             try{  
73                 rs.close();  
74             }catch (Exception e) {  
75                 throw new RuntimeException(e);  
76             }  
77         }  
78     }  
79       
80     public static void close(Connection conn,Statement st){  
81         if(conn!=null){  
82             try{  
83                 conn.close();  
84             }catch (Exception e) {  
85                 throw new RuntimeException(e);  
86             }  
87         }  
88           
89         if(st!=null){  
90             try{  
91                 st.close();  
92             }catch (Exception e) {  
93                 throw new RuntimeException(e);  
94             }  
95         }  
96     }  
97       
98 }  
注意导包时eclipse具有mysql的两个包,一个是java.sql.*,一个是com.mysql.jdbc.*。前者是应该选择的,后者是针对mysql优化使用的。举例来说,Statement这个类在两个包里面都有,但是继承关系不太一致,总之就是属于两个不同的类,注意不要混淆使用两个包,如果出现什么莫名其妙的报错,注意看看是不是包导错了。
 
1 InputStream in = JdbcUtil.class.getResourceAsStream("/db.properties"); 

这样的目的是为了针对java project和java web project的路径不一致而设计的,前者编译过后的当前路径是bin,后者则会是tomcat的bin目录,有区别,使用getResourceAsStream能保证两者都能读取到db.properties文件

db.properties文件配置大致如下,根据个人数据库自行修改

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

这是最基础的连接数据库方式
原文地址:https://www.cnblogs.com/cheese320/p/8529956.html