Results 1 to 2 of 2
  1. #1
    Dublin_kopite is offline Member
    Join Date
    Nov 2008
    Posts
    1
    Rep Power
    0

    Default SQL database issue

    Hey,

    I'm a newbie to java...

    I have connected to a database and updated it using a class called datahandler...

    I'm having problems when i pass an SQL query to the datahandler and try return a result..


    Java Code:
    String SQLstats = "Select MAX(CustomerID) FROM customer";
    String[] cID = new String[20];
    cID[1] = DataHandler.getCID(SQLstats);

    and the datahandler is

    Java Code:
    public class DataHandler
    {
        // DB details
        private static String dbURL = "jdbc:odbc:guiDB";
        private static java.sql.Connection con;
        private static java.sql.Statement stm;
        private static java.sql.ResultSet rs;
        private static java.sql.ResultSetMetaData rsMeta;
        private static int columnCount;
     
          public static String getCID(String sqlQuery1) {
           String cid[] = new String[5];
           int i = 0;
    
           
    
                try{
                    
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                    con = DriverManager.getConnection(dbURL, "","");
                    stm = con.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_UPDATABLE);
                    rs = stm.executeQuery(sqlQuery1);
                    rsMeta = rs.getMetaData();
                    columnCount = rsMeta.getColumnCount();
                    
                    int i = 0;
                    while(rs.next()) {
                    cid [1] = rs.getString(1);
                    i++;  }
            
                    
                    }
                catch ( ClassNotFoundException cnfex ) {
                  System.err.println("Failed to load JDBC/ODBC driver." );
                  cnfex.printStackTrace();
                  System.exit( 1 );  // terminate program
                }
                catch ( java.sql.SQLException sqlex ) {
                  System.err.println( sqlex );
                  sqlex.printStackTrace();
                }
                catch ( Exception ex ) {
                  System.err.println( ex );
                  ex.printStackTrace();
                }
                
                return cid[1];
    	}
    I'm getting errors such as
    java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

    I basically want to pass in a string, query the database and return the value.

    Any help would be fantastic!

  2. #2
    Jasonre is offline Member
    Join Date
    Jan 2009
    Posts
    40
    Rep Power
    0

    Default

    did you try to prepare your statement yet?

    stm = con.createStatement(java.sql.ResultSet.TYPE_SCROLL _INSENSITIVE, java.sql.ResultSet.CONCUR_UPDATABLE);

    do
    con.prepareStatement(sqlQuery1, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)
    see if that helps when reading.

Similar Threads

  1. Threading issue
    By Eku in forum New To Java
    Replies: 2
    Last Post: 09-18-2008, 10:47 AM
  2. NullPointerException issue
    By fritz1474 in forum AWT / Swing
    Replies: 2
    Last Post: 09-03-2008, 05:21 PM
  3. Compile Issue
    By CrazyShells Slam in forum New To Java
    Replies: 4
    Last Post: 05-14-2008, 08:51 PM
  4. Try Catch issue
    By curtis_fraser in forum Advanced Java
    Replies: 2
    Last Post: 12-13-2007, 11:04 PM
  5. Issue
    By chaitu444 in forum New To Java
    Replies: 2
    Last Post: 11-06-2007, 07:49 PM

Posting Permissions

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