forked from maxliaops/Java_Web_Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBResult.java
More file actions
58 lines (52 loc) · 1.29 KB
/
DBResult.java
File metadata and controls
58 lines (52 loc) · 1.29 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
package com.mingri.dbconn;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class DBResult {
public static Connection conn = null;
private static String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=db_shopping";
private static String user="sa";
private static String pwd = "";
static{
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(URL, user, pwd);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 用于获得执行SQL语句的ResultSet对象
*/
public ResultSet getResult(String sql) {
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
return rs;
} catch (Exception e) {
}
return null;
}
/**
* 用于执行SQL语句没有返回值
*/
public void doExecute(String sql) {
try {
Statement stmt = conn.createStatement();
stmt.executeQuery(sql);
} catch (Exception e) {
}
}
/**
* 用于获得执行SQL语句的PreparedStatement(预处理)对象
*/
public PreparedStatement getPreparedStatement(String sql) {
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
return pstmt;
} catch (Exception e) {
}
return null;
}
}