[英]I cannot connect Java to MySQL
我必须连接到MySQL中的同一个数据库的类,我连接的第一个类是可以的,但连接到同一个表的第二个类显示如下错误消息
无法使用
executeQuery()
发出数据操作语句。
怎么了? 这是第一个没有错误的类代码:
if(evt.getKeyChar()==KeyEvent.VK_ENTER ){
try {
rs = stmt.executeQuery("select * from TbSignUp where username='"+txt1.getText()+"' and userpassword='"+txt2.getText()+"'");
if(rs.next()){
FrmMain fm = new FrmMain();
JOptionPane.showMessageDialog(null, "Welcome to "+txt1.getText()+"","Information Message",JOptionPane.INFORMATION_MESSAGE);
fm.setVisible(true);
this.setVisible(false);
}
else{
JOptionPane.showMessageDialog(null, "Login Failed","Error Meassage",JOptionPane.ERROR_MESSAGE);
txt1.setText("");
txt2.setText("");
txt1.requestFocus();
}
} catch (SQLException ex) {
Logger.getLogger(FrmLogin.class.getName()).log(Level.SEVERE, null, ex);
}
}
这是第二堂课:
if(evt.getKeyChar()==KeyEvent.VK_ENTER ){
if(txt6.getText().equals(txt7.getText())){
try {
stmt.executeQuery("Insert into TbSignUp(FirstName,LastName,Username,Userpassword) values('"+txt3.getText()+"','"+txt4.getText()+"','"+txt5.getText()+"','"+txt7.getText()+"')");
JOptionPane.showMessageDialog(null, "User Create Successfully", "Congratulation Meassage",JOptionPane.INFORMATION_MESSAGE);
FrmLogin fl = new FrmLogin();
fl.setVisible(true);
this.setVisible(false);
} catch (SQLException ex) {
Logger.getLogger(FrmSignUp.class.getName()).log(Level.SEVERE, null, ex);
}
}else{
JOptionPane.showMessageDialog(null, "Password Not Match", "Error Message",JOptionPane.ERROR_MESSAGE);
}
}
Can not issue data manipulation statements with executeQuery().
您只能在executeQuery()
执行select
语句来更新数据库,您应该使用executeUpdate()
stmt.executeUpdate("Insert into TbSignUp(FirstName,LastName,Username,Userpassword) values('"+txt3.getText()+"','"+txt4.getText()+"','"+txt5.getText()+"','"+txt7.getText()+"')");
你应该按照@EJP
建议使用PreparedStatement
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.