gpt4 book ai didi

java - 带变量的结果集

转载 作者:太空宇宙 更新时间:2023-11-03 12:08:22 24 4
gpt4 key购买 nike

好的,基本上我有这段代码:

resultSet = statement.executeQuery("select * from FEEDBACK.COMMENTS");
writeResultSet(resultSet);

private void writeResultSet(ResultSet resultSet) throws SQLException {
System.out.println("jestem w writeresultset");
// resultSet is initialised before the first data set
while (resultSet.next()) {
// it is possible to get the columns via name
// also possible to get the columns via the column number
// which starts at 1
// e.g., resultSet.getSTring(2);
String id = resultSet.getString("id");
String user = resultSet.getString("IMIE");
String website = resultSet.getString("NAZWISKO");
String summary = resultSet.getString("ADRES");
String date = resultSet.getString("EMAIL");
String comment = resultSet.getString("TELEFON");
String opisso = resultSet.getString("OPIS");

JTextField myOutput = new JTextField(1600);
myOutput.setText("id w bazie danych to " + id + " imie to " + user
+ " nazwisko to " + website + " adres to " + summary + " email to "
+ date + " teelefon to " + comment + " opis to " + opisso);

add(myOutput);
}
}

我想实现的是:

resultSet = statement.executeQuery("select * from FEEDBACK.COMMENTS 
where NAZWISKO LIKE " variable );
writeResultSet(resultSet);

我想按已定义的变量进行搜索,但是我卡住了,不知道该怎么做。

最佳答案

使用PreparedStatement:

String nazwisko = ...
String query = "select * from FEEDBACK.COMMENTS where NAZWISKO LIKE ?";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, nazwisko);
ResultSet rs = pstmt.execute();
while (resultSet.next()) {
//...
}

如果您需要为您的LIKE 使用通配符,请选择以下之一:

nazwisko = nazwisko + "%";
nazwisko = "%" + nazwisko;
nazwisko = "%" + nazwisko + "%";

关于java - 带变量的结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25393036/

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