因此,我得到了一个Java应用程序,它通过JDBC连接到数据库。我现在要做的是从数据库中获取一个特定的条目,以匹配Java an中的输入。
public String getA(String tablename, String input){
String result = null;
try {
String query = ("select * from " +tablename+ " where name = "+input);
rs = stt.executeQuery(query);
while (rs.next()){
String rsh = rs.getString("Name");
result = rsh;
}
} catch (Exception acc_ex){
System.out.println("Acces excpetion in .getA: "+acc_ex);
return null;
} finally {
return result;
}
}投掷:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '[input-here]' in 'where clause'
其他方法中的查询工作非常好,以同样的方式构造它们。我有什么办法解决这个问题吗?谢谢
发布于 2015-10-09 20:26:59
这行代码input的字符串值("select * from " +tablename+ " where name = "+input);应该有单引号。
例如,如果您的表名是SomeTable,而输入值是SomeNameValue,那么发送给MySql的查询现在已经写好了.
select * from SomeTable where name = SomeNameValue这是错误的,应该是这样,单引号周围的值.
select * from SomeTable where name = 'SomeNameValue'所以我提到的代码行会改为.
String query = ("select * from " +tablename+ " where name = '" +input+ "'");发布于 2015-10-09 20:29:41
请在输入前后添加单引号:
String query = ("select * from " + tablename + " where name = '" + input + "'");https://stackoverflow.com/questions/33046536
复制相似问题