我在Java代碼中的SQL代碼,看起來像這樣:錯誤1064:SQL語法錯誤
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
beforeExerTestDTO dto = new beforeExerTestDTO();
StringBuffer sql = new StringBuffer();
sql.append(" select * ");
sql.append(" from n_before_exer ");
sql.append(" where id=?");
sql.append(" and reg_date = (select max(reg_date) from n_before_exer where id=?)");
try {
con = pool.getLocalConnection();
pstmt = con.prepareStatement(sql.toString());
pstmt.setString(1, id);
pstmt.setString(2, id);
System.out.println("여기까진 살까??");
rs = pstmt.executeQuery();
/......
...... some code/
}catch(SQLException e){
System.out.println("read : " + e);
System.out.println("read : " + sql);
}catch(Exception e){
System.out.println("read : " + e.getStackTrace().toString());
}finally{
DBClose.close(con, pstmt, rs);
}
return dto;
}
當文件被執行它形成了這樣的說法在控制檯:
select * from n_before_exer where id=? and reg_date = (select max(reg_date) from n_before_exer where id=?)
,並拋出一個
java.sql.SQLEXCEPTION
我試了一下:
- 我跑在MySQL Workbench中查詢相同:
,並得到了以下錯誤:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? and reg_date = (select max(reg_date) from n_before_exer where id=?)' at line 1
的研究課題顯示位:
- 這種方式是不是首選的方式,因爲它可能導致注入攻擊
- 並建議使用佔位符作爲參數
這似乎有點複雜,我,如果有人能幫助我建立這個說法在正確的首選方式,請
感謝
你更換在執行查詢之前使用參數? –
@DanteFañaBadia:在錯誤陳述中增加了額外的代碼。你能否看到它是否確認你在說什麼 –
我認爲是一個帶參數的問題,參數顯然沒有設置正確嘗試放在'周圍? ,只是爲了調試建議。 –