我是Java Applet和Java Swing的新手。已经检查过类似的问题,但没有多大帮助。单击登录按钮后,抛出异常而不是执行try块。其他一切都很好。
public class signin extends javax.swing.JFrame {
Connection conn;
OracleResultSet rs = null;
OraclePreparedStatement pst;
private void cancelActionPerformed(java.awt.event.ActionEvent evt) {
signin s = new signin();
s.setVisible(true);
}
单击登录按钮时会抛出异常而不是转到新框架菜单。
private void loginActionPerformed(java.awt.event.ActionEvent evt) {
try{
String pass = passTF.getText().trim();
String user = userTF.getText().trim();
String sql = "select uname,pass from login where uname = '"+user+"' pass
= '"+pass+"'"; //here is the issue
pst = (OraclePreparedStatement) conn.prepareStatement(sql);
rs = (OracleResultSet) pst.executeQuery(sql);
System.out.println("Error");
int count = 0;
尝试块的其余部分
while (rs.next())
{
count++;
}
if(count == 1)
{
JOptionPane.showMessageDialog(null, "User Found");
System.out.println("Success");
menu m = new menu();
m.setVisible(true);
}
else
{
System.out.println("Success but no user");
JOptionPane.showMessageDialog(null, "Such user does not exist!");
}
}
catch(Exception ex)
{
System.out.println("Fail");
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new signin().setVisible(true);
}
});
}
答案 0 :(得分:0)
你有无效的select语句,这段代码:
"select uname,pass from login where uname = '"+user+"' pass= '"+pass+"'"
应该是这样的:
"select uname,pass from login where uname = '"+user+"' and pass= '"+pass+"'"
请注意,您在and
子句中缺少where
。
此外,您应该避免使用这些类型的查询,而是使用PreparedStatement
目前您的代码容易受到SQL注入攻击。