我想从数据库中搜索和显示数据(在表单中输入的数据)。问题是我总是得到数据库中的最后一个值。 这是我的代码:
public void actionPerformed(ActionEvent ae) {
String nom, prenom;
nom = tf3.getText();
prenom = tf4.getText();
String url = "jdbc:mysql://localhost:3306/seminaire";
String userid = "root";
String password = "";
try {
if (nom != "" && prenom != "") {
Connection connection = DriverManager.getConnection(url,
userid, password);
Statement st = connection.createStatement();
ResultSet rs= st.executeQuery("SELECT * FROM participant WHERE nom=nom && prenom=prenom");
while (rs.next())
{
String nm = rs.getString("nom");
String prnm = rs.getString("prenom");
String cat = rs.getString("categorie");
String afl = rs.getString("affiliation");
String mnt = rs.getString("montant");
}
}
}
catch (FileNotFoundException | DocumentException e) {
e.printStackTrace();
}
}
请你知道吗? 谢谢
答案 0 :(得分:0)
看起来你分配给nom和prenom的值不会在你执行的查询中使用。另外,&&
应该AND
吗?
我建议您使用预准备语句,请参阅Java documentation on prepared statements.
我会更改您的代码以执行以下操作:
PreparedStatement selectStatement = connection.prepareStatement("SELECT * FROM participant WHERE nom=? AND prenom=?");
selectStatement.setString(1, nom);
selectStatement.setString(2, prenom);
ResultSet rs = selectStatement.executeQuery();