Results 1 to 4 of 4
  1. #1
    SeanC is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Wink What exactly am I doing wrong?

    I'm trying to retrieve data from a database written in MySQL, however there seems to be a problem with my code. I'm completely new to SQL and JDBC, so it's probably quite a stupid mistake. Can anyone please point me in the right direction?

    Java Code:
    try {
                Class.forName(driver);
                con = DriverManager.getConnection(url, userName, password);
                try {
                    Statement st = con.createStatement();
                    ResultSet executeQuery = st.executeQuery(queryCode);
                    System.out.println(executeQuery.getString(1));
                } catch (Exception e) {
                    System.out.println("Code not executed");
                }
            } catch (Exception e) {
                System.out.println("Not connected");
            }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by SeanC View Post
    Java Code:
    catch (Exception e) {
                    System.out.println("Code not executed");
                }
            } catch (Exception e) {
                System.out.println("Not connected");
            }
    Do'nt hide exceptions, most certainly not while you're developing; put an 'e.printStackTrace()' statement in both catch clauses and see what went wrong.

    kind regards,

    Jos

  3. #3
    SeanC is offline Member
    Join Date
    Aug 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Do'nt hide exceptions, most certainly not while you're developing; put an 'e.printStackTrace()' statement in both catch clauses and see what went wrong.

    kind regards,

    Jos
    After doing that, this is the error I got:

    [CODE]java.sql.SQLException
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:926)
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSet Impl.java:815)
    at com.mysql.jdbc.ResultSetImpl.getStringInternal(Res ultSetImpl.java:5528)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetIm pl.java:5448)
    at employeemanagementsystem.databaseConnectivity.read FromDatabase(databaseConnectivity.java:57)
    at employeemanagementsystem.addNewEmployee.jButton1Ac tionPerformed(addNewEmployee.java:785)
    at employeemanagementsystem.addNewEmployee.access$150 0(addNewEmployee.java:17)
    at employeemanagementsystem.addNewEmployee$16.actionP erformed(addNewEmployee.java:241)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6263)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:602 8)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122) [\CODE]

    Also, the 'queryCode' String is : "SELECT * FROM administrator"

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Didn't it give anymore details other than simply "java.sql.SQLException" and the trace?
    What driver are you using?

    Anyway, that aside, the result set returned is a cursor that starts before the first result. So you have to call next() to get the result set pointing at the first entry. This call returns a boolean by the way which tells you whether there is a next(). You only want to be getting stuff from an existing entry, ie where next() returns true.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. what am i doing wrong here?
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-21-2010, 04:04 PM
  3. where did i go wrong?
    By Sri Vidhya in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 03:11 PM
  4. What did i do wrong
    By jpnym15 in forum New To Java
    Replies: 8
    Last Post: 11-17-2008, 10:07 AM
  5. Can someone tell me what I did wrong??
    By booter4429 in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 08:35 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •