forked from maxliaops/Java_Web_Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnDB.java
More file actions
125 lines (116 loc) · 3.65 KB
/
ConnDB.java
File metadata and controls
125 lines (116 loc) · 3.65 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.tools;
import java.sql.*;
import java.io.*;
import java.util.*;
public class ConnDB {
public Connection conn = null;
public Statement stmt = null;
public ResultSet rs = null;
private static String propFileName = "/com/connDB.properties"; // 指定资源文件保存的位置
private static Properties prop = new Properties();
private static String dbClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String dbUrl = "jdbc:sqlserver://localhost:1433;DatabaseName=db_shop";
private static String dbUser = "sa";
private static String dbPwd = "";
public ConnDB() {
try {
InputStream in = getClass().getResourceAsStream(propFileName);
prop.load(in); // 通过输入流对象加载Properties文件
dbClassName = prop.getProperty("DB_CLASS_NAME"); // 获取数据库驱动
dbUrl = prop.getProperty("DB_URL", dbUrl);
dbUser = prop.getProperty("DB_USER", dbUser);
dbPwd = prop.getProperty("DB_PWD", dbPwd);
} catch (Exception e) {
e.printStackTrace(); // 输出异常信息
}
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
} catch (Exception ee) {
ee.printStackTrace();
}
if (conn == null) {
System.err
.println("警告: DbConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:"
+ dbClassName
+ "\r\n链接位置:"
+ dbUrl
+ "\r\n用户/密码"
+ dbUser + "/" + dbPwd);
}
return conn;
}
/*
* 功能:执行查询语句
*/
public ResultSet executeQuery(String sql) {
try { // 捕捉异常
conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {
System.err.println(ex.getMessage()); // 输出异常信息
}
return rs; // 返回结果集对象
}
/*
* 功能:执行更新操作
*/
public int executeUpdate(String sql) {
int result = 0; // 定义保存返回值的变量
try { // 捕捉异常
conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
result = stmt.executeUpdate(sql); // 执行更新操作
} catch (SQLException ex) {
result = 0; // 将保存返回值的变量赋值为0
}
try {
stmt.close();
} catch (SQLException ex1) {
}
return result; // 返回保存返回值的变量
}
public int executeUpdate_id(String sql) {
int result = 0; // 定义保存返回值的变量
try { // 捕捉异常
conn = getConnection(); // 调用getConnection()方法构造Connection对象的一个实例conn
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
result = stmt.executeUpdate(sql); // 执行更新操作
// 获取刚刚插入记录的自动编号
String ID = "select @@IDENTITY as id";
rs = stmt.executeQuery(ID);
if (rs.next()) {
int autoID = rs.getInt("id");
result = autoID;
}
} catch (SQLException ex) {
result = 0;
}
return result; // 返回刚刚插入的自动编号
}
/*
* 功能:关闭数据库的连接
*/
public void close() {
try { // 捕捉异常
if (rs != null) { // 当ResultSet对象的实例rs不为空时
rs.close(); // 关闭ResultSet对象
}
if (stmt != null) { // 当Statement对象的实例stmt不为空时
stmt.close(); // 关闭Statement对象
}
if (conn != null) { // 当Connection对象的实例conn不为空时
conn.close(); // 关闭Connection对象
}
} catch (Exception e) {
e.printStackTrace(System.err); // 输出异常信息
}
}
}