您的位置:首页 > 数据库 > MySQL

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()里面什么内容都不需要放,因为前面已经设置过了,再次设置是错误的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql
相关文章推荐