Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting into multiple tables using a single query in preparestatement [duplicate]

Tags:

java

mysql

jdbc

This is my code

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;

If I insert without dynamic values it gets inserted otherwise I get the below error

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

How can I overcome this ??? I am able to insert using this statement below

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

But I want it to be dynamic.Please help

like image 275
Raja Dhasan Avatar asked Dec 12 '25 09:12

Raja Dhasan


1 Answers

allowMultiQueries=true&rewriteBatchedStatements=true needs to be added to the connection params to allow execution of multiple queries.

For example, the url may be like:

jdbc:mysql://xxx.xxx.xxx.xxx:nnnnn/db_name?allowMultiQueries=true&rewriteBatchedStatements=true&user=xxxx&password=yyyy
like image 89
Dylan Su Avatar answered Dec 15 '25 00:12

Dylan Su



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!