check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?
2016-12-02 17:22
537 查看
测试类如下:在执行的过程中一致报上面的错误:
package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCUtils1 { public static void main(String[] args) throws Exception { System.out.println(getGeneratedKey("dada", "123")); } public static Connection conn; public static String user = "root"; public static String password = "123456"; public static String url = "jdbc:mysql://localhost/shiro"; /** * 获取自增的 ID * @return * @throws Exception */ public static int getGeneratedKey(String username, String password) throws Exception { Connection conn = getMysqlConn(); String sql = "insert into test (username,password) values (?,?)"; PreparedStatement pstat = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); pstat.setString(1, username); pstat.setString(2, password); pstat.executeUpdate(sql); ResultSet rs = pstat.getGeneratedKeys(); rs.next(); int key = rs.getInt(1); return key; } /** * @return 获取 Mysql 连接 * @throws Exception */ public static Connection getMysqlConn() throws Exception { if(conn == null) { Class.forName("org.gjt.mm.mysql.Driver"); conn = DriverManager.getConnection(url, user, password); } return conn; } }
修改后如下:
package com.github.zhangkaitao.shiro.chapter2.realm; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCUtils1 { public static void main(String[] args) throws Exception { System.out.println(getGeneratedKey("dada", "123")); } public static Connection conn; public static String user = "root"; public static String password = "123456"; public static String url = "jdbc:mysql://localhost/shiro"; /** * 获取自增的 ID * @return * @throws Exception */ public static int getGeneratedKey(String username, String password) throws Exception { Connection conn = getMysqlConn(); String sql = "insert into test (username,password) values (?,?)"; PreparedStatement pstat = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); pstat.setString(1, username); pstat.setString(2, password); pstat.executeUpdate(); ResultSet rs = pstat.getGeneratedKeys(); rs.next(); int key = rs.getInt(1); return key; } /** * @return 获取 Mysql 连接 * @throws Exception */ public static Connection getMysqlConn() throws Exception { if(conn == null) { Class.forName("org.gjt.mm.mysql.Driver"); conn = DriverManager.getConnection(url, user, password); } return conn; } }
区别:
第一个里面在执行 pstat.executeUpdate(sql); 时候把原始的sql又放进去了,这肯定是有问题的,前面已经设置过值了,现在又设置了一遍 sql 那么前面的东西都被替换掉了,pstmt 执行的内容变成了:“insert into test (username,password) values (?,?)”,这样自然会报错了。其实只需要执行 pstat.executeUpdate()里面什么内容都不需要放,因为前面已经设置过了,再次设置是错误的。相关文章推荐
- check the manual that corresponds to your MySQL server version for the right syntax to use near 'cre
- 异常“check the manual that corresponds to your MySQL server version for the right syntax to use near ”
- check the manual that corresponds to your MySQL server version for the right syntax to use near 'TYP
- check the manual that corresponds to your MySQL server version for the right syntax to use near '..'
- 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
- check the manual that corresponds to your MySQL server version for the right syntax to use near 'lim
- check the manual that corresponds to your MySQL server version for the right syntax to use near
- 关于check the manual that corresponds to your MySQL server version for the right syntax to use near
- 插入mysql语句报错: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
- check the manual that corresponds to your MySQL server version for the right syntax to use near 'oiw
- check the manual that corresponds to your MySQL server version for the right syntax to use near‘XXX’
- CHECK the manual that corresponds TO your MySQL SERVER VERSION FOR the RIGHT syntax TO USE near.....
- MySql 执行语句错误 Err] 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
- check the manual that corresponds to your MySQL server version for the right syntax to use near 'USI
- "check the manual that corresponds to your MySQL server version for the right syntax to use“解决
- ”check the manual that corresponds to your MySQL server version for the right syntax to use“解决
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
- heck the manual that corresponds to your MySQL server version for the right syntax to use near 'OPTI
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
- 01-check the manual that corresponds to your MySQL server version for the right syntax