2016-04-13 12 views
3

tek sorgu kullanarak birden fazla tablo içine yerleştirilmesi Bu benim benpreparestatement

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)......... 

nasıl aşabiliriz aşağıda hatayı alıyorum aksi sokulmuş olur dinamik değerler olmadan eklerseniz kodum

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; 

olduğunu bu ??? Ben

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'); 

altında bu ifadeyi kullanarak eklemek mümkün duyuyorum Ama

+0

bir yolu ayrı preparedstatements kullanarak iki ayrı uç ifadeleri çalıştırmaktır. Onları bir arada istediğin için mi? Aynı etkiyi bir işlemde iki deyimle alabilirsiniz. Tek bir ifadede iki parametreli ekleme ifadesi yapabileceğiniz hiçbir belge bulamadım. –

+0

@SamM - İki hazırlık işlemini açmak ve kapatmak için iyi bir uygulama mı? –

+0

@SamM - İki hazırlık işlemini açmak ve kapatmak için iyi bir uygulama mı? –

cevap

2

allowMultiQueries=true&rewriteBatchedStatements=true birden fazla sorgu yürütülmesine izin bağlantı params eklenecek yardıma ihtiyacı dynamic.Please olmak istiyorum.

Örneğin, url gibi olabilir: Bu aşmak için

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

- Teşekkür etti çalıştı ... ama rewriteBatchedStatements = true eklemedim, herhangi bir şeyi etkiler mi? –

+1

Bu iyi. 'rewriteBatchedStatements' bazen daha hızlı olabilir. Ama senin durumun için, hiçbir fark olmadığını düşünüyorum. –