【发布时间】:2017-05-21 17:45:29
【问题描述】:
我试图弄清楚为什么这段代码会引发 SQL 异常。当我运行此代码时,它会打印“Bad SQL in customer insert ps”,这是该内部 catch 块中的消息。我在这个类和我的应用程序的其他地方都有多个带有 SQL 插入的准备好的语句。他们都工作正常。我一遍又一遍地查看这个,我无法弄清楚为什么这个抛出异常。
try {
Connection conn = DBconnection.getConnection();
PreparedStatement ps = conn.prepareStatement("SELECT customerId FROM customer WHERE customerName=\"" + name + "\";");
System.out.println(ps.toString());
ResultSet rs = ps.executeQuery();
if (rs.next()) {
customerId = rs.getString("customerId");
}
try {
PreparedStatement customerInsert = DBconnection.getConnection().prepareStatement("INSERT "
+ "INTO customer (customerName, addressId, active, createDate, createdBy, lastUpdate, lastUpdateBy)"
+ "VALUES(\"" + name + "\", " + addressId + ", " + active + ", UTC_TIMESTAMP(), \"" + LogInController.getUserName() + "\", UTC_TIMESTAMP(), \"" + LogInController.getUserName() + "\");");
customerInsert.executeUpdate();
System.out.println(customerInsert.toString());
System.out.println(rs.toString());
} catch (SQLException sq) {
System.out.println("Bad SQL in customer insert ps");
}
} catch (SQLException customerIdException) {
System.out.println("Bad SQL in customer ps");
}
【问题讨论】:
标签: java sql prepared-statement