How to store a file in a mysql database using blob - java

I have searched about this matter a lot but, I didn't get a proper solution for my problem, I have following method for storing a file using java,
private void uploadFile() throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/FileDb?autoReconnect=true", "user1", "123789");
System.out.println("DB connection extablished!");
PreparedStatement statement = connection.prepareStatement("INSERT INTO FilesTb(`FileData`) VAULES(?)");
File file = new File(txtDir.getText() + "/" + txtFile.getText());// get path and file name from text fields
FileInputStream fIn = new FileInputStream(file);
statement.setBlob(1, fIn);
statement.executeUpdate();
JOptionPane.showMessageDialog(null, "File stored successfully!");
}
when I try to run this code I got following 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 'VAULES(_binary'<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<jasperReport xmlns=\"' 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:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3562)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3494)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1960)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2105)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2398)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2316)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2301)
at com.views.FileUploader.uploadFile(FileUploader.java:154)
at com.views.FileUploader.btnUploadActionPerformed(FileUploader.java:125)
at com.views.FileUploader.access$100(FileUploader.java:27)
at com.views.FileUploader$2.actionPerformed(FileUploader.java:66)
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:6504)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6269)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4860)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4686)
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:2713)
at java.awt.Component.dispatchEvent(Component.java:4686)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
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:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
please assist me regarding this..

convert the file data into byte array and set in the sql statement

Try this
File fBlob = new File ("<your_path>");
FileInputStream is = new FileInputStream(fBlob);
statement.setBinaryStream(1, is, (int) fBlob.length());
statement.execute();

Related

How to apply LIKE keyword's search using jtextfield's value?

All I want it search the related names and set value on jtextfeild, like we got in google. (Give name suggestions that you have in database). Here is the code I used but it gets syntax error. Of course it should get it. Because code is wrong.
private void txtLnameKeyReleased(java.awt.event.KeyEvent evt)
{
try {
ResultSet rs = JDBC.getData("select lot_name from lot where lot_type='" + company.getSelectedItem().toString() + "' and lot_name like '"+txtLname.getText()+"'% ");
if (rs.first()) {
txtLname.setText(rs.getString("lot_name"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
I really don't know how to set up % with text field's gettext() method. Please help me. Thank you.
Added 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 '%' 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:525)
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:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2619)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2569)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1521)
at Modle.JDBC.getData(JDBC.java:46)
at lottery.LottryRegistration.txtLnameKeyReleased(LottryRegistration.java:736)
at lottery.LottryRegistration.access$1800(LottryRegistration.java:22)
at lottery.LottryRegistration$16.keyReleased(LottryRegistration.java:298)
at java.awt.Component.processKeyEvent(Component.java:6466)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2829)
at java.awt.Component.processEvent(Component.java:6282)
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.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
at java.awt.Component.dispatchEventImpl(Component.java:4731)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
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)
Percent on the wrong side of comment sign:
ResultSet rs = JDBC.getData("select lot_name from lot where lot_type='"
+ company.getSelectedItem().toString()
+ "' and lot_name like '"
+ txtLname.getText()
+ "%' ");

data not saving in database through jtable (Edited)

i have made a jtable where i am giving the values manually,and saving the data in the sql by clicking the save(jButton2) button.now the problem is that when i am keeping the jtable blank and saving it..its showing data saved,but nothing is saving in the database.
And when i am putting the data in the jtable and trying to save its giving exception.
column(i,5) is getting value by multiplying column(i,3) and column(i,4)
jButton2ActionPerformed(save button) method is as follows:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
String sql="insert into `sampleDB`.`Item_detail` (P_name,Manuf_name,Model_no,Purchase_price,Available,total Price,Sell_price,Quality,Color,Description,profit,Cost_price,idDealerItem) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
pst=con.prepareStatement(sql);
int last=jTable1.getRowCount();
for( int i=0;i<last;i++){
if(((String) jTable1.getValueAt(i,0))!=null){
String column1= (String) jTable1.getValueAt(i,0);
String column2= (String) jTable1.getValueAt(i,1);
String column3= (String) jTable1.getValueAt(i,2);
String column4= (String) jTable1.getValueAt(i,3);
String column5= (String) jTable1.getValueAt(i,4);
String column6= (String) jTable1.getValueAt(i,5);
String column7= (String) jTable1.getValueAt(i,6);
pst.setString(1,column1);
pst.setString(2,column2);
pst.setString(3,column3);
pst.setString(4,column4);
pst.setString(5,column5);
pst.setString(6,column6);
pst.setString(7,null);
pst.setString(8,column7);
pst.setString(9,null);
pst.setString(10,null);
pst.setString(11,null);
pst.setString(12,null);
pst.setString(13,null);
pst.execute();
}
}
JOptionPane.showMessageDialog(null, "data saved");
}
catch(Exception e) {
e.printStackTrace();
}
Method where columns are getting multiplied is as follow.:
private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
if(evt.getKeyCode()==KeyEvent.VK_TAB){
int r = jTable1.getRowCount();
for (int i = 0; i < r; i++){
if(jTable1.getValueAt(i,3)!=null && jTable1.getValueAt(i,4)!=null ){
int a=Integer.parseInt(jTable1.getValueAt(i,3).toString());
int b=Integer.parseInt(jTable1.getValueAt(i,4).toString());
int d= a*b;
jTable1.setValueAt(d,i,5);
}
}
}
}
new error is:
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 'Price,Sell_price,Quality,Color,Description,profit,Cost_price,idDealerItem) value' 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:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1364)
at Extra.jButton2ActionPerformed(Extra.java:331)
at Extra.access$300(Extra.java:14)
at Extra$4.actionPerformed(Extra.java:121)
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:3311)
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)
Please help..
thanks in advance..
You have ClassCastException because of your JTable store Integer values which can't be cast to String like next String column1= (String) jTable1.getValueAt(i,0);. Change that to:
String column1= jTable1.getValueAt(i,0) == null ? "" : jTable1.getValueAt(i,0).toString();
You can't cast Integer to String, but you can write
Integer integer = new Integer(4);
String string = integer.toString(); // "4"

java.sql.SQLException: There is no column named:

I am using Netbeans IDE to develop a desktop Java App. I have an embedded Derby database which has a few tables. All of the tables are in the APP schema. The problem I am having is that a query I am making to the DB is coming back with the above exception. I know the table exists and if I run the exact same SQL query from inside the editor it functions as expected. I am looking for help with the problem but more so looking for help as to how you would troubleshoot a problem like this. I have attached some code and the stack trace. Please don't hesitate to ask for more info if it will help
public static void doKeywordListDisplayLogic() {
try {
SimpleQuery keywordIdQuery = new SimpleQuery();
keywordIdQuery.setSelectExpressionList("*");
keywordIdQuery.setDataSource("KEYWORD_LOOKUP");
keywordIdQuery.setWherePredicates("SDS_NUMBER");
keywordIdQuery.setComparisonOperator("=");
keywordIdQuery.setQueryPredicate(selectedSdsNumber);//selectedSdsNumber comes from a previous query and table row selection.
keywordIdQuery.simpleQuery();
String keyId = rs.getString("KEY_ID");
System.out.println(keyId);
} catch (Exception e) {
e.printStackTrace();
}
}
public void simpleQuery() {
try {
conn = DbCommunication.JavaConnect.ConnectDb();
String dbQuery = "SELECT ".concat(selectExpressionList).concat(" FROM ").concat(dataSource).concat(" WHERE ").concat(wherePredicates).concat(" ").concat(comparisonOperator).concat(" ").concat(queryPredicate);
System.out.println(dbQuery);
PreparedStatement pst = conn.prepareStatement(dbQuery);
rs = pst.executeQuery();
} catch (Exception e) {
e.printStackTrace(System.out);
JOptionPane.showMessageDialog(null, e);
}
}
***THE SELECT SQL QUERY BELOW IS THE FINAL STRING SUBMITTED TO THE DB***
SELECT * FROM LOCATION_LOOKUP WHERE SDS_NUMBER = 998
java.sql.SQLException: There is no column named: KEY_ID.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.ResultSet.getString(Unknown Source)
at RecordHandling.PopulateRecord.doKeywordListDisplayLogic(PopulateRecord.java:264)
at RecordHandling.PopulateRecord.doRowRetrieval(PopulateRecord.java:202)
at Gui.mainFrame.jButton7ActionPerformed(mainFrame.java:717)
at Gui.mainFrame.access$400(mainFrame.java:25)
at Gui.mainFrame$5.actionPerformed(mainFrame.java:232)
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)
Caused by: org.apache.derby.client.am.SqlException: There is no column named: KEY_ID.
at org.apache.derby.client.am.ColumnMetaData.findColumnX(Unknown Source)
at org.apache.derby.client.am.ResultSet.findColumnX(Unknown Source)
... 42 more
There is no column named: KEY_ID
Instead of retrieving using column name,
String keyId = rs.getString("KEY_ID");
try retrieve using index:
String keyId = rs.getString(indexOfKeyId);//if key_id is at column 2 in table, then rs.getString(2)
To troubleshoot, display all value from rs, and see if your desired output(key id) is there.

strange exception in java sql update

I'm trying to edit a sql table using a java program. However, i get the following exception(includes stacktrace). The exception is a verystrange one, never encountered it before.
com.mysql.jdbc.exceptions.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 '[,20,375,298x28,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmen' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
at transaction.EditClientTrans.clEditTrActionPerformed(EditClientTrans.java:259)
at transaction.EditClientTrans.access$300(EditClientTrans.java:23)
at transaction.EditClientTrans$4.actionPerformed(EditClientTrans.java:121)
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: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.pumpEventsForFilter(EventDispatchThread.java:154)
at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:182)
at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:221)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:219)
at java.awt.Dialog.show(Dialog.java:1082)
at java.awt.Component.show(Component.java:1651)
at java.awt.Component.setVisible(Component.java:1603)
at java.awt.Window.setVisible(Window.java:1014)
at java.awt.Dialog.setVisible(Dialog.java:1005)
at transaction.EditClientTrans$5.run(EditClientTrans.java:310)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
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.awt.EventQueue.dispatchEvent(EventQueue.java:703)
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 is the code:
private void clEditTrActionPerformed(java.awt.event.ActionEvent evt) {
try {
long bal = amtnum - downp;
payoption = (String) payOpt.getSelectedItem();
if(bal==0)
pstat = "Fully Paid";
else if((bal>0)&&(bal<amtnum))
pstat = "Partially Paid";
else if(bal==amtnum)
pstat = "Unpaid";
String sql = "update kusinanikambal.ctrans "
+ "set clname = '"+cliName.getText()
+"', contdet = '"+cliCNo.getText()
+"', date = '"+Date.getText()
+"', amt = "+amtnum
+", paidamt= "+down
+", bal = "+bal
+", paymethod = '"+payoption+"'"
+", paystat = '"+pstat+"'"
+ "where transno = "+ transNo.getText();
PreparedStatement pst = conn.prepareStatement(sql);
int rs = pst.executeUpdate();
JOptionPane.showMessageDialog(null,"Edit Successful");
super.setVisible(false);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null,ex);
ex.printStackTrace();
}
}
You've appended an Object (in this, some kind of component) as part of your query, which has used the object's toString method as the result. Can't tell which one as you've not provided that much information.
You should also consider using PreparedStatements instead of appending the query String together...

Restore java derby database backup

I am new to derby database and i am trying to
restore a backup of a derby database
with codes from the apache tutorials.
But it gives an error that no suitable driver found for derby ,
anytime I run the code.
below are the code snippets:
public void restoreDatabase()
{
try {
String dbURL = "jdbc:derby:/localhost:1527/NSS_DB;restoreFrom="
+ "c:/mybackups/2013-03-31/NSS_DB";
Connection conn = DriverManager.getConnection(dbURL);
conn.close();
System.out.println("restore wassuccessful ");
} catch (SQLException ex) {
Logger.getLogger(MyJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
After modifying the code I do not get the error any longer but the backup does not get restored. below is a modification of the above code. this is the second code snippet.
public void restoreDatabase() throws ClassNotFoundException
{
try {
String dbURL = "jdbc:derby://localhost:1527/NSS_DB;restoreFrom="
+ "c:/mybackups/2013-03-31/NSS_DB";
Connection conn = null;
Properties props = new Properties();
props.put("user", uName);
props.put("password", uPass);
conn = DriverManager.getConnection(dbURL, props);
conn.commit();
} catch (SQLException ex) {
Logger.getLogger(MyJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
the only addition i suppose is the use of the java.util.Properties object.
This error message from the Netbeans console was produced by the frst code snippets
Apr 01, 2013 8:39:25 PM my.gnssregsitry.DBSearch restoreDatabase
SEVERE: null
java.sql.SQLException: No suitable driver found for jdbc:derby:/localhost:1527/NSS_DB;restoreFrom=c:/mybackups/2013-03-31/NSS_DB
at java.sql.DriverManager.getConnection(DriverManager.java:604)
at java.sql.DriverManager.getConnection(DriverManager.java:243)
at my.gnssregsitry.DBSearch.restoreDatabase(DBSearch.java:242)
at my.gnssregsitry.MyJFrame.jButton2ActionPerformed(MyJFrame.java:929)
at my.gnssregsitry.MyJFrame.access$1900(MyJFrame.java:41)
at my.gnssregsitry.MyJFrame$20.actionPerformed(MyJFrame.java:591)
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:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
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:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Looks like there is more to be done that the codes am implementing
any siggestions
The no-suitable-driver issue means that: (a) you have to put derbyclient.jar into your classpath, and (b) you probably have to call Class.forName( "org.apache.derby.jdbc.ClientDriver" );
Also, there should be two slashes in front of "localhost"; it should be "jdbc:derby://localhost:1527/NSS_DB;restoreFrom=...", etc.

Categories

Resources