Reputation: 513
I am, for the most part, just importing values into a table on MySQL. The following code gets a SQL syntax error:
grossProfit = Double.parseDouble(salesText.getText())-Double.parseDouble(cogsText.getText());
totalExpenses = Double.parseDouble(ExSalesText.getText())+Double.parseDouble(GaExText.getText())+Double.parseDouble(otherExText.getText());
profit = grossProfit-totalExpenses;
Sql = "update budgetreport" +
" set sales=?"+
", cogs=?"+
", gross_profit=?"+
", sales_ex=?"+
", g&a_ex=?"+
", other_ex=?"+
", total_ex=?"+
", profit=?"+
" where quarter=?";
try {
PreparedStatement statement = conn.prepareStatement(Sql);
statement.setString(1, salesText.getText());
statement.setString(2, cogsText.getText());
statement.setDouble(3, grossProfit);
statement.setString(4, ExSalesText.getText());
statement.setString(5, GaExText.getText());
statement.setString(6, otherExText.getText());
statement.setDouble(7, totalExpenses);
statement.setDouble(8, profit);
statement.setInt(9, qTracker);
statement.executeUpdate();
} catch (Exception e1) {
e1.printStackTrace();
}
It worked before, when I only had 2 input variables in the SQL code. How do I make this work?
Edit: Error(Just a SQL syntax error I believe)
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 '&a_ex='1', other_ex='1', total_ex=3.0, profit=3.0, where quarter=2' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:400)
at com.mysql.jdbc.Util.getInstance(Util.java:383)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:980)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2049)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2034)
at myProjects.budgetReport.lambda$2(budgetReport.java:180)
at myProjects.budgetReport$$Lambda$15/1910163204.actionPerformed(Unknown Source)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Upvotes: 1
Views: 96
Reputation: 68715
g&a_ex
does not look like a valid column name. Most likely a typo
EDIT: Thanks to Jason W for mentioning that actually & is allowed in mysql identifiers provided escaped with backtick `
Upvotes: 3
Reputation: 13209
Not knowing your table definition or benfit of error log, I assume the syntax error is one of your table names, g&a_ex
. Perhaps this should be verified as the correct table name and corrected or escaped appropriately. To escape the table name, use the reverse single quote (`) around the column name.
Upvotes: 0
Reputation: 2154
Check your query. It should be like
Sql = "update table budgetreport" +
" set sales=?"+
", cogs=?"+
", gross_profit=?"+
", sales_ex=?"+
", g&a_ex=?"+
", other_ex=?"+
", total_ex=?"+
", profit=?"+
" where quarter=?";
Upvotes: 0