Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中执行复合sql查询?_Java_Mysql_Jdbc - Fatal编程技术网

如何在java中执行复合sql查询?

如何在java中执行复合sql查询?,java,mysql,jdbc,Java,Mysql,Jdbc,如何执行以下查询并通过prepared语句检索结果: INSERT INTO vcVisitors (sid) VALUES (?); SELECT LAST_INSERT_ID(); 有没有办法同时执行这两条语句? 我已尝试执行以下操作: Connection con = DbManager.getConnection(); PreparedStatement ps = con.PrepareStatement( "INSERT INTO vcVisitors (sid) VAL

如何执行以下查询并通过prepared语句检索结果:

INSERT INTO vcVisitors (sid) VALUES (?); SELECT LAST_INSERT_ID();
有没有办法同时执行这两条语句?

我已尝试执行以下操作:

Connection con = DbManager.getConnection();
PreparedStatement ps = con.PrepareStatement(
     "INSERT INTO vcVisitors (sid) VALUES (?); SELECT LAST_INSERT_ID();");
ps.setInt(1, 10);
ResultSet rs = ps.exequteQuery();
rs.next();
return rs.getInt("LAST_INSERT_ID()");
ps.execute();
rs = ps.getResultSet();
但是它给了我一个错误executeQuery不能执行这样的查询, 我还尝试用以下内容替换executeQuery:

Connection con = DbManager.getConnection();
PreparedStatement ps = con.PrepareStatement(
     "INSERT INTO vcVisitors (sid) VALUES (?); SELECT LAST_INSERT_ID();");
ps.setInt(1, 10);
ResultSet rs = ps.exequteQuery();
rs.next();
return rs.getInt("LAST_INSERT_ID()");
ps.execute();
rs = ps.getResultSet();
但它给了我SQL语法错误:

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 'SELECT LAST_INSERT_ID()' at line 1
但执行查询并没有问题 “插入vcVisitors(sid)值('10');直接从mysql控制台选择LAST_INSERT_ID();”。

在更新(插入)数据时使用
executeUpdate
而不是
executeQuery
。尝试执行
选择上次插入\u ID()
作为另一个查询

但它不是可移植的查询。我建议改用
语句.getGeneratedKeys
。请看这里:

以下是正确使用的最后一个插入ID()的示例:

在这里,getGeneratedKeys也是如此:

     Statement stmt = null;
   ResultSet rs = null;

   try {

    //
    // Create a Statement instance that we can use for
    // 'normal' result sets assuming you have a
    // Connection 'conn' to a MySQL database already
    // available

    stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY,
                                java.sql.ResultSet.CONCUR_UPDATABLE);

    //
    // Issue the DDL queries for the table for this example
    //

    stmt.executeUpdate("DROP TABLE IF EXISTS autoIncTutorial");
    stmt.executeUpdate(
            "CREATE TABLE autoIncTutorial ("
            + "priKey INT NOT NULL AUTO_INCREMENT, "
            + "dataField VARCHAR(64), PRIMARY KEY (priKey))");

    //
    // Insert one row that will generate an AUTO INCREMENT
    // key in the 'priKey' field
    //

    stmt.executeUpdate(
            "INSERT INTO autoIncTutorial (dataField) "
            + "values ('Can I Get the Auto Increment Field?')",
            Statement.RETURN_GENERATED_KEYS);

    //
    // Example of using Statement.getGeneratedKeys()
    // to retrieve the value of an auto-increment
    // value
    //

    int autoIncKeyFromApi = -1;

    rs = stmt.getGeneratedKeys();

    if (rs.next()) {
        autoIncKeyFromApi = rs.getInt(1);
    } else {

        // throw an exception from here
    }

    rs.close();

    rs = null;

    System.out.println("Key returned from getGeneratedKeys():"
        + autoIncKeyFromApi);
} finally {

    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException ex) {
            // ignore
        }
    }

    if (stmt != null) {
        try {
            stmt.close();
        } catch (SQLException ex) {
            // ignore
        }
    }
}
尝试:

Connection con = DbManager.getConnection();
int lastid = 0;

PreparedStatement psInsert = con.PrepareStatement(
     "INSERT INTO vcVisitors (sid) VALUES (?)");
psInsert.setInt(1, 10);
psInsert.executeUpdate();

PreparedStatement psSelect = 
    con.PrepareStatement("SELECT LAST_INSERT_ID() AS lastid");
ResultSet rs = psSelect.executeQuery();
rs.next();

lastid = rs.getInt("lastid");

rs.close();
psInsert.close();
psSelect.close();
con.close();
return lastid;

正如其他人已经说过的,这是行不通的。但看起来您只是想获取最后一次插入的ID。要实现这一点,可以使用类的方法。

他特别希望同时执行两个查询。我认为这是不可能的。尤其不是使用
INSERT
SELECT
。是的,它肯定可以工作,但我的问题是如何同时执行几个查询once@tsds:但两个查询始终作为两个查询运行。