-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBHelper.java
More file actions
100 lines (93 loc) · 2.4 KB
/
DBHelper.java
File metadata and controls
100 lines (93 loc) · 2.4 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package io.github.hooj0.jdbc;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* 数据库连接助手类、工具类、辅助类
* @author hoojo
* @createDate 2011-5-9 下午09:36:00
* @file DBHelper.java
* @package com.hoo.base
* @project JavaJDBC
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public abstract class DBHelper {
private static String driver;
private static String url;
private static String user;
private static String password;
private static Connection conn;
/**
* 加载参数信息
* @author hoojo
* @createDate May 9, 2011 9:25:45 PM
* @param paramFile
* @throws IOException
*/
public static void loadParams(String paramFile) throws IOException {
//使用properties文件来加载参数信息
Properties props = new Properties();
try {
props.load(new FileInputStream(paramFile));
} catch (FileNotFoundException e) {
throw new RuntimeException("文件不存在" + e);
}
driver = props.getProperty("driver");
url = props.getProperty("url");
user = props.getProperty("user");
password = props.getProperty("password");
}
/**
* 获得数据库连接
* @author hoojo
* @createDate 2011-5-9 下午09:47:21
* @return 返回数据库连接
*/
public final static Connection getConnection() {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(driver + " 驱动类不存在!" + e);
}
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("数据库连接出现错误 " + e);
}
return conn;
}
/**
* 关闭数据库连接
* @author hoojo
* @createDate 2011-5-9 下午09:46:53
* @return 关闭是否成功
*/
public final static boolean close() {
boolean flag = false;
try {
if (conn != null && !conn.isClosed()) {
try {
conn.close();
flag = true;
} catch (SQLException e) {
e.printStackTrace();
flag = false;
throw new RuntimeException("数据库关闭错误" + e);
}
}
} catch (SQLException e) {
e.printStackTrace();
flag = false;
throw new RuntimeException("数据库关闭错误" + e);
}
return flag;
}
}