在JAVA中执行Transaction Mysql

时间:2013-11-25 17:00:53

标签: java mysql jdbc

我正在尝试在我的Java程序中运行此查询:

START TRANSACTION;
INSERT INTO Term(mot) VALUES ('NAN');
SELECT LAST_INSERT_ID() INTO @term_id;
INSERT INTO Paragraphe(xpath,idDoc) VALUES ('/BALADE/doc','d001.xml');
SELECT LAST_INSERT_ID() INTO @parag_id;
INSERT INTO occurrence(occurrence) VALUES (22);
SELECT LAST_INSERT_ID() INTO @occur_id;
INSERT INTO Term_Parag(id_term,id_parag,id_occur) VALUES (@term_id, @parag_id,@occur_id);
COMMIT;

当我像这样运行程序时,我收到一个错误:

query = "START TRANSACTION; "+
                         "INSERT INTO Term(mot) VALUES (?); "+
                         "SELECT LAST_INSERT_ID() INTO @term_id; "+
                         "INSERT INTO Paragraphe(xpath,idDoc) VALUES (?,?); "+
                         "SELECT LAST_INSERT_ID() INTO @parag_id; "+
                         "INSERT INTO occurrence(occurrence) VALUES (?); "+
                         "SELECT LAST_INSERT_ID() INTO @occur_id; "+
                         "INSERT INTO Term_Parag(id_term,id_parag,id_occur) VALUES (@term_id, @parag_id,@occur_id); "+
                         "COMMIT;";

                ps = conn.prepareStatement(query);
                ps.setString(1, unWord.content);
                ps.setString(2, unWord.path);
                ps.setString(3, doc);
                ps.setInt(4, unWord.occurence);

                ps.execute();

当我直接在Mysql PHPmyadmin中运行查询时,它可以工作。帮助!

1 个答案:

答案 0 :(得分:0)

我通过在Mysql中创建一个存储过程并从我的Java程序中调用它来解决了这个问题。