check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?
来源:互联网 发布:阿里云国际版购买 编辑:程序博客网 时间:2024/06/05 15:00
测试类如下:在执行的过程中一致报上面的错误:
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()里面什么内容都不需要放,因为前面已经设置过了,再次设置是错误的。
0 0
- 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 'oiw
- 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 near '?,?
- 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 ”
- 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.....
- 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
- check the manual that corresponds to your MySQL server version for the right syntax to
- ”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“解决
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
- MySQL:check the manual that corresponds to your MySQL server version for the right syntax
- MySQL check the manual that corresponds to your MySQL server version for the right syntax错误
- 【t074】上学路线
- loadrunner问题备忘
- c 语言宏定义 #define 的理解与资料整理
- 在Linux里设置环境变量的方法(export PATH)
- weblogic查看访问工程地址
- check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?
- 一个简单的 java 文件流下载函数
- Laravel5 支付宝
- MySQL 日期类型及默认设置
- Mac上设置MySQL默认编码为UTF-8
- Python pip安装模块报错
- 移动端中的vMin和vMax
- mysql中取随机数据
- 第十四周 实践项目<1>--数组大折腾(3)