Netbeans MySQL连接 - 与jbdc无关

时间:2021-10-05 17:03:46

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.

我有一个登录应用程序,需要连接到服务器以检查用户名和密码。我正在使用netbeans并安装了jbdc并在服务选项卡中工作(感谢堆栈溢出!)。通过jbdc工作我的意思是我可以通过它执行SQL脚本。

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

我已经使用MS Server 16和MySQL进行了设置,所以我确信这是代码:

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:

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.

我相信问题是返回null;来自dbConnection文件。 if(this.connection == Null)返回true并且系统正在退出。

Thank you in advance.

先谢谢你。

1 个解决方案

#1


1  

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

您的dbConnection类是个坏主意。为什么在传递它们时硬连接这些值?

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.

学习Java编码标准。你的代码不遵循它们;这使得阅读和理解变得更加困难。

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:

我可能会这样写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 {

    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.

这是我猜测你的代码失败的原因:你的运行时CLASSPATH中没有MySQL JDBC驱动程序JAR。当它找不到驱动程序类时会抛出异常,但是你不知道它因为你吞下了异常。

#1


1  

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

您的dbConnection类是个坏主意。为什么在传递它们时硬连接这些值?

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.

学习Java编码标准。你的代码不遵循它们;这使得阅读和理解变得更加困难。

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:

我可能会这样写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 {

    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.

这是我猜测你的代码失败的原因:你的运行时CLASSPATH中没有MySQL JDBC驱动程序JAR。当它找不到驱动程序类时会抛出异常,但是你不知道它因为你吞下了异常。