注:以下代码均贴的片段,并非完整代码。
1
check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id=5' at line 1check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id=5' at line 1
检查“where id=5”附近的代码:
//Test
Person p = new Person(5,"李莲英","男",20,"110");
//Function
String sql = "update users set name=?, sex=?, age=?, phone=?, where id=? ";
观察到在“where”前多了个“,”,去掉即可:
String sql = "update users set name=?, sex=?, age=?, phone=? where id=? ";
2
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 '?' at line 1
检查“?”附近的代码:
//Test
public void test7(){
Person p = dao.findByIdPerson(10);
System.out.println(p.toString());
}
//Function
String sql = "select * from users where id=? ";
//4.为占位符传值
pmst.setInt(1, id);
//5.执行查询语句
rs = pmst.executeQuery(sql);
修改为:
String sql = "select * from users where id= "+id;
//4.为占位符传值
// pmst.setInt(1, id);
//5.执行查询语句
rs = pmst.executeQuery(sql);
或者:
//Function
String sql = "select * from users where id=? ";
//4.为占位符传值
pmst.setInt(1, id);
//5.执行查询语句
rs = pmst.executeQuery();
错误原因:若用占位符方式传值,executeQuery();要无参。
添加字符串可有参,也可无参,最好是所有的都写成无参。