Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package textwars;
- import java.sql.*;
- /**
- *
- * @author cody
- */
- class AuthModel {
- protected Connection conn=null;
- public boolean register(String user, String pass) throws ClassNotFoundException {
- String query = "SELECT user FROM users WHERE user = " + user + " AND password = " + pass;
- String registerQuery = "INSERT INTO users (user, password) VALUES(" + user + ", " + pass +")";
- Boolean registered = false;
- String username = "root";
- String password = "j0nag0ld";
- String url = "jdbc:mysql://localhost/users";
- try {
- Class.forName("com.mysql.jdbc.Driver");
- this.conn = DriverManager.getConnection(url, username, password);
- }catch(SQLException e){
- System.out.println(e);
- }
- try {
- Statement stmt = this.conn.createStatement();
- ResultSet result = stmt.executeQuery(query);
- if (result.getFetchSize() > 1) {
- System.out.println("User Already Present. Please select different username");
- registered = false;
- }else{
- try {
- result = stmt.executeQuery(registerQuery);
- registered = true;
- }catch(SQLException e) {
- System.out.println(e.getCause());
- }
- }
- }catch(SQLException e) {
- e.getStackTrace();
- }
- return registered;
- }
- public boolean login(String user, String password) throws ClassNotFoundException {
- String query = "SELECT user FROM users WHERE `user` = '" + user + "' AND BINARY password = '" + password + "'";
- boolean loggedIn = false;
- String username = "root";
- String pass = "j0nag0ld";
- String url = "jdbc:mysql://localhost/users";
- Integer i = 0;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- this.conn = DriverManager.getConnection(url, username, pass);
- }catch(SQLException e){
- System.out.println(e);
- }
- try {
- Statement stmt = this.conn.createStatement();
- ResultSet result = stmt.executeQuery(query);
- while (result.next()) {
- i++;
- }
- if (i == 1) {
- loggedIn = true;
- }
- }catch(SQLException e) {
- e.printStackTrace();
- }
- return loggedIn;
- }
- }
Add Comment
Please, Sign In to add comment