0

I have a login app that needs to connect to a server to check the username and password. I am using netbeans and the jbdc is installed and working in the services tab(thanks stack overflow!). By the jbdc is work I mean that i can execute SQL script through it.

I have set this up with MS Server 16 and MySQL so I am convied it is the code:

Connection method:

package dbUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class dbConnection {
    private static final String USERNAME = "root";
    private static final String PASSWORD = "mess";
    private static final String SQCONN = "jdbc:mysql://localhost:1434/MessyLogin?zeroDateTimeBehavior=convertToNull";


    public static Connection getConnection()throws SQLException{

        try {
            Class.forName("com.mysql.jdbc.Driver");
            return DriverManager.getConnection(SQCONN, USERNAME, PASSWORD);
        }catch (ClassNotFoundException e) {
        }
        return null;
    }
}

loginmodel:

package LogIn;


import dbUtil.dbConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;



public class LogInModel {
    Connection connection;

    public LogInModel() {
        try{

            this.connection = dbConnection.getConnection();

        }catch(SQLException e){

        }

        if(this.connection == null){
            System.out.println("here");
            // System.exit(1);
        }
    }
    public boolean isDatabaseConnected(){
        return this.connection != null;
    }

    public boolean isLogin(String username, String password) throws Exception{
        PreparedStatement pr = null;
        ResultSet rs = null;

        String sql = "SELECT * FROM MessyLogin where username = ? and Password = ?";

        try{
            pr = this.connection.prepareStatement(sql);
            pr.setString(1, username);
            pr.setString(2, password);

            rs = pr.executeQuery();

            boolean bool1;

            if(rs.next()){
                return true;
            }
            return false;
        }
        catch(SQLException ex){        
            return false;
        }

        finally {
            {
                pr.close();
                rs.close();
            }
        }
    }  

}

I believe the issue is the return null; from the dbConnection file. The if(this.connection==Null) comes back true and the system is exiting.

Thank you in advance.

  • 3
    You have no idea what happens when you write empty catch blocks. Print the stack trace. – duffymo Aug 28 '17 at 10:13
  • Maybe you should instantiate (with new) the class "dbConnection" in "LogInModel" before using the method "getConnection()". – ziMtyth Aug 28 '17 at 10:18
  • You can check with the debugger to see if "dbConnection" is actually different from null, so you can call its method. – ziMtyth Aug 28 '17 at 10:23
  • @duffymo netbeans told me to delete the stack trace. Will put it back in. – Shawn Taylor Aug 28 '17 at 11:43
  • That's why you should not use NetBeans. What a rubbish suggestion. – duffymo Aug 28 '17 at 11:59
  • I simply do not believe that NetBeans "told" you to use an empty catch block - that is clearly nonsense. Display the stack trace in the catch block, and [edit] your question to include the complete stack trace that is generated. Identify the line in your code that is throwing the exception. – James_D Aug 28 '17 at 13:20
  • @james_DRegarding the first portion of you comment: Throwable.PrintStackTrace() should be removed. When I click it, the error goes away and there is no error afterwards. Believe me I feel REALLY stupid for saying it in the beginning, but I am at home and just confirmed it. I have to go to bed I work a 12 hour shift in less than 6 hours. I will edit the question ASAP. Thank you for your response. http://imgur.com/a/jgpuo – Shawn Taylor Aug 28 '17 at 13:31
  • @ShawnTaylor Yeah, that's a really poor warning message (note it's not an error message). What I think they mean is that you should replace it with some more production-appropriate action (such as logging, at least). But you should not have an empty catch block. Put the `pirintStackTrace()` back in and post the stack trace in the question (or, better, [read it to figure out what is going wrong](https://stackoverflow.com/q/3988788/2775450)). – James_D Aug 28 '17 at 17:46

1 Answers1

1

Your dbConnection class is a bad idea. Why hard wire those values when you can pass them in?

Your application will only have one Connection if you code it this way. A more practical solution will use a connection pool.

Learn Java coding standards. Your code doesn't follow them; it makes it harder to read and understand.

Here's a couple of recommendations:

package dbUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class dbConnection {

    public static final String DRIVER = "com.mysql.jdbc.Driver";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "mess";
    public static final String URL = "jdbc:mysql://localhost:1434/MessyLogin?zeroDateTimeBehavior=convertToNull";


    public static Connection getConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        return DriverManager.getConnection(url, username, password);
    }
}

I might write that LogInModel this way:

package LogIn;


import dbUtil.dbConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LogInModel {

    private static final String sql = "SELECT * FROM MessyLogin where username = ? and Password = ?";

    private Connection connection;

    public LogInModel(Connection connection) {
        this.connection = connection;
    }

    public boolean isLogin(String username, String password) {
        boolean isValidUser = false;
        PreparedStatement pr = null;
        ResultSet rs = null;
        try {
            pr = this.connection.prepareStatement(sql);
            pr.setString(1, username);
            pr.setString(2, password);    
            rs = pr.executeQuery();
            while (rs.hasNext()) {
                isValidUser = true; 
            }
        } catch (SQLException ex) {  
            e.printStackTrace();      
            isValidUser = false;
        } finally {
              dbUtils.close(rs);
              dbUtils.close(pr);
        }
        return isValidUser;
    }      
}

Here's my guess as to why your code fails: You don't have the MySQL JDBC driver JAR in your runtime CLASSPATH. There's an exception thrown when it can't find the driver class, but you didn't know it because you swallowed the exception.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • For now, I am not concerned about the connection of this app. I need to learn and use connection pools(you are without a doubt correct). Seeing how this is something only I will ever use learning connection pools did not seem warranted at this point. Given this information, do you believe that I should chuck this and try a different way of getting a connection? – Shawn Taylor Aug 28 '17 at 13:00
  • You should figure out what's wrong with this code, but eventually you should use a pool. I don't know your requirements. Worry about understanding this code first. – duffymo Aug 28 '17 at 13:02
  • I really appreciate your input, especially the coding standards. This will be a valuable resource. – Shawn Taylor Aug 28 '17 at 13:12