Guest User

DB

a guest
Jun 20th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. package V6;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import javax.swing.JOptionPane;
  10.  
  11. public class DBHandler {
  12.  
  13.     public String driver = "com.mysql.jdbc.Driver";
  14.     public String url = "jdbc:mysql://localhost/Blackjack";
  15.     public String user = "root";
  16.     public String pass = "";
  17.    
  18.     public Connection getConnection() throws ClassNotFoundException, SQLException{
  19.        
  20.         Class.forName(driver);
  21.         return DriverManager.getConnection(url, user, pass);
  22.     }
  23.    
  24.     public boolean checkAccount(String username, String password) throws ClassNotFoundException, SQLException{
  25.        
  26.         Connection con = getConnection();
  27.         Statement st = con.createStatement();
  28.         String sql = "SELECT * FROM Accounts WHERE username='" + username + "' AND password='" + password + "';";
  29.         ResultSet rt = st.executeQuery(sql);
  30.         boolean toReturn = false;
  31.         if (rt.next()) {
  32.             toReturn = true;
  33.             Account acc = new Account();
  34.             acc.id = rt.getInt("id");
  35.             acc.username = rt.getString("username");
  36.             acc.balance = rt.getInt("balance");
  37.             acc.win = rt.getInt("wins");
  38.             acc.loss = rt.getInt("losses");
  39.             acc.tie = rt.getInt("ties");
  40.             Main.loggedUser = acc;
  41.         }
  42.         con.close();
  43.         st.close();
  44.         rt.close();
  45.         return toReturn;
  46.     }
  47.    
  48.     public boolean insertAccount(String username, String password) throws ClassNotFoundException, SQLException{
  49.        
  50.         Connection con = getConnection();
  51.         String check = "SELECT username FROM Accounts WHERE username='" + username + "';";
  52.         ResultSet rt = con.createStatement().executeQuery(check);
  53.         if (rt.next()) {
  54.             JOptionPane.showMessageDialog(null,"An account with that username already exists.");
  55.             rt.close();
  56.             return false;
  57.         }
  58.         String sql = "INSERT INTO Accounts (username, password, balance, wins, losses, ties) VALUES ('" + username + "', '" + password + "', 5000, 0, 0, 0);";
  59.         PreparedStatement ps = con.prepareStatement(sql);
  60.         ps.executeUpdate();
  61.         con.close();
  62.         ps.close();
  63.         rt.close();
  64.         return true;
  65.     }
  66.    
  67.     public void updateAccount(Account acc) throws ClassNotFoundException, SQLException{
  68.        
  69.         Connection con = getConnection();
  70.         String sql = "UPDATE Accounts SET balance=" + acc.balance + ",wins=" + acc.win + ",losses=" + acc.loss + ",ties=" + acc.tie + " WHERE id=" + acc.id + ";";
  71.         PreparedStatement ps = con.prepareStatement(sql);
  72.         ps.executeUpdate();
  73.         con.close();
  74.         ps.close();
  75.     }
  76. }
Add Comment
Please, Sign In to add comment