请帮帮我:这是一个代码示例。 每次按下登录按钮,它都会在空结果集上给出java.sql.SQLExcpetion非法操作
登录按钮的代码:
private void loginBtnActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
con = DatabaseConnection.DBConnect();
String sql = "SELECT * FROM user_table WHERE user_name = ? AND user_pass = ?";
try
{
getPrivilege(username_Textfield.getText());
prepStat = con.prepareStatement(sql);
prepStat.setString(1, username_Textfield.getText());
prepStat.setString(2,password_Textfield.getText());
rs = prepStat.executeQuery();
if(rs.next())
{
if (isAdmin == true)
{
JOptionPane.showMessageDialog(null,"Login Successful");
mainDashboard mD = new mainDashboard();
mD.setVisible(true);
mD.setPrivilege(isAdmin);
dispose();
}else if(isAdmin == false)
{
JOptionPane.showMessageDialog(null,"Login Successful");
userDashboard uD= new userDashboard();
uD.setVisible(true);
uD.setPrivilege(isAdmin);
dispose();
}
}else
{
JOptionPane.showMessageDialog(null,"The username or password is incorrect","Access Denied", JOptionPane.ERROR_MESSAGE);
username_Textfield.setBackground(Color.red);
password_Textfield.setBackground(Color.red);
}
}catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}
连接类的代码: 这里的代码是用于数据库连接我有一个名为user的表,它包含user_id,user_name,user_pass,emp_id。所有这些都设置为非空
package guest_house;
//imports needed for the classs
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import javax.swing.JOptionPane;
/**
*
* @author POWER
*/
public class DatabaseConnection {
public static Connection DBConnect()
{
Connection con = null;
PreparedStatement preStat = null;
ResultSet rs = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/guest_house?","root","");
return con;
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
答案 0 :(得分:2)
您的专栏名称为user_name
,user_pass
,emp_id
,user
,并且表格的名称为userName
。但是你的sql语句使用userPass
,users
和"SELECT * FROM user WHERE user_name =? AND user_pass=?"
个单词。你必须改变这样的sql语句:
{{1}}