gpt4 book ai didi

java - 将表连接到 jtextbox

转载 作者:行者123 更新时间:2023-12-01 13:47:17 24 4
gpt4 key购买 nike

我正在尝试连接 jtextbox 中的每个表字段,但是当我运行此代码时,它显示语法错误。我正在使用 sqlite 和 netbeans 我希望你能帮助我

 try{

String sql="Select * From account where id=txtId.getText() AND fname=txtFname.getText() AND lname=txtLname.getText() AND username=txtUsername.getText() AND password=txtPassword.getText()";

pst=conn.prepareStatement(sql);
pst.setString(1, txtId.getText());
pst.setString(2, txtFname.getText());
pst.setString(3, txtLname.getText());
pst.setString(4, txtUsername.getText());
pst.setString(5, txtPassword.getText());

pst.execute();
rs.close();
pst.close();

}
catch(SQLException e){
JOptionPane.showMessageDialog(null,e);
}
finally{
try{
rs.close();
pst.close();

}
catch(SQLException e){
JOptionPane.showMessageDialog(null,e);
}

}

最佳答案

尝试在查询中添加占位符(?)..

String sql="Select * From account where id=? AND fname=? AND lname=? AND username=? AND password=?";

您正在提供值,但查询中没有占位符:
pst.setString(1, txtId.getText());

编辑1:回复您的评论

try{
String sql="Select * From account where id=? AND fname=? AND lname=? AND username=? AND password=?";
pst=conn.prepareStatement(sql);
pst.setString(1, txtId.getText());
pst.setString(2, txtFname.getText());
pst.setString(3, txtLname.getText());
pst.setString(4, txtUsername.getText());
pst.setString(5, txtPassword.getText());

rs=pst.execute(); //to get resultset
while(rs.next())
{
//Do what you want to do if the records are found
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,e);
}
finally{
try{
if(rs!=null)rs.close();
if(pst!=null)pst.close();
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,e);
}
}

关于java - 将表连接到 jtextbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20267834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com