I want to use testContainers because, from what I understood, it is a container of the database at some point in time ( we can pull that database state with Docker, so no mocking and stuff). Then we can just test it ( insert some stuff, assert that it is in the db, delete it, assert that it is gone, etc.. ). My problem is that I can't pull the image of the database with Docker. Moreover, the database is remote ( hosted by my university ). It has username and password. If someone can help me with getting the image and injecting it into MySQLContainer (https://www.testcontainers.org/modules/databases/), I would be really grateful. Here is my code:
public class SQLConnection {
protected transient Connection connection;
protected transient PreparedStatement preparedStatement;
protected transient ResultSet resultSet;
protected transient Statement statement;
protected transient String url = "jdbc:mysql://remote.host.nl:3306/project_Name?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
public SQLConnection() throws SQLException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Properties connectionProps = new Properties();
connectionProps.put("user", "remoteDbUsername");
connectionProps.put("password", "remoteDbPassword");
this.connection = DriverManager.getConnection(url, connectionProps);
} catch (Exception e) {
System.out.println(e.getMessage());
}
this.statement = connection.createStatement();
}
public void closeSQLConnection() throws SQLException {
this.connection.close();
}
public boolean DBRegister(User user) {
try {
String query = "INSERT INTO `user` (`username`,`password`,`email`, `high_score`) VALUES (?,?,?,?)";
this.preparedStatement = this.connection.prepareStatement(query);
this.preparedStatement.setString(1, user.getUsername());
this.preparedStatement.setString(2, this.hashPassword(user.getPassword()));
this.preparedStatement.setString(3, user.getEmail());
this.preparedStatement.setInt(4, user.getScore());
this.preparedStatement.executeUpdate();
System.out.println("Registration worked for user " + user.getUsername());
return true;
} catch (Exception e) {
System.out.println("Username already exists :(");
}
return false;
}
public boolean DBLogin(User user) throws Exception {
try {
System.out.println(user.getUsername());
String query = "SELECT * FROM `user` WHERE `username` = '"
+ user.getUsername() + "'";
this.preparedStatement = this.connection.prepareStatement(query);
this.resultSet = this.preparedStatement.executeQuery();
this.resultSet.next();
String storedPassHashed = this.resultSet.getString("password");
return this.checkPass(user.getPassword(), storedPassHashed);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("User does not exist");
}
return false;
}
private String hashPassword(String plainTextPass) {
return BCrypt.hashpw(plainTextPass, BCrypt.gensalt());
}
private boolean checkPass(String plainTextPassword, String hashedPassword) {
return BCrypt.checkpw(plainTextPassword, hashedPassword);
}
}
Aucun commentaire:
Enregistrer un commentaire