3 Replies Latest reply on Jul 9, 2008 4:18 AM by 843859

    createSQLException

    843859

      I'm able to connect to the db, and have previously gotten console output read from the MySQL db "cars". At the moment I'm perplexed as to what's causing the exception, though.

      thufir@arrakis:~$
      thufir@arrakis:~$ mysql -u java -p
      Enter password:
      Welcome to the MySQL monitor. Commands end with ; or \g.
      Your MySQL connection id is 31
      Server version: 5.0.51a-3ubuntu5.1 (Ubuntu)

      Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

      mysql>
      mysql> USE cars;
      Reading table information for completion of table and column names
      You can turn off this feature to get a quicker startup with -A

      Database changed
      mysql>
      mysql> SELECT * FROM cars;
      ---------------------------+
      | carid | name | vendor | type |
      ---------------------------+
      | a | b | c | d |
      | aa | b | c | d |
      | aaa | b | c | d |
      ---------------------------+
      3 rows in set (0.01 sec)

      mysql>
      mysql> quit
      Bye
      thufir@arrakis:~$
      thufir@arrakis:~$ cat NetBeansProjects/mysql/src/a00720398/model/CarModel.java
      package a00720398.model;

      import a00720398.controller.*;
      import java.sql.*;
      import java.util.*;
      import java.util.logging.*;
      import javax.swing.*;
      import javax.swing.table.*;

      @SuppressWarnings({"serial"})
      public class CarModel extends DefaultTableModel {

      private static final TableModel INSTANCE = new CarModel();
      Connection connection = CarController.getInstance().getConnection();

      private CarModel() {
      try {
      init();
      } catch (SQLException ex) {
      Logger.getLogger(CarModel.class.getName()).log(Level.SEVERE, null, ex);
      }
      }

      public static TableModel getInstance() {
      return INSTANCE;
      }

      public void init() throws SQLException {
      //columnIdentifiers = convertToVector(columnIdentifiersString);
      //dataVector = convertToVector(dataVectorString);
      Statement statement = connection.createStatement();
      ResultSet resultSet = statement.executeQuery("select * from cars");
      Car car = getCar(resultSet);
      System.out.println(car);
      }

      private Car getCar(ResultSet resultSet) throws SQLException {
      Car car = new Car();

      /*
      * how do I find out why this causes:

      init:
      deps-jar:
      Compiling 1 source file to /home/thufir/NetBeansProjects/mysql/build/classes
      compile:
      run:
      8-Jul-2008 6:01:08 AM a00720398.model.CarModel <init>
      SEVERE: null
      java.sql.SQLException
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
      at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
      at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:817)
      at com.mysql.jdbc.ResultSetImpl.getObject(ResultSetImpl.java:4703)
      at a00720398.model.CarModel.getCar(CarModel.java:74)
      at a00720398.model.CarModel.init(CarModel.java:33)
      at a00720398.model.CarModel.<init>(CarModel.java:18)
      at a00720398.model.CarModel.<clinit>(CarModel.java:13)
      at a00720398.view.CarView.initComponents(CarView.java:31)
      at a00720398.view.CarView.<init>(CarView.java:14)
      at a00720398.view.CarView$1.run(CarView.java:65)
      at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
      BUILD SUCCESSFUL (total time: 6 seconds)
      *
      * */
      Object obj = resultSet.getObject(1);

      //car.setCarID(carid);
      System.out.println(car);
      return car;
      }
      }

      thufir@arrakis:~$


      thanks,

      Thufir

        • 1. Re: createSQLException
          843859
          You forget to set the cursor on the resultset using ResultSet#next(). Read the Sun JDBC tutorial.

          That the exception didn't tell it you it the correct way is likely a bug in MySQL JDBC driver. Report it there, or get the newer version where it is been fixed. At least with the current Connector/J 5.0 you should get this:
          Caused by: java.sql.SQLException: Before start of result set
               at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
               at com.mysql.jdbc.ResultSet.checkRowPos(ResultSet.java:692)
          In the future, please wrap code in CODE tags. You can use the CODE button in the message editor toolbar to get it.
          • 2. Re: createSQLException
            843859
            package a00720398.model;
            
            import a00720398.controller.*;
            import java.sql.*;
            import java.util.*;
            import java.util.logging.*;
            import javax.swing.*;
            import javax.swing.table.*;
            
            @SuppressWarnings({"serial", "unchecked"})
            public class CarModel extends DefaultTableModel {
            
                private static final TableModel INSTANCE = new CarModel();
                Connection connection = CarController.getInstance().getConnection();
            
                private CarModel() {
                    try {
                        init();
                    } catch (SQLException ex) {
                        Logger.getLogger(CarModel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            
                public static TableModel getInstance() {
                    return INSTANCE;
                }
            
                public void init() throws SQLException {
                    //columnIdentifiers = convertToVector(columnIdentifiersString);
                    //dataVector = convertToVector(dataVectorString);
                    ResultSet cars = getCars();
                    while (cars.next()) {
                        Car car = getCar(cars);
                        dataVector.add(car);
                    }
                }
            
                private ResultSet getCars() throws SQLException {
                    Statement statement = connection.createStatement();
                    ResultSet cars = statement.executeQuery("select * from cars");
                    return cars;
                }
            
                private Car getCar(ResultSet cars) throws SQLException {
            
                    String carid = cars.getString("carid");
                    Car car = new Car();
                    car.setCarID(carid);
                    return car;
                }
            }
            Ah, thanks, that helped. I'm just trying to figure out how add the data into the dataVector so that the JList which uses CarModel as its model displays the data.
            • 3. Re: createSQLException
              843859
              I suggest reading up on the section on databases from http://www.javapractices.com/home/HomeAction.do
              to learn how to structure the code correctly.