Guest User

Untitled

a guest
Nov 27th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. package textwars;
  2.  
  3. import java.sql.*;
  4.  
  5. /**
  6. *
  7. * @author cody
  8. */
  9. class AuthModel {
  10. protected Connection conn=null;
  11.  
  12. public boolean register(String user, String pass) throws ClassNotFoundException {
  13. String query = "SELECT user FROM users WHERE user = " + user + " AND password = " + pass;
  14. String registerQuery = "INSERT INTO users (user, password) VALUES(" + user + ", " + pass +")";
  15. Boolean registered = false;
  16. String username = "root";
  17. String password = "j0nag0ld";
  18. String url = "jdbc:mysql://localhost/users";
  19.  
  20. try {
  21. Class.forName("com.mysql.jdbc.Driver");
  22. this.conn = DriverManager.getConnection(url, username, password);
  23. }catch(SQLException e){
  24. System.out.println(e);
  25. }
  26.  
  27. try {
  28. Statement stmt = this.conn.createStatement();
  29. ResultSet result = stmt.executeQuery(query);
  30.  
  31. if (result.getFetchSize() > 1) {
  32. System.out.println("User Already Present. Please select different username");
  33. registered = false;
  34. }else{
  35. try {
  36. result = stmt.executeQuery(registerQuery);
  37. registered = true;
  38. }catch(SQLException e) {
  39. System.out.println(e.getCause());
  40. }
  41. }
  42. }catch(SQLException e) {
  43. e.getStackTrace();
  44. }
  45. return registered;
  46. }
  47.  
  48. public boolean login(String user, String password) throws ClassNotFoundException {
  49. String query = "SELECT user FROM users WHERE `user` = '" + user + "' AND BINARY password = '" + password + "'";
  50. boolean loggedIn = false;
  51. String username = "root";
  52. String pass = "j0nag0ld";
  53. String url = "jdbc:mysql://localhost/users";
  54. Integer i = 0;
  55.  
  56. try {
  57. Class.forName("com.mysql.jdbc.Driver");
  58. this.conn = DriverManager.getConnection(url, username, pass);
  59. }catch(SQLException e){
  60. System.out.println(e);
  61. }
  62. try {
  63. Statement stmt = this.conn.createStatement();
  64. ResultSet result = stmt.executeQuery(query);
  65.  
  66. while (result.next()) {
  67. i++;
  68. }
  69. if (i == 1) {
  70. loggedIn = true;
  71. }
  72. }catch(SQLException e) {
  73. e.printStackTrace();
  74. }
  75. return loggedIn;
  76. }
  77. }
Add Comment
Please, Sign In to add comment