占位符報語法錯誤
新增和更新方法類里面的占位符能夠跑起來,但是刪除和查詢單個女神里面的占位符就會報語法錯誤,將占位符手動改為具體整型數(shù)字就可以正確運行。
以下是部分源碼。
//刪除功能代碼塊 public?void?delPeople?(Integer?id)?throws?SQLException?{ //獲取數(shù)據(jù)庫連接 ?Connection?conn?=?DBUtil.getConnection(); ?//編寫sql語法規(guī)范的字符串為數(shù)據(jù)庫修改數(shù)據(jù) ?String?sql?=?"" ? +?"?DELETE?FROM?test" ? +?"?WHERE?id=?"; ?PreparedStatement?pstmt?=?conn.prepareStatement(sql); ? ?pstmt.setInt(1,?id); //通過PreparedStatement對象執(zhí)行SQL語句 ?pstmt.execute(); } //查詢功能代碼塊 public?People?get?(Integer?id)?throws?Exception?{ //獲取數(shù)據(jù)庫連接 ?Connection?conn?=?DBUtil.getConnection(); ?//編寫sql語法規(guī)范的字符串為數(shù)據(jù)庫修改數(shù)據(jù) ?String?sql?=?"" ? +?"?SELECT?*?FROM?test" ? +?"?WHERE?id=?"; ?PreparedStatement?pstmt?=?conn.prepareStatement(sql); ?pstmt.setInt(1,?id); //通過PreparedStatement對象的executeQuery方法查詢數(shù)據(jù)庫存儲的信息 ?ResultSet?rs?=?pstmt.executeQuery(sql); ?People?p?=?null; ?while(rs.next())?{ ?p?=?new?People(); ?p.setAge(rs.getInt("age")); ?p.setUsername(rs.getString("username")); ?p.setId(rs.getInt("id")); ?p.setSex(rs.getInt("sex")); ?} return?p; } //錯誤信息 Exception?in?thread?"main"?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?'?'?at?line?1 at?sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native?Method) at?sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at?sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at?java.lang.reflect.Constructor.newInstance(Constructor.java:526) at?com.mysql.jdbc.Util.handleNewInstance(Util.java:425) at?com.mysql.jdbc.Util.getInstance(Util.java:408) at?com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943) at?com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) at?com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) at?com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527) at?com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680) at?com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2497) at?com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2455) at?com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1369) at?com.javajdbc.dao.PeopleDao.get(PeopleDao.java:103) at?com.javajdbc.peopleaction.PeopleAction.main(PeopleAction.java:35)
2017-03-29
問題已經(jīng)被我查找API幫助文檔解決了,刪除功能代碼塊沒有問題,出問題的是查詢功能代碼塊里面,executeQuery方法不應該傳入?yún)?shù),將括號里面的String字符串去掉即可,希望各位對各位同學有幫助。