I’m using NetBeans IDE for connecting with MySQL database and I want to use select query that take input value from user and put that value in where condition .
and I get this error message
Unknown column ‘A’ in ‘where clause’
It works when I write the value ‘A’ in the code, but it doesn’t work when its an input from the user?
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);
}
I guess you are missing the ‘ ‘