jdbc工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package tool;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
* 这是JDBC的工具类
* 这个类提供了获取数据库连接对象的方法
*
*/
public class {
private static Connection con;
private static String url ;
private static String user;
private static String password;
private static String className;
static{
try{
readConfig();
//获取驱动
Class.forName(className);
//获取连接
con = DriverManager.getConnection(url, user, password);
}catch(Exception ex){
System.out.println(ex +"连接失败");
}
}
public static void readConfig() throws Exception{
Properties p = new Properties();
FileReader fr = new FileReader("config.Properties");
p.load(fr);
className = p.getProperty("drivername");
url = p.getProperty("url");
user = p.getProperty("user");
password = p.getProperty("password");
}
public static Connection getConnection() {
return con;
}
}