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.
Related
I am having trouble in checking the username in the ms access database
java.sql.SQLException: No data found
here is the database connect
public class dbAccess
{
Connection conn;
ResultSet rs;
Statement s;
PreparedStatement ps;
public void doConnect()
{
try
{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String login = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=E:\\File Kuliah\\Semester 7\\AOOP\\JavaApplication1\\build\\classes\\Database1.accdb";
conn = DriverManager.getConnection(login);
System.out.println("Connected");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
and here is the code that has the error
private void registerButtonActionPerformed(java.awt.event.ActionEvent evt) {
try
{
db.ps = db.conn.prepareStatement("SELECT * FROM DatabaseAOOP");
db.rs = db.ps.executeQuery();
System.out.print("1");
while(db.rs.next())
{
if(userRegis.getText().equals(db.rs.getString("username")))
{
System.out.print("3");
JOptionPane.showMessageDialog(this, "Username is already exists !");
userRegis.setText("");
passRegis.setText("");
//db.rs=null;
//db.ps=null;
break;
}
else if(userRegis.getText().isEmpty())
{
JOptionPane.showMessageDialog(this, "Please input the name");
}
else if(!userRegis.getText().equals(db.rs.getString("username")))
{
JOptionPane.showMessageDialog(this, "Searching ...");
}
else
{
System.out.print("2");
db.ps = db.conn.prepareStatement("INSERT INTO DatabaseAOOP(username,password) VALUES(?,?)");
db.s = db.conn.createStatement();
db.ps.setString(1, userRegis.getText());
db.ps.setString(2, passRegis.getText());
db.ps.executeUpdate();
JOptionPane.showMessageDialog(this, "Thank you for registering !");
userRegis.setText("");
passRegis.setText("");
break;
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
System.out.println(ex);
}
}
here are the printstacktrace
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3914)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5697)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:353)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:410)
at Frame.registerButtonActionPerformed(Frame.java:277)
at Frame.access$600(Frame.java:4)
at Frame$7.actionPerformed(Frame.java:135)
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:3320)
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: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)
actually i just checked the database connected, but there is no data found
Immediately after entering the while(db.rs.next()) loop you call db.rs.getString("username"), then later in the loop you call db.rs.getString("username") again. It is the second invocation that throws the "No data found" exception.
In many cases JDBC will only let us call get... methods once per column for a given (current) row in the ResultSet. You should save the value to a String
String theUserName = db.rs.getString("username");
and then use the String variable for your (multiple) comparisons.
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.
public void displayPhoto() {
rs = null;
String displaySQL = "select * from images where USERNAME ='" + temp.getUsername() + "'";
try {
con = getDBConnection();
rs = st.executeQuery(displaySQL);
while (rs.next()) {
BufferedImage im = ImageIO.read(rs.getBinaryStream("IMAGES"));
displayPhoto.setIcon(new ImageIcon(im));
}
} catch (Exception e) {
e.printStackTrace();
}
}
I'm trying to display the image from database, the image stored as longblob. it says NULLPOINTERS, I dunno what's the problem
the table has 2 columns(USERNAME,IMAGES)
images = table name,
IMAGES = Column name,
displayPhoto = JLabel
can anyone help me?
thanks in advance<3
Here is the full error message
java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:228)
at MyProfile.displayPhoto(MyProfile.java:395)
at MyProfile.<init>(MyProfile.java:195)
at Login.actionPerformed(Login.java:93)
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)
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login", "root", "obierofelix");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from images");
rs.next();
BufferedImage im = ImageIO.read(rs.getBinaryStream("Image"));
BackGroundImage.setIcon(new ImageIcon(im));
} catch (Exception err) {
JOptionPane.showMessageDialog(this, err.getMessage());
}
Apply rs.next(); before the try and catch block to move the pointer to the first record in the database.
This question already has answers here:
ResultSet.getString(1) throws java.sql.SQLException: Invalid operation at current cursor position
(5 answers)
Closed 9 years ago.
This block of code gives me SQLException when i try to equal ResultSet to a String variable. can't i get a ResultSet part into a String Variable ? or any error in the code ?
And also it doesn't select any result from the database.
Pl help !
b1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e)
{
String unm = tf1.getText().toString();
String pwd = tf2.getPassword().toString();
try{
Connection con = null;
Statement stmt = null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String cn = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=E:/userlogin.accdb";
con = DriverManager.getConnection(cn,"","");
stmt = con.createStatement();
String sql = "select position from userlogin.users where users.username ='"+unm+"' and users.pwd ='"+pwd+"' ";
ResultSet rs;
rs = stmt.executeQuery(sql);
String position;
rs.next();
position = rs.getString(1);
if (position.equals("Salesman"))
{
frame.setVisible(false);
Salesman.main(null);
}
if (rs == null)
{
JOptionPane.showOptionDialog(null,
"Incorrect Username or Password !",
"Error !",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Ok", "Cancel"}, // this is the array
"default");
}
stmt.close();
}
catch (HeadlessException err) {
JOptionPane.showOptionDialog(null,
"HeadlessException !",
"Error !",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Ok", "Cancel"}, // this is the array
"default");
}
catch (ClassNotFoundException err) {
JOptionPane.showOptionDialog(null,
"ClassNotFoundException !",
"Error !",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Ok", "Cancel"}, // this is the array
"default");
}
catch (SQLException err) {
JOptionPane.showOptionDialog(null,
"SQLException !",
"Error !",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
new String[]{"Ok", "Cancel"}, // this is the array
"default");
}
}
});
what it says is :
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - unreported exception java.sql.SQLException; must be caught or declared to be thrown
at javaproject.JavaProject$1.actionPerformed(JavaProject.java:65)
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:713)
at java.awt.EventQueue.access$000(EventQueue.java:104)
at java.awt.EventQueue$3.run(EventQueue.java:672)
at java.awt.EventQueue$3.run(EventQueue.java:670)
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:686)
at java.awt.EventQueue$4.run(EventQueue.java:684)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:683)
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)
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - unreported exception java.sql.SQLException; must be caught or declared to be thrown
at javaproject.JavaProject$1.actionPerformed(JavaProject.java:65)
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:713)
at java.awt.EventQueue.access$000(EventQueue.java:104)
at java.awt.EventQueue$3.run(EventQueue.java:672)
at java.awt.EventQueue$3.run(EventQueue.java:670)
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:686)
at java.awt.EventQueue$4.run(EventQueue.java:684)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:683)
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)
BUILD SUCCESSFUL (total time: 5 seconds)
First, you don't "equal" something to a variable. You assign it. (Equal is not a verb!!)
Next, you have misread the exception. It says:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException:
Uncompilable source code - unreported exception java.sql.SQLException;
must be caught or declared to be thrown at ...
You are not "getting an SQLException". And SQLException is not "appearing".
What you actually have is a compilation error ... which you have instructed your IDE to ignore. Don't do that! Fix the compilation errors before you attempt to run your application.
And the compilation error is happening because you are calling a method or methods in your code that are declared as throwing the checked exception SQLException, but your code neither catches the exception or declares it as being thrown by the method in which this code occurs.
In this particular case, that method is actionPerformed ... which is required to conform to the signature specified in the ActionListener interface that your anonymous class is implementing. Therefore, you have to catch the exception.
Now I could give you some code to copy and paste into your program to fix it. But I think it would better if you read the Oracle Java Tutorial section on exceptions and exception handling so that you could actually understand what is going on here, and understand what you need to do to fix it.
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();