【发布时间】:2017-08-28 10:11:51
【问题描述】:
我有一个登录应用程序,它需要连接到服务器以检查用户名和密码。我正在使用 netbeans 并且 jbdc 已安装并在服务选项卡中工作(感谢堆栈溢出!)。 jbdc 是工作,我的意思是我可以通过它执行 SQL 脚本。
我已经使用 MS Server 16 和 MySQL 进行了设置,所以我确信它是代码:
连接方式:
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;
}
}
登录模型:
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();
}
}
}
}
我认为问题在于 dbConnection 文件中的 return null;。 if(this.connection==Null) 返回 true,系统正在退出。
提前谢谢你。
【问题讨论】:
-
你不知道写空 catch 块时会发生什么。打印堆栈跟踪。
-
也许你应该在使用方法“getConnection()”之前在“LogInModel”中实例化(使用新的)类“dbConnection”。
-
你可以用调试器检查一下“dbConnection”是否真的与null不同,所以你可以调用它的方法。
-
@duffymo netbeans 告诉我删除堆栈跟踪。会把它放回去。
-
这就是您不应该使用 NetBeans 的原因。多么垃圾的建议。
标签: java mysql javafx netbeans