Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.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
使用BEGIN和COMMIT java在多个表上插入MySql_Java_Mysql - Fatal编程技术网

使用BEGIN和COMMIT java在多个表上插入MySql

使用BEGIN和COMMIT java在多个表上插入MySql,java,mysql,Java,Mysql,这个查询在java上可能吗 "BEGIN;" + "INSERT INTO product(code, name, description, category_id) " + "VALUES(?,?,?,?);" + "INSERT INTO inventory_item(quantity, price, product_id) " + "VALUES(?,?,LAST_INSERT_ID());" + "COMMIT;"; 我在一个PreparedStatement上使用了它,它真的占用了我

这个查询在java上可能吗

"BEGIN;"
+ "INSERT INTO product(code, name, description, category_id) "
+ "VALUES(?,?,?,?);"
+ "INSERT INTO inventory_item(quantity, price, product_id) "
+ "VALUES(?,?,LAST_INSERT_ID());"
+ "COMMIT;";
我在一个
PreparedStatement
上使用了它,它真的占用了我的时间,只是为了找出错误我的
dbUnit
说语句上有错误

com.example.dao.exception.DataAccessException: 
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 'INSERT INTO product(code, name, description, 
category_id) VALUES('00003','lemon ' at line 1
    at 
com.example.dao.InventoryDaoImpl.addInventoryItem(InventoryDaoImpl.java:126)
我无法在我尝试过的控制台上打印PreparedStatement

PreparedStatement statement = 
        connection.prepareStatement( FIND_INVENTORY_ITEM_BY_PRODUCT_CODE_QUERY );
System.out.print( statement );
你们能帮我找出错误吗?

可能不是这样,但这对我来说一直很有效:

List<String> sqlStatements = new ArrayList<String>();
// stuff your statements into this list
// (I'm often reading them from some file. The file often
// contains blank lines, comments and semicolons, which I
// strip out.)
Statement stmt = null;
try {
    dbConn.setAutoCommit(false);
    stmt = dbConn.prepareStatement();
    for ( String sql : sqlStatements ) {
        logger.debug("\t"+sql);
        stmt.addBatch(sql);
    }
    stmt.executeBatch();
    dbConn.commit();
} catch ( Exception e ) {
    // handle exceptions
} finally {
    // close statement
}
List sqlStatements=new ArrayList();
//把你的陈述填入这个列表
//(我经常从一些文件中读它们
//包含空行、注释和分号,其中
//去掉。)
语句stmt=null;
试一试{
数据库连接设置自动提交(错误);
stmt=dbConn.prepareStatement();
for(字符串sql:sqlStatements){
logger.debug(“\t”+sql);
stmt.addBatch(sql);
}
stmt.executeBatch();
提交();
}捕获(例外e){
//处理异常
}最后{
//结束语
}

我想,在begin-END块的末尾有
END
)我已经有了
COMMIT
当你在mysql服务器上手动尝试时,同样的查询是否有效?必须有
END在块的末尾-否则是语法错误