📄 processthread.java
字号:
package com.jlib.server;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.*;
import java.sql.*;
import java.util.Calendar;
class BorrowBook {
public String ISBN;
public String name;
public String department;
public Date borrowDate;
public Date returnDate;
}
public class ProcessThread extends Thread{
private Socket cs;
public String sn;
public static BufferedReader bfc;
public ProcessThread(Socket cs){
this.cs = cs;
}
public void makedefault(){
try {
String sql = null;
File file = new File("library.sql");
FileInputStream in = new FileInputStream(file);
while(in.read()!=(-1)){
sql = sql + (char)in.read();
}
String userName = "sa"; // Your Database user id
String userPassword = "sa"; // Your Database password
String JDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=library";
Connection conn;
Statement stmt;
Class.forName(JDriver);
conn = DriverManager.getConnection(url,userName,userPassword);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public void Nsearch(){
try {
PrintStream ps = new PrintStream(cs.getOutputStream());
String ID = bfc.readLine();
String readerID = null;
String readerName = null;
String readerDep = null;
String readerPhone = null;
String readeraddress = null;
String readerother = null;
BorrowBook[] borrowBooks = new BorrowBook[10];
//连接数据库(不用类方法是为了避免代码重复)
String userName = "sa"; // Your Database user id
String userPassword = "sa"; // Your Database password
String JDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=library";
Connection conn;
Statement stmt;
Class.forName(JDriver);
conn = DriverManager.getConnection(url,userName,userPassword);
stmt = conn.createStatement();
//搜索信息
String sql = "select * from reader where ID = '" + ID + "'"; //查询读者名
String sql1 = "select * from borrow where readerID = " + "'" + ID + "'"; //查询借阅日期和还书日期
String sql2 = "select * from book where ISBN in(select bookISBN from borrow where readerID =" + "'" + ID + "'" + ")"; //查询书名
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
readerID = rs.getString("ID");
readerName = rs.getString("name");
readerDep = rs.getString("department");
readerPhone = rs.getString("mobilephone");
readeraddress = rs.getString("address");
readerother = rs.getString("other");
System.out.println("\n"+ readerID + " " + readerName + " " + readerDep + " " + readerPhone + " " + readeraddress + " " + readerother);
ps.println("NsearchOK");
ps.println(readerID);
ps.println(readerName);
ps.println(readerDep);
ps.println(readerPhone);
ps.println(readeraddress);
ps.println(readerother);
}
int i = 0;
ResultSet rs1 = stmt.executeQuery(sql1);
while(rs1.next()) {
borrowBooks[i] = new BorrowBook();
borrowBooks[i].borrowDate = rs1.getDate("borrowDate");
borrowBooks[i].returnDate = rs1.getDate("returnDate");
System.out.println("\n"+ borrowBooks[i].borrowDate.toString() + "\n" + borrowBooks[i].returnDate.toString());
i++;
}
ResultSet rs2 = stmt.executeQuery(sql2);
i = 0;
while(rs2.next()) {
borrowBooks[i].name = rs2.getString("name");
borrowBooks[i].ISBN = rs2.getString("ISBN");
System.out.println("\n"+ borrowBooks[i].name);
i++;
}
for(int j = 0;j<i;j++){
ps.println("start");
ps.println(borrowBooks[j].name);
ps.println(borrowBooks[j].ISBN);
ps.println(borrowBooks[j].borrowDate);
ps.println(borrowBooks[j].returnDate);
}
ps.println("over");
rs.close();
rs1.close();
rs2.close();
conn.close();
stmt.close();
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public void returnbook(){
try {
PrintStream ps = new PrintStream(cs.getOutputStream());
String ISBN;
String ID = bfc.readLine();
System.out.println(ID);
ISBN = bfc.readLine();
System.out.println(ISBN);
String userName = "sa"; // Your Database user id
String userPassword = "sa"; // Your Database password
String JDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=library";
Connection conn;
Statement stmt;
Class.forName(JDriver);
conn = DriverManager.getConnection(url,userName,userPassword);
stmt = conn.createStatement();
String sql1="delete from borrow where bookISBN='"+ISBN+"'";
String sql2="update book set state='可借' where ISBN='"+ISBN+"'";
conn.setAutoCommit(false);
stmt.executeUpdate(sql1);
stmt.executeUpdate(sql2);
conn.commit();
conn.close();
ps.println("returnbookOK");
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public void borrowbook(){
try {
PrintStream ps = new PrintStream(cs.getOutputStream());
String ID;
String ISBN;
Calendar cd = Calendar.getInstance();
int year;
int month;
int day;
ID = bfc.readLine();
ISBN = bfc.readLine();
year = cd.get(Calendar.YEAR);
month = cd.get(Calendar.WEEK_OF_MONTH);
day = cd.get(Calendar.DAY_OF_MONTH);
String borrowDate = Integer.toString(year) + "-" +
Integer.toString(month) + "-" +
Integer.toString(day);
cd.add(Calendar.DAY_OF_MONTH, 30);
year = cd.get(Calendar.YEAR);
month = cd.get(Calendar.WEEK_OF_MONTH);
day = cd.get(Calendar.DAY_OF_MONTH);
String returnDate = Integer.toString(year) + "-" +
Integer.toString(month) + "-" +
Integer.toString(day);
String sql = "insert into borrow values('" + ID + "'," + "'" + ISBN +
"'," + "'" + borrowDate + "'," + "'" + returnDate +
"')";
System.out.println(sql);
String sql1 = "update book set state='不可借' where ISBN='" + ISBN +
"'";
System.out.println(sql1);
//连接数据库(不用类方法是为了避免代码重复)
String userName = "sa"; // Your Database user id
String userPassword = "sa"; // Your Database password
String JDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=library";
Connection conn;
Statement stmt;
Class.forName(JDriver);
conn = DriverManager.getConnection(url,userName,userPassword);
conn.setAutoCommit(false);
stmt = conn.createStatement();
ResultSet tempst;
String str;
str = "select * from borrow where readerID='" + ID + "'";
tempst = stmt.executeQuery(str);
int i = 0;
while(tempst.next()){
i++;
}
if(i < 10){
str = "select * from book where state='不可借' and ISBN='" + ISBN + "'";
System.out.println(str);
tempst = stmt.executeQuery(str);
if(tempst.next()){
System.out.println("此书已被借");
ps.println("bookborrowlate");
}
stmt.executeUpdate(sql);
stmt.executeUpdate(sql1);
conn.commit();
System.out.println("借阅成功");
ps.println("borrowbookOK");
conn.close();
}else{
System.out.println("已经超过最大借阅数量");
ps.println("borrowbookpass");
System.out.println(i);
}
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
System.out.println("抱歉,借阅失败");
try {
PrintStream ps = new PrintStream(cs.getOutputStream());
ps.println("borrowbookfaild");
} catch (Exception e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
}
public void editreader(){
try {
PrintStream ps = new PrintStream(cs.getOutputStream());
String ID;
String name;
String department;
String password;
String role;
String cardclass;
String cardnumber;
String borrowdate;
String borrownumber;
String mobilephone;
String userclass;
String address;
String other;
String sex;
ID = bfc.readLine();
System.out.println(ID);
name = bfc.readLine();
System.out.println(name);
department = bfc.readLine();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -