Please help me: here is a sample of the code. Everytime i press the login button it gives me java.sql.SQLExcpetion illegal operation on empty result set
Code for the Login Button:
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);
}
}
code for the connection class: the code here is for the DB connection i have a table called user which consists of user_id, user_name,user_pass, emp_id. all of which are set to not null
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;
}
}
}
Your column's name are user_id
, user_name
, user_pass
, emp_id
and table's name is user
. But your sql sentence use userName
, userPass
and users
words. You must change the sql sentence like this:
"SELECT * FROM user WHERE user_name =? AND user_pass=?"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.