无法在Java中参数化SQl查询
我需要使用Fillo对象在Java中参数化SQL查询无法在Java中参数化SQl查询,java,sql,Java,Sql,我需要使用Fillo对象在Java中参数化SQL查询 Fillo fillo=new Fillo(); Connection connection=fillo.getConnection("C:\\Jeerdd\\Test.xlsx"); String strQuery="Select * from Sheet1"; Recordset recordset=connection.executeQuery(strQuery); String
Fillo fillo=new Fillo();
Connection connection=fillo.getConnection("C:\\Jeerdd\\Test.xlsx");
String strQuery="Select * from Sheet1";
Recordset recordset=connection.executeQuery(strQuery);
String y = "XYX"
while(recordset.next()){
String dd = recordset.getField("ID");
String strQuery1="Update Sheet1 Set Results = 'y' where ID="+dd;
//String strQuery1="Update Sheet1 Set Results="+y"and Time ="+xx" where ID="+dd;
System.out.println(strQuery1);
connection.executeUpdate(strQuery1);
}
我想参数化下面尝试的两个值结果&ID,但它们都不起作用
String strQuery1="Update Sheet1 Set Results = 'y' where ID="+dd;
OR
String strQuery1="Update Sheet1 Set Results = "+y+" where ID="+dd;
我不想使用事先准备好的声明。有人能帮我找出哪里做错了吗 据我所知,我甚至不知道第一次选择的意义。相反,只需对整个表执行直接更新:
Fillo fillo = new Fillo();
Connection connection = fillo.getConnection("C:\\Jeerdd\\Test.xlsx");
String sql = "UPDATE Sheet1 SET Results = 'y'";
connection.executeUpdate(sql);
我不认为你的方法是好的,在你的例子中有更多的sql注入的机会 您可以简单地使用JPA命名参数,如
String strQuery1="Update Sheet1 Set Results = :reults where ID= :id;
Query updateQuery = entityManager
.createNativeQuery(strQuery1);
updateQuery.setParameter("reults", y);
updateQuery.setParameter("id", dd);
updateQuery.executeUpdate();
Fillo中的代码对我有用 “更新图纸1集结果=”+字段值+”,其中ID=“+字段名称+”强> 下面是完整的代码
public static void statusUpdateTest(String field_Name, String field_Value) throws Throwable {
Fillo fillo = new Fillo();
Connection connection = null;
try {
String excelPath = "C:\\Book\\Test.xlsx";
connection = fillo.getConnection(excelPath);
String strQuery = "Update Sheet1 Set Results='"+field_Value+"' where ID='"+field_Name+"'";
connection.executeUpdate(strQuery);
connection.close();
} catch (Exception e) {
throw new Exception(ExceptionUtils.getFullStackTrace(e));
} finally {
Objects.requireNonNull(connection).close();
}
}
我不想使用事先准备好的声明——为什么不呢?还有,你得到了什么错误?加上你的查询很奇怪。基本上,您正在更新检索到的所有行,所以只需更新所有记录。我收到一个错误,因为无法解决“连接”中的“prepareStatement”方法是使用Fillo对象使用bcoz Iam的任何其他方法。Fillo到底是什么?使用堆栈跟踪编辑您的问题实际上,我所阅读的准备语句与Connection con=DriverManager.getConnection(“jdbc:p”)一起使用ostgresql://localhost/database“、”博士后“、”pswd”);但是我在这里使用的简单fillo是@Learner的链接,我也是
fillo
库的学习者;我以前从未使用过或见过它。由于您不需要准备好的语句,您可以直接在Fillo connection对象上调用executeUpdate()
。更新后的答案。@Tim,谢谢。我想参数化两件事1)结果和2)ID如何做请告诉我。