-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBaseconnection.java
More file actions
79 lines (67 loc) · 1.54 KB
/
DataBaseconnection.java
File metadata and controls
79 lines (67 loc) · 1.54 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
package login;
//数据库的连接
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DataBaseconnection {
Connection con = null;
Statement sql = null;
ResultSet rs = null;
public DataBaseconnection() {
// 将可能出现异常的语句放入try块中.
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // 加载驱动
} catch (ClassNotFoundException e) {
System.out.println(e);//catch块处理异常.
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/abcd","root","578111"); // 连接数据库
} catch (SQLException e) {
System.out.println(e);
}
}
// 查询
public ResultSet executeQuery(String sql_s) {
try {
sql = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = sql.executeQuery(sql_s);
} catch (SQLException e) {
System.out.println(e);
}
return rs;
}
// update操作
public int executeUpdate(String sql_s) {
int rs = 0;
try {
sql = con.createStatement();
rs = sql.executeUpdate(sql_s);
} catch (SQLException e) {
System.out.println(e);
}
return rs;
}
// 关闭数据库
public void close() {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//还有很多功能没有实现,后面有时间再补上!!Ps:正在学习中~~~