插入Select&;在java中插入到单个查询中

插入Select&;在java中插入到单个查询中,java,mysql,select,insert,Java,Mysql,Select,Insert,我正在创建一个计费应用程序。我想在mySQL中同时将相同的数据插入两个不同的表中。谁能告诉我该怎么问。我的任务是首先将数据插入calculationinfo表,然后将相同的数据插入salesinfo表,然后从calculation表中选择all。我正在尝试此操作,但遇到SQL语法错误。 这是我的问题 "Insert into calculationinfo (`date`,`prod_name`, `prod_amount`, `total_amount`,`user`, `Gtotal`) v

我正在创建一个计费应用程序。我想在mySQL中同时将相同的数据插入两个不同的表中。谁能告诉我该怎么问。我的任务是首先将数据插入calculationinfo表,然后将相同的数据插入salesinfo表,然后从calculation表中选择all。我正在尝试此操作,但遇到SQL语法错误。 这是我的问题

"Insert into calculationinfo (`date`,`prod_name`, `prod_amount`, `total_amount`,`user`, `Gtotal`) values(?,?,?,?,?,?) INSERT INTO salesinfo (`date`,`prod_name`, `prod_amount`, `total_amount`,`user`, `Gtotal`) SELECT * From calculationinfo"
我正在使用java语言。这是指纹

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT salesinfo `date`,`prod_name`, `prod_amount`, `total_amount`,`user`, `Gtot' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2487)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
at Restaurant.Calculation.item_tblMouseClicked(Calculation.java:323)
at Restaurant.Calculation.access$600(Calculation.java:34)
at Restaurant.Calculation$7.mouseClicked(Calculation.java:259)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6536)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

使用自动关闭资源,如

try (PreparedStatement stmt = connection.prepareStatement("Insert into calculationinfo (`date`,`prod_name`, `prod_amount`, `total_amount`,`user`, `Gtotal`) values(?,?,?,?,?,?)") {

        //Set Data here     

        stmt.executeUpdate();
    }
    // stmt is auto closed here, even if SQLException is thrown


    try (PreparedStatement stmt = connection.prepareStatement("INSERT INTO salesinfo (`date`,`prod_name`, `prod_amount`, `total_amount`,`user`, `Gtotal`) VALUES (?, ?, ?,?,?,?)");
       //set same data above here
        stmt.executeUpdate();
    }

您需要使用终止符分隔这两个查询

"Insert into calculationinfo"+ 
"(`date`,`prod_name`, `prod_amount`, `total_amount`,`user`, `Gtotal`) "+ 
"values(?,?,?,?,?,?);"+ 
"INSERT INTO salesinfo"+ 
"(`date`,`prod_name`, `prod_amount`,`total_amount`,`user`, `Gtotal`) "+
"values(?,?,?,?,?,?)";
这将帮助您完成查询。我在上面所做的是在以可读模式格式化查询后追加字符串

注意:我将值传递到第二个表的过程与您将值传递到第一个表的过程相同。这也行


Insert into table 1从table 2中选择*
会将table 1中的所有行插入table 2中,这会弄乱您的数据。

您在查询中缺少了
into
这个词。我在插入时使用了这两个词,我在哪里遗漏了这个词?根据堆栈跟踪,你在插入salesinfo时漏掉了它,但它还是给了我同样的错误。你应该用
分隔你的语句。现在我得到了这个异常java.sql.SQLException:没有为参数7指定值。首先,我为出于嫉妒而否决这个答案的人感到遗憾。其次,您需要像在Java中通常做的那样,将用户输入传递给
值(UserInput1,UserInput2)
。我不知道java是如何处理数据插入的。