Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package V6;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.JOptionPane;
- public class DBHandler {
- public String driver = "com.mysql.jdbc.Driver";
- public String url = "jdbc:mysql://localhost/Blackjack";
- public String user = "root";
- public String pass = "";
- public Connection getConnection() throws ClassNotFoundException, SQLException{
- Class.forName(driver);
- return DriverManager.getConnection(url, user, pass);
- }
- public boolean checkAccount(String username, String password) throws ClassNotFoundException, SQLException{
- Connection con = getConnection();
- Statement st = con.createStatement();
- String sql = "SELECT * FROM Accounts WHERE username='" + username + "' AND password='" + password + "';";
- ResultSet rt = st.executeQuery(sql);
- boolean toReturn = false;
- if (rt.next()) {
- toReturn = true;
- Account acc = new Account();
- acc.id = rt.getInt("id");
- acc.username = rt.getString("username");
- acc.balance = rt.getInt("balance");
- acc.win = rt.getInt("wins");
- acc.loss = rt.getInt("losses");
- acc.tie = rt.getInt("ties");
- Main.loggedUser = acc;
- }
- con.close();
- st.close();
- rt.close();
- return toReturn;
- }
- public boolean insertAccount(String username, String password) throws ClassNotFoundException, SQLException{
- Connection con = getConnection();
- String check = "SELECT username FROM Accounts WHERE username='" + username + "';";
- ResultSet rt = con.createStatement().executeQuery(check);
- if (rt.next()) {
- JOptionPane.showMessageDialog(null,"An account with that username already exists.");
- rt.close();
- return false;
- }
- String sql = "INSERT INTO Accounts (username, password, balance, wins, losses, ties) VALUES ('" + username + "', '" + password + "', 5000, 0, 0, 0);";
- PreparedStatement ps = con.prepareStatement(sql);
- ps.executeUpdate();
- con.close();
- ps.close();
- rt.close();
- return true;
- }
- public void updateAccount(Account acc) throws ClassNotFoundException, SQLException{
- Connection con = getConnection();
- String sql = "UPDATE Accounts SET balance=" + acc.balance + ",wins=" + acc.win + ",losses=" + acc.loss + ",ties=" + acc.tie + " WHERE id=" + acc.id + ";";
- PreparedStatement ps = con.prepareStatement(sql);
- ps.executeUpdate();
- con.close();
- ps.close();
- }
- }
Add Comment
Please, Sign In to add comment