想用占位符实现页面对servlet传参,再对Mysql数据库进行查询,可是一直不知道哪错了
代码
Connection conn = DriverManager.getConnection(url,username,password);
String sql = "select * from tb_book where name=? or author=?";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,author);
ResultSet rs = ps.executeQuery(sql);
出错信息
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '? or author=?' at line 1
--------------------编程问答--------------------
sql语句有问题吧,你先不用占位符试下可以不
--------------------编程问答--------------------
不用占位符就可以了
但是
String sql = "update tb_book set bookcount=?where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, bookCount);
ps.setInt(2, id);
ps.executeUpdate();
是对的,可以执行出来,很郁闷的
--------------------编程问答--------------------
name author有这两个字段吗
--------------------编程问答--------------------
ResultSet rs = ps.executeQuery(sql);
改为
ResultSet rs = ps.executeQuery();
相信我,没错的!
--------------------编程问答--------------------
在数据库里有的
--------------------编程问答--------------------
恩恩,改了之后就正确了,但是为什么
ps.setString(1,name);
ps.setString(2,author);
的情况下,只能识别数字查询不能识别汉字的查询呢