引号问题、Parameter index out of range问题
引号问题
String sql = "INSERT INTO bs_bookstore(storeName) VALUES ("+"'?')";
为什么老师演示的过程中不加单引号也可以,我自己试了却不行?
Parameter index out of range问题
public void addBookStore(BookStore bs) throws Exception{ Connection conn = DBUtils.getConnection(); String sql = "INSERT INTO bs_bookstore(storeName) VALUES ("+"'?')"; PreparedStatement psmt = conn.prepareStatement(sql); //psmt.setInt(1, bs.getMainId()); psmt.setString(1, bs.getStoreName()); psmt.execute(); System.out.println(sql); System.out.println("------------------"+psmt.execute()+"------------------"); }
增加方法如上所示,测试方法如下所示
public static void main(String[] args) { BookStore bs = new BookStore("test"); BookStoreDao bsd = new BookStoreDao(); try { bsd.addBookStore(bs); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
控制台报错:
Parameter index out of range (1 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3700)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3684)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4535)
at com.cjt.dao.BookStoreDao.addBookStore(BookStoreDao.java:21)
at com.test.Test.main(Test.java:13)
是说psmt.setString(1, bs.getStoreName())这一行有错吗?有啥错啊???求教。。。。。
PS:我设置了主键mainId自增