2016-04-13 2 views
3

に単一のクエリを使用して複数のテーブルへの挿入これは、私はそれが私がのprepareStatement

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 Activities(EmployeeID,ActivityType,Activities) values ('6','createbr' at line 1 
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 Activities(EmployeeID,ActivityType,Activities) values ('6','createbr' at line 1 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
    at com.mysql.jdbc.Util.getInstance(Util.java:386) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1053) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794) 
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)......... 

がどのように克服することができ、以下のエラーが出るそうでない場合は挿入します動的な値なしで挿入した場合、私のコード

String query = "insert into Branch (CompanyID,BranchName,BranchAddress,Email,Phone,ContactPerson) values (?,?,?,?,?,?);insert into Activities(EmployeeID,ActivityType,Activities) values (?,?,?)"; 

      // System.out.println(mobile);//BranchName  | BranchAddress  | Email  | Phone  | ContactPerson 

      pstmt = conn.prepareStatement(query); 
      pstmt.setString(1, compid); 
      pstmt.setString(2, name); 
      pstmt.setString(3, address); 
      pstmt.setString(4, email); 
      pstmt.setString(5, mobile); 
      pstmt.setString(6, contactperson); 
      pstmt.setString(7, empid); 
      pstmt.setString(8, acttype); 
      pstmt.setString(9, activity); 
      System.out.println(query+"-----"+acttype); 
      pstmt.execute(); 
      status = true; 

ですこの ???私は

insert into Branch (CompanyID,BranchName,BranchAddress,Email,Phone,ContactPerson) values ('1','sample','11 cross','[email protected]','5467897656','Deigo');insert into Activities(EmployeeID,ActivityType,Activities) values ('6','createdbranch','cearted branch sample'); 

の下にこの文を使用して挿入することですが、私はそれが複数のクエリの実行を許可するには、接続のparamsに追加する必要がallowMultiQueries=true&rewriteBatchedStatements=trueヘルプ

+0

例えば、urlは次のようになることがあります。あなたが1つでそれらをしたい任意の理由?あるトランザクションで2つのステートメントで同じ効果を得ることができます。私はあなたが1つのステートメントで2つのパラメータ化された挿入ステートメントを行うことができるドキュメントは見つかりませんでした。 –

+0

@SamM - 2つの準備文を開いて閉じるのは良い習慣ですか? –

+0

@ SamM - 2つの準備文を開いて閉じるのは良い方法ですか? –

答えて

2

をdynamic.Pleaseすることにしたいです。これを克服するための1つの方法は、別のPreparedStatementを使用して2つの別々のINSERT文を実行することです

jdbc:mysql://xxx.xxx.xxx.xxx:nnnnn/db_name?allowMultiQueries=true&rewriteBatchedStatements=true&user=xxxx&password=yyyy 
+1

- ありがとうございました。しかし、私はrewriteBatchedStatements = trueを追加しませんでした。何かに影響しますか? –

+1

それは問題ありません。 'rewriteBatchedStatements'は時々より速いかもしれません。しかし、あなたの場合、私は違いがないと思う。 –

関連する問題