forked from uboger/LibraryManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbOp.java
58 lines (57 loc) · 1.26 KB
/
DbOp.java
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
//DbOp.java;
//package PublicModule;
import java.sql.*;
import javax.swing.JOptionPane;
public class DbOp{
private static String driver="sun.jdbc.odbc.JdbcOdbcDriver";
private static String url="jdbc:odbc:bookdb";
private static Connection con=null;
private DbOp(){
try{
if(con==null){
Class.forName(driver);
con=DriverManager.getConnection(url);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null,"数据库未能打开!");
System.out.println(e.getMessage());
}
}
public static ResultSet executeQuery(String sql){
ResultSet rs=null;
try{
if(con==null){
new DbOp();
}
rs=con.createStatement().executeQuery(sql);
}catch(SQLException e){
JOptionPane.showMessageDialog(null,"数据查询失败!");
rs=null;
}
return rs;
}
public static int executeUpdate(String sql){
int a=0;
try{
if(con==null){
new DbOp();
}
a= con.createStatement().executeUpdate(sql);
}catch(SQLException e){
JOptionPane.showMessageDialog(null,"数据库更新失败!");
a= -1;
}
return a;
}
public static void close(){
try{
if(con!=null){
con.close();
con=null;
//JOptionPane.showMessageDialog(null,"数据库已关闭!");
}
}catch(SQLException e){
JOptionPane.showMessageDialog(null,"数据库未打开!");
}
}
}