Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // connect to mysql
- public class SqlConnector {
- private static final String CONNECTION_URL = "jdbc:mysql://localhost:3306/";
- public SqlConnector() {
- }
- public Connection getConnection(
- final String databaseName,
- final String username,
- final String password
- ) {
- try {
- return DriverManager.getConnection(CONNECTION_URL + databaseName + "?autoReconnect=true&useSSL=false", username, password);
- } catch (SQLException ex) {
- System.out.println("DATABASE_CONNECTION_ERR");
- System.out.println("Message: " + ex.getLocalizedMessage());
- }
- return null;
- }
- }
- // repository class for managing database
- public class Repository {
- private final Connection conn;
- private PreparedStatement preparedStatement;
- private ResultSet resultSet;
- public Repository() {
- conn = new SqlConnector().getConnection("student_record", "root", "root");
- System.out.println("Connection Established!");
- createAuthTableIfNotExists();
- }
- public final void createAuthTableIfNotExists() {
- final String mAuthTable = "CREATE TABLE IF NOT EXISTS auth (uuid BINARY(16), username VARCHAR(50) PRIMARY KEY, password VARCHAR(100));";
- try {
- Statement statement = conn.createStatement();
- statement.executeUpdate(mAuthTable);
- } catch (SQLException e) {
- System.out.println("Error to create auth table. " + e.getLocalizedMessage());
- }
- }
- public boolean selectStudentRecord(String username, String password) {
- final String select = "SELECT BIN_TO_UUID(uuid) uuid FROM auth WHERE username = '"
- + username
- + "' AND password = '"
- + password
- + "';";
- try {
- preparedStatement = conn.prepareStatement(select);
- resultSet = preparedStatement.executeQuery();
- int i = 0;
- String uuid;
- while (resultSet.next()) {
- i++;
- uuid = resultSet.getString("uuid");
- System.out.println("UUID: " + uuid);
- }
- System.out.println("Result Set Count: " + i);
- preparedStatement.close();
- resultSet.close();
- if (i > 0) {
- return true;
- } else {
- return false;
- }
- } catch (SQLException ex) {
- Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- }
- }
- public boolean insertStudentRecord(String username, String password) {
- final String insert = "INSERT INTO auth (uuid,username,password) VALUES (UUID_TO_BIN(UUID()),'"
- + username
- + "','"
- + password
- + "');";
- try {
- preparedStatement = conn.prepareStatement(insert);
- preparedStatement.execute();
- preparedStatement.close();
- resultSet.close();
- return true;
- } catch (SQLException ex) {
- System.out.println("Failed to create new account. Error :" + ex.getLocalizedMessage());
- return false;
- }
- }
- public boolean deleteStudentRecord(String username) {
- final String delete = "DELETE FROM auth WHERE username = '"
- + username
- + "';";
- try {
- preparedStatement = conn.prepareStatement(delete);
- preparedStatement.execute();
- preparedStatement.close();
- resultSet.close();
- return true;
- } catch (SQLException ex) {
- System.out.println("Failed to create new account. Error :" + ex.getLocalizedMessage());
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement