JDBC Mysql Syntax Error - java

I'm using mysql and having select fields from a gui append to my query. However, I'm getting a mysql exception for this query: String query = "INSERT INTO racebikes " + "VALUES(?,?,?)";
For testing measures I printed out the query to make sure the fields were proper which when populated read as: INSERT INTO racebikes VALUES('BMC','Switzerland',10000.0). How is this throwing an syntax exception?
Stack trace:
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 '?,?,?)' 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:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2828)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1842)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1764)
at JDBC_GUI$7.actionPerformed(JDBC_GUI.java:375)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Snippet:
public void actionPerformed(ActionEvent e) {
String query = "INSERT INTO racebikes " + "VALUES(?,?,?)";
try{
statement = connection.prepareStatement(query);
statement.setString(1, (String)bikes_combo_box.getSelectedItem());
statement.setString(2, bike_fields[0].getText());
statement.setDouble(3, Double.parseDouble(bike_fields[1].getText()));
statement.executeUpdate(query);
} catch(SQLException e1) {
e1.printStackTrace();
}

Can you try to put the three fields before VALUES
String query = "INSERT INTO racebikes (field1, field2, field3)" + "VALUES(?,?,?)";
Replace them with your actual field names.

Related

get a date using a JDateChooser and save it into mysql database

I need to read a date from a JDateChooser and save it into a MYSQL database table using the ActionPerformed method of a JButton.
There are three JDateChoosers called LETTER_DATE , RECEIVED_DATE , SENDING_DATE in this JInternalFrameForm called LETTER .
Below is the code of the ActionPerformed method of the JButton SAVE which will save the data into the MYSQL table.
private void SAVEActionPerformed(java.awt.event.ActionEvent evt) {
try{
Statement s=DB.getConnection().createStatement();
s.executeUpdate("insert into LETTER values('"+LETTER_ID.getText()+"','"+LETTER_CATEGORY.getSelectedItem()+"','"+LETTER_DATE.getDate()+"','"+HEADING.getText()+"','"+SECTION_ID.getSelectedItem()+"','"+SUBJECT_ID.getSelectedItem()+"','"+INSTITUTE_ID.getSelectedItem()+"','"+NEW_INSTITUTE.getText()+"','"+SENDERS_ID.getSelectedItem()+"','"+NEW_SENDER.getText()+"','"+RECEIVED_DATE.getDate()+"','"+RECEIVERS_ID.getSelectedItem()+"','"+NEW_RECEIVER.getText()+"','"+SENDING_DATE.getDate()+"')");
JOptionPane.showMessageDialog(this, "Saved");
}
catch(Exception ex){
ex.printStackTrace();
}
}
When I clicked the JButton to save the data, it gives the following stacktrace.
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: 'Mon Dec 04 14:45:36 PST 2017' for column 'LETTER_DATE' at row 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4118)
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:2788)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1816)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1730)
at Viewer.LETTER.SAVEActionPerformed(LETTER.java:280)
at Viewer.LETTER.access$000(LETTER.java:16)
at Viewer.LETTER$1.actionPerformed(LETTER.java:99)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Can anyone please help me to figure out the problem here and correct the datetime value for column ['LETTER_DATE'][1] at row 1?
Well, you should be using getDateFormatString() instead of getDate(). For instance:
s.executeUpdate("insert into LETTER values('"+LETTER_ID.getText()+"','"+LETTER_CATEGORY.getSelectedItem()+"','"+LETTER_DATE.getDateFormatString()+"','"+HEADING.getText()+"','"+SECTION_ID.getSelectedItem()+"','"+SUBJECT_ID.getSelectedItem()+"','"+INSTITUTE_ID.getSelectedItem()+"','"+NEW_INSTITUTE.getText()+"','"+SENDERS_ID.getSelectedItem()+"','"+NEW_SENDER.getText()+"','"+RECEIVED_DATE.getDateFormatString()+"','"+RECEIVERS_ID.getSelectedItem()+"','"+NEW_RECEIVER.getText()+"','"+SENDING_DATE.getDateFormatString()+"')");

Unable to get AutoGenerated ID in MySql JDBC

I am trying to get an autogenerated Id after performing an insert operation. I have a table called sg_InsuranceInfo, and it generates an autoIncremented id. So I need to get it. I have attached sample code for reference
try
{
String insertQuery = "INSERT INTO sg_InsuranceInfo(Patient_Name,LIC_Name) VALUES(?,?)";
PreparedStatement preparedStatement = getConnectionObject().getConnection().prepareStatement(insertQuery, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, insuranceInformation.getPatientName());
preparedStatement.setString(2, insuranceInformation.getLicName());
int noOfRowsAffected = preparedStatement.executeUpdate();
if (noOfRowsAffected != 0)
{
ResultSet resultSet = preparedStatement.getGeneratedKeys();
if(resultSet.next())
{
System.out.println(resultSet.getInt(1));
}
}
}
catch (SQLException e)
{
e.printStackTrace();
}
This is the error text
Exception in thread "AWT-EventQueue-0" java.lang.AbstractMethodError: com.inet.tds.TdsConnection.prepareStatement(Ljava/lang/String;I)Ljava/sql/PreparedStatement;
at hospitalApp.common.InsuranceObject.saveInformation(InsuranceObject.java:42)
at hospitalApp.common.InsuranceObject.updateInformationToDatabase(InsuranceObject.java:161)
at hospitalApp.common.InsuranceController.actionPerformed(InsuranceController.java:117)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
AbstractMethodError.html
Thrown when an application tries to call an abstract method. Normally,
this error is caught by the compiler; this error can only occur at run
time if the definition of some class has incompatibly changed since
the currently executing method was last compiled
I suspect that you may be using different version of JDBC jar file for compiling and running your program as this happens only on runtime.
It does the job
public void saveInformation(InsuranceInformation insuranceInformation)
{
try
{
int noOfRowsAffected = getConnectionObject().getStatement().executeUpdate(
"INSERT INTO sg_InsuranceInfo(Patient_Name,LIC_Name) VALUES(" + "'" + insuranceInformation.getPatientName() + "','" + insuranceInformation.getLicName() + "'"
+ ")");
if (noOfRowsAffected != 0)
{
ResultSet autoGeneratedId = getConnectionObject().getStatement().executeQuery("SELECT SCOPE_IDENTITY()");
if(autoGeneratedId.next())
{
System.out.println(autoGeneratedId.getInt(1));
}
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
You can get further information here http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in Resultset

I'm Trying to run this code to get the ResultSet row by row into an Array so i can put them in a jTable. I don't get why i get this error, i'm new in this.
I would be totally thankfull if you could help me.
try {
int pos = 1;
for (int r=0;r<rsCount;r++){
String[] na = null;
res.absolute(pos);
int a=1;
while(res.next()){
na[a]=res.getString(a+1);
a+=1;
}
pos+=1;
for(int c=0;c<8;c++){
tabla.setValueAt(na[c], r, c);
}
}
} catch (SQLException ex) {
Logger.getLogger(Reporte.class.getName()).log(Level.SEVERE, null, ex);
}
}
And here is the OUTPUT:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Main.Reporte.jButton1ActionPerformed(Reporte.java:254)
at Main.Reporte.access$000(Reporte.java:17)
at Main.Reporte$1.actionPerformed(Reporte.java:78)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Your string array is not being properly instantiated. You need to instantiate it like:
String[] na = new String[100];
If you don't know how big the array is going to be you might want to consider using an ArrayList
ArrayList<String> na = new ArrayList<String>();
then each string can be added with:
na.add(res.getString(a+1));

Error while inserting data into Access using Netbeans

While creating a connection to Access and trying to insert simple data I get the following error:
run:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javaapplication1.Authenticate.insertdata(Authenticate.java:49)
at javaapplication1.vendor.actionPerformed(vendor.java:74)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Here's the code snippet if anyone can help a little and look into this. The code
code compiles fine but gives an error at runtime:
package javaapplication1;
import java.sql.*;
public class Authenticate {
final String fileName = "C:/Users/darksword/Desktopdb.accdb";
Connection con = null;
Authenticate()
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+fileName;
con = DriverManager.getConnection(url,"","");
}
catch (Exception e)
{
}
finally
{
try { if(con!=null) {con.close();} } catch (Exception e) {}
}
}
void insertdata(String Name,String Address,String ContactNumber,int y) throws SQLException
{
String str = Integer.toString(y);
//Statement st=con.createStatement();
PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("insert into product(id,VName,VAddress,VContactNumber) values(?,?,?,?)");
pstmt.setString(1, str);
pstmt.setString(1, Name);
pstmt.setString(1, Address);
pstmt.setString(1, ContactNumber);
pstmt.executeUpdate();
pstmt.close();
}
}
You're silently catching an exception when establishing the database connection which leaves con as null should an exception occur. Add
e.printStackTrace();
to the exception block.
In addition, you close your Connection immediately after assigning the variable in the constructor
con.close();

PreparedStatementCallback; bad SQL grammar in JDBC Spring

I have an update method to update the content of a table based on a specific field and I am getting following exception.
Code is :
public void updateTaxoActive(String oldDesc, String description) {
Logger.getLogger(TypeCategoryJDBCTemplate.class.getName()).log(Level.INFO, "old taxonomy code is {0} new description is {1}", new Object[]{oldDesc, description});
String sql = "update product.typecategory set taxonomydescription = ? where taxonomydescription = ?";
jdbcTemplateObject.update(sql, new Object[]{description,oldDesc},new TypeCategoryMapper());
}
Exception is:
Exception in thread "AWT-EventQueue-0" org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [update product.typecategory set taxonomydescription = ? where taxonomydescription = ?]; nested exception is com.ibm.db2.jcc.am.SqlSyntaxErrorException: [jcc][1091][10824][4.13.127] Invalid data conversion: Parameter instance [Ljava.lang.Object;#1dc421 is invalid for the requested conversion. ERRORCODE=-4461, SQLSTATE=42815
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:95)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:605)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:818)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:874)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:882)
at com.ibm.quest.model.typecategory.TypeCategoryJDBCTemplate.updateTaxoActive(TypeCategoryJDBCTemplate.java:59)
at com.ibm.quest.taxonomyupdate.ui.ApplicationWindow.updateButtonActionPerformed(ApplicationWindow.java:255)
at com.ibm.quest.taxonomyupdate.ui.ApplicationWindow.access$800(ApplicationWindow.java:16)
at com.ibm.quest.taxonomyupdate.ui.ApplicationWindow$10.actionPerformed(ApplicationWindow.java:218)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: [jcc][1091][10824][4.13.127] Invalid data conversion: Parameter instance [Ljava.lang.Object;#1dc421 is invalid for the requested conversion. ERRORCODE=-4461, SQLSTATE=42815
at com.ibm.db2.jcc.am.id.a(id.java:677)
at com.ibm.db2.jcc.am.id.a(id.java:60)
at com.ibm.db2.jcc.am.id.a(id.java:103)
at com.ibm.db2.jcc.am.oo.c(oo.java:2584)
at com.ibm.db2.jcc.am.oo.setObject(oo.java:2363)
at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:365)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:217)
at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:145)
at org.springframework.jdbc.core.ArgPreparedStatementSetter.doSetValue(ArgPreparedStatementSetter.java:65)
at org.springframework.jdbc.core.ArgPreparedStatementSetter.setValues(ArgPreparedStatementSetter.java:46)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:822)
at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:818)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:589)
... 43 more
What are you doing with TypeCategoryMapper ?
Remove ,new TypeCategoryMapper()

Categories