我只是想知道这是否是所有事情的良好实施? 我已经实现了多线程系统,所以每个线程都有自己的连接。
这里是更多上下文的代码:
package dao;
import dao.jdbc.DatabaseConnection;
import dao.contracts.AdminContract;
import dao.dto.Account;
import dao.dto.AccountBuilder;
import dao.dto.Director;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
/**
*
* @author Milos
*/
public class AdminAccess implements AdminContract {
@Override
public boolean addAccount(Account account) {
try ( Connection con = DatabaseConnection.getConnection()) {
String addAccountQuery = "INSERT INTO account (username_email, password, account_type) VALUES(?, ?, ?);";
String addAdminQuery = "INSERT INTO admin (admin_id, first_name, last_name, email, phone_number, god_id) VALUES(?, ?, ?, ?, ?, ?);";
String addInstructorQuery = "INSERT INTO instructor (instructor_id, first_name, last_name, initials, birthdate, gender, phone_number, admin_id) VALUES(?, ?, ?, ?, ?, ?, ?, ?);";
String addStudentQuery = "INSERT INTO student (student_id, first_name, last_name, city, street, postal_code, birthdate, gender, instructor_id, admin_id) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
con.setAutoCommit(false);
try ( PreparedStatement addAccount = con.prepareStatement(addAccountQuery, Statement.RETURN_GENERATED_KEYS); PreparedStatement addAdmin = con.prepareStatement(addAdminQuery); PreparedStatement addInstructor = con.prepareStatement(addInstructorQuery);
PreparedStatement addStudent = con.prepareStatement(addStudentQuery)) {
switch (account.getAccountType()) {
case 'A' -> {
addAccount.setString(1, account.getUsernameEmail());
addAccount.setString(2, account.getPassword());
addAccount.setString(3, String.valueOf(account.getAccountType()));
addAccount.executeUpdate();
try ( ResultSet rs = addAccount.getGeneratedKeys()) {
rs.next();
addAdmin.setInt(1, rs.getInt(1));
addAdmin.setString(2, account.getFirstName());
addAdmin.setString(3, account.getLastName());
addAdmin.setString(4, account.getEmail());
addAdmin.setString(5, account.getPhoneNumber());
addAdmin.setInt(6, account.getGodId());
addAdmin.executeUpdate();
}
con.commit();
}
case 'I' -> {
addAccount.setString(1, account.getUsernameEmail());
addAccount.setString(2, account.getPassword());
addAccount.setString(3, String.valueOf(account.getAccountType()));
addAccount.executeUpdate();
try ( ResultSet rs = addAccount.getGeneratedKeys()) {
rs.next();
addInstructor.setInt(1, rs.getInt(1));
addInstructor.setString(2, account.getFirstName());
addInstructor.setString(3, account.getLastName());
addInstructor.setString(4, account.getInitials());
addInstructor.setString(5, account.getBirthDate());
addInstructor.setString(6, String.valueOf(account.getGender()));
addInstructor.setString(7, account.getPhoneNumber());
addInstructor.setInt(8, account.getAdminId());
addInstructor.executeUpdate();
}
con.commit();
}
case 'S' -> {
addAccount.setString(1, account.getUsernameEmail());
addAccount.setString(2, account.getPassword());
addAccount.setString(3, String.valueOf(account.getAccountType()));
addAccount.executeUpdate();
try ( ResultSet rs = addAccount.getGeneratedKeys()) {
rs.next();
addStudent.setInt(1, rs.getInt(1));
addStudent.setString(2, account.getFirstName());
addStudent.setString(3, account.getLastName());
addStudent.setString(4, account.getCity());
addStudent.setString(5, account.getStreet());
addStudent.setInt(6, account.getPostalCode());
addStudent.setString(7, account.getBirthDate());
addStudent.setString(8, String.valueOf(account.getGender()));
addStudent.setInt(9, account.getInstructorId());
addStudent.setInt(10, account.getAdminId());
addStudent.executeUpdate();
}
con.commit();
}
}
} catch (SQLException ex) {
System.out.println(ex.getLocalizedMessage());
System.out.println("Insert is being rolled back!");
con.rollback();
return false;
}
} catch (SQLException ex) {
System.out.println(ex.getLocalizedMessage());
return false;
}
return true;
}
}
package dao.jdbc;
import org.apache.commons.dbcp2.BasicDataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
*
* @author Milos
*/
public class DatabaseConnection {
private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/mydatabase";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
private static final BasicDataSource DATA_SOURCE;
static {
DATA_SOURCE = new BasicDataSource();
DATA_SOURCE.setDriverClassName(DRIVER);
DATA_SOURCE.setUrl(URL);
DATA_SOURCE.setUsername(USERNAME);
DATA_SOURCE.setPassword(PASSWORD);
}
private DatabaseConnection() {
}
public static Connection getConnection() throws SQLException {
return DATA_SOURCE.getConnection();
}
}
一切正常,我测试了几次,但代码似乎有点乱。 有没有办法改进它?