gpt4 book ai didi

java - java中sql的字符串参数和SELECT准备语句

转载 作者:行者123 更新时间:2023-11-30 07:56:40 25 4
gpt4 key购买 nike

我有问题>>

编程语言:java数据库:Mysql数据库

我编写了一个java代码,用于根据方法中的数据参数从数据库中检索记录>>

代码是:

public static void Get_patient_data(String Hospital1_ID,String Hospital2_ID {       
try {
Connection con = getConnection2();
PreparedStatement statement = (com.mysql.jdbc.PreparedStatement)
con.prepareStatement(
"SELECT PatientGender "+
"FROM patientcorepopulatedtable "+
"WHERE PatientID = Hospital1_ID LIMIT 1" );
ResultSet result = statement.executeQuery();
ArrayList<String> array = new ArrayList<String>();
while( result.next()) {
System.out.print("the patient Gender is" +
result.getString("PatientGender"));
}
}
catch(Exception e) {
System.out.println("Error"+e);
}
}

如您所见,问题是 Hospital1_ID 参数 .. 来自该方法,而 patientID 是表 patientcorepopulatedtable 中的一列 ...

= 等于运算符不起作用。

最佳答案

试试这个

String query =
"SELECT PatientGender FROM patientcorepopulatedtable "+
" WHERE PatientID = ? LIMIT ?";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString (1, Hospital1_ID);
preparedStmt.setInt (2, 1);
preparedStmt.executeQuery();

关于java - java中sql的字符串参数和SELECT准备语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41918983/

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