我正在使用 NetBeans IDE 连接 MySQL 数据库,我想使用选择查询从用户那里获取输入值并将该值放在 where 条件中。并且我收到此错误消息 Unknown column 'A' in 'where Clause' 当我在代码中写入值'A'时它可以工作,但是当它是来自用户的输入时它不起作用?
String dataSourceName = "testSqlDb";
String dbUrl = "jdbc:odbc:" + dataSourceName;
try {
bloodgroup = jTextField2.getText().trim();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbUrl, "", "");
Statement s = con.createStatement();
ResultSet rs;
String query = "SELECT COUNT(*) FROM blood_donation WHERE blood_group="
+ bloodgroup;
rs = s.executeQuery(query);
while (rs.next()) {
System.out.println("COUNT(*)=" + rs.getInt("COUNT(*)"));
}
s.close();
con.close();
} catch (Exception e) {
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null, e.getMessage(), "erro",
JOptionPane.ERROR_MESSAGE, null);
}