【发布时间】:2017-03-01 16:18:03
【问题描述】:
我正在尝试允许来自我的 Java 项目的键盘输入以从我的数据库中搜索汽车牌照号码 (VARCHAR)。我在我的测试程序类中收到关于 SQL 语法错误的错误。正确的程序是什么,以便在我搜索许可证时它会显示该许可证。提前致谢
public Car getCar(String searchLicense) {
Car foundCar = new Car();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url + dbName, userName, password);
statement = conn.createStatement();
resultSet = statement.executeQuery(
"select * from eflow.registration.cLicense where="+searchLicense);
while (resultSet.next()) {
foundCar = new Car(resultSet.getInt("cID"), resultSet.getString("cLicense"),
resultSet.getInt("cJourneys"), resultSet.getString("cUsername"),
resultSet.getString("cPassword").toString());
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return foundCar;
}
【问题讨论】:
-
添加错误信息
-
您的 SQL 语法有误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“.cLicense where=”附近使用正确的语法
-
eflow.registration.cLicense应该是什么? -
简单看一下代码... cLicense 是一个字段,对吧?您从 TABLE 而不是字段中选择。您的 where 子句也不正确。将其更改为 " where cLicense = 'VALUE'
-
不知道架构...尝试 "select * from eflow.registration where cLicense = '" + searchLicense + "';"