forked from kongxin-github/Java_Library_Management_System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdduser.java
More file actions
67 lines (54 loc) · 1.39 KB
/
Adduser.java
File metadata and controls
67 lines (54 loc) · 1.39 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
package database;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class Adduser {
public static Boolean adduser(String user, String studentid, String name, String password) {
Connection con = ConnectDatabase.connectDB();
PreparedStatement preSql;
String sqlStr=null;
if(userlist()) {
sqlStr = "insert into usertable values (?,?,?,?,0)";
}else {
sqlStr = "insert into usertable values (?,?,?,?,1)";
}
try {
preSql = con.prepareStatement(sqlStr);
preSql.setString(1, user);
preSql.setString(2, studentid);
preSql.setString(3, name);
preSql.setString(4, password);
int ok = preSql.executeUpdate();
con.close();
return true;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "用户名已存在", "警告", JOptionPane.WARNING_MESSAGE);
return false;
}
}
//判断用户是否存在
public static boolean userlist() {
Connection con = ConnectDatabase.connectDB();
PreparedStatement preSql;
ResultSet rs;
String sqlStr = "select * from usertable";
try {
preSql = con.prepareStatement(sqlStr);
rs = preSql.executeQuery();
boolean flag = false;
while(rs.next()) {
flag = true;
return true;
}
if(!flag) {
return false;
}
con.close();
return false;
} catch (SQLException e) {
return false;
}
}
}