【问题标题】:Inserting into multiple tables using a single query in preparestatement [duplicate]在preparestatement中使用单个查询插入多个表[重复]
【发布时间】:2016-08-04 01:07:12
【问题描述】:

这是我的代码

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;

如果我插入没有动态值,它会被插入,否则我会收到以下错误

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

我该如何克服这个???我可以在下面使用此语句插入

insert into Branch (CompanyID,BranchName,BranchAddress,Email,Phone,ContactPerson) values ('1','sample','11 cross','fakemail@gmail.com','5467897656','Deigo');insert into Activities(EmployeeID,ActivityType,Activities) values ('6','createdbranch','cearted branch sample');

但我希望它是动态的。请帮助

【问题讨论】:

  • 克服这个问题的一种方法是使用单独的preparedstatements运行两个单独的insert语句。你有什么理由想要它们合二为一?您可以在事务中使用两个语句获得相同的效果。我还没有找到任何可以在单个语句中执行两个参数化插入语句的文档。
  • @SamM - 打开和关闭两个preparedstatement是一个好习惯吗?
  • @SamM - 打开和关闭两个preparedstatement是一个好习惯吗?
  • 是的,使用多个准备好的语句是标准做法。记得在通话之间关闭它。如果需要,您可以将它们包装在单个数据库事务中。

标签: java mysql jdbc


【解决方案1】:

allowMultiQueries=true&rewriteBatchedStatements=true 需要添加到连接参数中以允许执行多个查询。

例如,url 可能是这样的:

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

【讨论】:

  • - 谢谢它有效..但我没有添加 rewriteBatchedStatements=true,它会影响任何事情吗?
  • 没关系。 rewriteBatchedStatements 有时可能会更快。但是对于你的情况,我认为没有区别。
猜你喜欢
  • 1970-01-01
  • 2019-02-03
  • 2013-02-28
  • 2019-04-19
  • 2017-01-15
  • 2011-12-03
  • 2015-02-24
  • 1970-01-01
  • 2011-08-01
相关资源
最近更新 更多