Netbeans MySQL连接 - 与jbdc无关

时间:2017-08-28 10:11:52

标签: java mysql javafx netbeans

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

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

连接方法:

dependencies

loginmodel:

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;
    }
}

我认为问题是来自dbConnection文件的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(); } } } } return null;回来了,系统正在退出。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

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

如果您以这种方式编码,您的应用程序将只有一个连接。更实用的解决方案将使用连接池。

了解Java coding standards。你的代码不遵循它们;这使得阅读和理解变得更加困难。

以下是一些建议:

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);
    }
}

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

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