Compare commits
3 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
e04f8aec8a | 2 years ago |
|
|
b70b5822ee | 2 years ago |
|
|
5150c6b944 | 2 years ago |
@ -0,0 +1,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,134 @@
|
||||
package database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.Date;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
|
||||
public class BorrowRecords {
|
||||
public BorrowRecords() {
|
||||
}
|
||||
//借阅图书
|
||||
public static void Borrow(String user, int bookid, String bookname) {
|
||||
Connection con = ConnectDatabase.connectDB();
|
||||
|
||||
PreparedStatement preSql;
|
||||
|
||||
String sqlStr = "insert into borrowrecords(user,bookid,bookname,borrowtime,returntime,status) values (?,?,?,now(),null,?)";
|
||||
|
||||
try {
|
||||
preSql = con.prepareStatement(sqlStr);
|
||||
preSql.setString(1, user);
|
||||
preSql.setInt(2, bookid);
|
||||
preSql.setString(3, bookname);
|
||||
preSql.setString(4, "未还");
|
||||
int ok = preSql.executeUpdate();
|
||||
con.close();
|
||||
ChangeBorrowState(bookid);
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
//借阅图书修改图书状态
|
||||
private static void ChangeBorrowState(int bookid) {
|
||||
Connection con = ConnectDatabase.connectDB();
|
||||
|
||||
PreparedStatement preSql;
|
||||
|
||||
String sqlStr = "update booktable set state=? where bookid = ?";
|
||||
|
||||
try {
|
||||
preSql = con.prepareStatement(sqlStr);
|
||||
preSql.setString(1, "外借");
|
||||
preSql.setInt(2, bookid);
|
||||
|
||||
int ok = preSql.executeUpdate();
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
|
||||
//还书
|
||||
public static void Return(String user, int bookid) {
|
||||
Connection con = ConnectDatabase.connectDB();
|
||||
|
||||
PreparedStatement preSql;
|
||||
String sqlStr = "select now()";
|
||||
ResultSet rs;
|
||||
Date date=null;
|
||||
try {
|
||||
preSql = con.prepareStatement(sqlStr);
|
||||
rs = preSql.executeQuery();
|
||||
while(rs.next()) {
|
||||
date = rs.getDate(1);
|
||||
}
|
||||
sqlStr = "update borrowrecords set returntime = ? where bookid = ? and user = ? and status = ?";
|
||||
preSql = con.prepareStatement(sqlStr);
|
||||
preSql.setDate(1, date);
|
||||
preSql.setInt(2, bookid);
|
||||
preSql.setString(3, user);
|
||||
preSql.setString(4, "未还");
|
||||
int ok = preSql.executeUpdate();
|
||||
|
||||
sqlStr = "update borrowrecords set status = ? where bookid = ? and user = ? and status = ?";
|
||||
preSql = con.prepareStatement(sqlStr);
|
||||
preSql.setString(1, "已还");
|
||||
preSql.setInt(2, bookid);
|
||||
preSql.setString(3, user);
|
||||
preSql.setString(4, "未还");
|
||||
ok = preSql.executeUpdate();
|
||||
|
||||
con.close();
|
||||
ChangeReturnState(bookid);
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
//还书图书修改图书状态
|
||||
private static void ChangeReturnState(int bookid) {
|
||||
Connection con = ConnectDatabase.connectDB();
|
||||
|
||||
PreparedStatement preSql;
|
||||
|
||||
String sqlStr = "update booktable set state=? where bookid = ?";
|
||||
|
||||
try {
|
||||
preSql = con.prepareStatement(sqlStr);
|
||||
preSql.setString(1, "在馆");
|
||||
preSql.setInt(2, bookid);
|
||||
|
||||
int ok = preSql.executeUpdate();
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
//比对还书
|
||||
public static boolean comparison(String user,int bookid) {
|
||||
Connection con = ConnectDatabase.connectDB();
|
||||
PreparedStatement preSql;
|
||||
ResultSet rs;
|
||||
String sqlStr = "select * from borrowrecords where bookid = ? and status = ?";
|
||||
try {
|
||||
preSql = con.prepareStatement(sqlStr);
|
||||
preSql.setInt(1, bookid);
|
||||
preSql.setString(2, "未还");
|
||||
rs = preSql.executeQuery();
|
||||
while (rs.next()) {
|
||||
String user2 = rs.getString(2);
|
||||
if(user2.equals(user)) {
|
||||
return true;
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
con.close();
|
||||
return false;
|
||||
} catch (SQLException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,103 @@
|
||||
package database;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
public class UpdateAdmin {
|
||||
//判断用户是否存在
|
||||
public static boolean sureuser(String user) {
|
||||
Connection con = ConnectDatabase.connectDB();
|
||||
|
||||
PreparedStatement preSql;
|
||||
|
||||
ResultSet rs;
|
||||
|
||||
String sqlStr = "select * from usertable where user = ?";
|
||||
try {
|
||||
preSql = con.prepareStatement(sqlStr);
|
||||
preSql.setString(1, user);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
//删除用户
|
||||
public static void deleteuser(String user) {
|
||||
Connection con = ConnectDatabase.connectDB();
|
||||
|
||||
PreparedStatement preSql;
|
||||
|
||||
|
||||
String sqlStr = "delete from usertable where user = ?";
|
||||
try {
|
||||
preSql = con.prepareStatement(sqlStr);
|
||||
preSql.setString(1, user);
|
||||
int ok = preSql.executeUpdate();
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
|
||||
//更改为用户
|
||||
public static void updateuser(String user) {
|
||||
Connection con = ConnectDatabase.connectDB();
|
||||
PreparedStatement preSql;
|
||||
|
||||
String sqlStr = "update usertable set admin = ? where user = ?";
|
||||
try {
|
||||
preSql = con.prepareStatement(sqlStr);
|
||||
preSql.setInt(1, 0);
|
||||
preSql.setString(2, user);
|
||||
int ok = preSql.executeUpdate();
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
|
||||
//添加为管理员
|
||||
public static void updateadmin(String user) {
|
||||
Connection con = ConnectDatabase.connectDB();
|
||||
PreparedStatement preSql;
|
||||
|
||||
String sqlStr = "update usertable set admin = ? where user = ?";
|
||||
try {
|
||||
preSql = con.prepareStatement(sqlStr);
|
||||
preSql.setInt(1, 1);
|
||||
preSql.setString(2, user);
|
||||
int ok = preSql.executeUpdate();
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
|
||||
//更改密码
|
||||
public static void updatepass(String user,String password) {
|
||||
Connection con = ConnectDatabase.connectDB();
|
||||
PreparedStatement preSql;
|
||||
|
||||
String sqlStr = "update usertable set password = ? where user = ?";
|
||||
try {
|
||||
preSql = con.prepareStatement(sqlStr);
|
||||
preSql.setString(1, password);
|
||||
preSql.setString(2, user);
|
||||
int ok = preSql.executeUpdate();
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue