我正在创建用于管理车库的简单Java程序。 在该程序中,您具有登录窗口,然后您可以注册新用户。 问题是当我想注册新用户时,在SELECT查询之后程序被卡住了(新用户不在数据库中时)。
我已确保所有连接均已关闭,并确保我使用的查询执行正确(SELECT的executeQuery和INSERT的updateQuery)。
注册类别:
class RegisterButtonListener implements ActionListener {
Connection c = null;
Statement stmt = null;
String username, password;
String selectUser="";
@Override
public void actionPerformed(ActionEvent arg0) {
if (garageView.getTxtNewPassword().getText().isEmpty() == false
&& garageView.getTxtNewUsername().getText().isEmpty() == false){
username = garageView.getTxtNewUsername().getText();
password = garageView.getTxtNewPassword().getText();
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:GarageDoctor.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully - SELECT");
stmt=c.createStatement();
String selectSql = "SELECT NAME FROM USERS WHERE NAME = '"+username+"';";
ResultSet rs = stmt.executeQuery(selectSql);
while(rs.next()) {
selectUser+=rs.getString("name");
System.out.println(selectUser);
if(selectUser.equals(username)) {
JOptionPane.showMessageDialog(garageView, "User Already Exists!");
System.out.println("closing");
selectUser="";
rs.close();
stmt.close();
c.close();
System.out.println("closed");
}
else {
System.out.println("user isn't in db, inserting now");
DBInsertNewUsers(username,password);
}
}
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
}else {
JOptionPane.showMessageDialog(garageView, "Invalid input");
}
}
}
哪个链接指向DBInsertNewUsers:
public void DBInsertNewUsers(String username,String password) {
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:GarageDoctor.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully - Insert");
stmt = c.createStatement();
System.out.println("stmt = c.createStatement();");
String sql = "INSERT INTO USERS (ID,PASSWORD,NAME) VALUES ("+i+",'"+password+"', '"+username+"');";
stmt.executeUpdate(sql);
System.out.println("stmt.executeUpdate(sql);");
i++;
stmt.close();
c.commit();
System.out.println("action done successfully - Insert");
c.close();
JOptionPane.showMessageDialog(garageView, "Register Success");
}
catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
}
尝试注册新用户时,应用程序卡住,最后一个控制台消息是:
Opened database successfully - SELECT
答案 0 :(得分:1)
您的调用DBInsertNewUsers的代码在while(rs.next())循环内。如果没有匹配的行,则rs.next()将返回false(因为没有行),并且控件将永远不会进入该循环。您还应该考虑使用“ try-with-resources”模式关闭资源并使用准备好的语句:
String query = "SELECT NAME FROM USERS WHERE NAME = ?";
try (Connection con = DriverManager.getConnection(myConnectionURL);
PreparedStatement ps = con.prepareStatement(query);) {
ps.setString(1,username);
try(ResultSet rs = stmt.executeQuery(selectSql);){
if(rs.next()){
selectUser+=rs.getString("name");
}else{
System.out.println("user isn't in db, inserting now");
DBInsertNewUsers(username,password);
}
}
}
答案 1 :(得分:0)
此声明:
SELECT NAME FROM USERS WHERE NAME = '"+username+"';
仅当存在具有该用户名的行时才会返回行。
同样,在这种情况下,rs.next()
就是true
,您进入的循环中有一个if
语句,其条件是:
selectUser.equals(username)
肯定是true
。
因此else
块将永远不会执行。
您必须做的是删除while循环,并仅保留一个if / else块:
@Override
public void actionPerformed(ActionEvent arg0) {
if (garageView.getTxtNewPassword().getText().isEmpty() == false
&& garageView.getTxtNewUsername().getText().isEmpty() == false) {
username = garageView.getTxtNewUsername().getText();
password = garageView.getTxtNewPassword().getText();
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:GarageDoctor.db");
c.setAutoCommit(false);
System.out.println("Opened database successfully - SELECT");
stmt = c.createStatement();
String selectSql = "SELECT NAME FROM USERS WHERE NAME = '" + username + "';";
ResultSet rs = stmt.executeQuery(selectSql);
boolean exists = rs.next();
System.out.println("closing");
rs.close();
stmt.close();
c.close();
System.out.println("closed");
if (exists) {
JOptionPane.showMessageDialog(garageView, "User Already Exists!");
selectUser = "";
} else {
System.out.println("user isn't in db, inserting now");
DBInsertNewUsers(username, password);
}
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
} else {
JOptionPane.showMessageDialog(garageView, "Invalid input");
}
}
还在DBInsertNewUsers()
内,您正在使用变量i
来设置列id
的值。
从您发布的代码中,找不到id
列是主键的初始化方式。
因此,如果要避免插入失败,则需要特别注意这一部分。
在DBInsertNewUsers()
中,将语句的顺序更改为:
c.commit();
stmt.close();