一、JDBCUtil
package Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtil {
private static final String JDBC_DRIVER_8UP = "com.mysql.cj.jdbc.Driver";
private static final String DB_URL_8UP = "jdbc:mysql://localhost:3306/mandarin?useSSL=false&serverTimezone=UTC&&allowPublicKeyRetrieval=true";
private static final String user = "root";
private static final String password = "1234";
private static Connection con = null;
private static Statement stmt = null;
private static PreparedStatement pstmt = null;
private static ResultSet rs = null;
public static void init() {
try {
Class.forName(JDBC_DRIVER_8UP);
con = DriverManager.getConnection(DB_URL_8UP, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("[DB init] ClassNotFoundException");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("[DB init] SQLException");
}
}
public static void close() {
try {
if (con != null) {
con.close();
}
if (stmt != null) {
stmt.close();
}
if (pstmt != null) {
pstmt.close();
}
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
System.out.println("[DB close] SQLException");
e.printStackTrace();
}
}
public static int insDelUpd(String sql) {
int flag = 0;
try {
stmt = con.createStatement();
flag = stmt.executeUpdate(sql);
} catch (SQLException e) {
System.out.println("[DB insDelUpd] SQLException");
e.printStackTrace();
}
return flag;
}
public static ResultSet select(String sql) {
try {
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.out.println("[DB select] SQLException");
e.printStackTrace();
}
return rs;
}
public static int insDelUpd(String sql, Object[] param) {
int flag = 0;
try {
pstmt = con.prepareStatement(sql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
pstmt.setObject(i + 1, param[i]);
}
}
flag = pstmt.executeUpdate();
} catch (SQLException e) {
System.out.println("[DB insDelUpd] SQLException");
e.printStackTrace();
}
return flag;
}
public static ResultSet select(String sql, Object[] param) {
try {
pstmt = con.prepareStatement(sql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
pstmt.setObject(i + 1, param[i]);
}
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
System.out.println("[DB select] SQLException");
e.printStackTrace();
}
return rs;
}
}
二、User
package Model;
public class User {
private int userId;
private String password;
private String userName;
private int sex;
private int age;
private String email;
private String phone;
private String type;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
三、LibrarianDAO
package DAO;
import Model.User;
public interface LibrarianDAO {
public String registerReader(String userName, int sex, int age, String password, String email, String phone);
public User getReader(int userId);
public boolean editReader(User reader);
public boolean deleteReader(int userId);
}
四、LibrarianDAOImpl
package DAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import Model.User;
import Utils.JDBCUtil;
import Utils.StringUtil;
public class LibrarianDAOImpl implements LibrarianDAO {
public String registerReader(String userName, int sex, int age, String password, String email, String phone) {
String userId = null;
if (StringUtil.isEmpty(password)) {
password = "12345678";
}
String sql_insert = "INSERT INTO `mandarin`.`user` (`userName`, `sex`,`age`,`password`,`email`,`phone`,`type`) VALUES (?,?,?,?,?,?,?);";
Object[] param_insert = new Object[7];
param_insert[0] = userName;
param_insert[1] = sex;
param_insert[2] = age;
param_insert[3] = password;
param_insert[4] = email;
param_insert[5] = phone;
param_insert[6] = "reader";
int flag = JDBCUtil.insDelUpd(sql_insert, param_insert);
if (flag > 0) {
String sql_select = "SELECT * FROM mandarin.user;";
ResultSet rs = JDBCUtil.select(sql_select);
try {
while (rs.next()) {
userId = "" + rs.getInt("userId");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return userId;
}
public User getReader(int userId) {
User reader = null;
String sql = "select * from user where userid = ?";
Object[] param = new Object[1];
param[0] = userId;
ResultSet rs = JDBCUtil.select(sql, param);
try {
if (rs.next()) {
reader = new User();
reader.setUserId(rs.getInt("userId"));
reader.setPassword(rs.getString("password"));
reader.setUserName(rs.getString("userName"));
reader.setAge(rs.getInt("age"));
reader.setSex(rs.getInt("sex"));
reader.setEmail(rs.getString("email"));
reader.setPhone(rs.getString("phone"));
reader.setType("reader");
}
} catch (SQLException e) {
e.printStackTrace();
}
return reader;
}
public boolean editReader(User reader) {
String sql = "UPDATE `mandarin`.`user` SET `password` = ?, `userName` = ?, `age`,`sex`,`email`,`phone` = ? WHERE (`userId` = ? and `type` = ?);";
Object[] param = new Object[8];
param[0] = reader.getPassword();
param[1] = reader.getUserName();
param[2] = reader.getAge();
param[3] = reader.getSex();
param[4] = reader.getEmail();
param[5] = reader.getPhone();
param[6] = reader.getUserId();
param[7] = reader.getType();
int flag = JDBCUtil.insDelUpd(sql, param);
if (flag > 0) {
return true;
} else {
return false;
}
}
public boolean deleteReader(int userId) {
String sql = "DELETE from `mandarin`.`user` where userId = ?;";
Object[] param = new Object[1];
param[0] = userId;
int flag = JDBCUtil.insDelUpd(sql, param);
if (flag > 0) {
return true;
} else {
return false;
}
}
}