-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBConnect.java
More file actions
44 lines (36 loc) · 991 Bytes
/
DBConnect.java
File metadata and controls
44 lines (36 loc) · 991 Bytes
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
package com.DBConnect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBConnect {
// 7个变量
private String DriverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String Url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=FlowerDBTest";
private String User = "sa";
private String Pwd = "123456";
// 创建数据库连接
public Connection conn = null;
public Connection getConnection()throws SQLException{
try {
Class.forName(DriverName);
conn=DriverManager.getConnection(Url,User,Pwd);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn,PreparedStatement pstm,ResultSet rs) {
try {
if(rs!=null);
rs.close();
if(pstm!=null);
pstm.close();
if(conn!=null)
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}