1

I have a database table consisting of 6 columns and 6 rows. I want to access all 6 column values of the 2nd row, but I'm unable to figure out why I'm getting this exception.

public void send()
{
int row=2;
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection cn=DriverManager.getConnection("jdbc:odbc:DSN2");
    Statement st=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    ResultSet rs=st.executeQuery("select *from Table1");
    rs.absolute(row);
    System.out.println(rs.getInt(1)+"\t"+rs.getInt(2)+"\t"+rs.getInt(3)+"\t"+rs.getInt(4)+"\t"+rs.getInt(5)+"\t"+rs.getInt(6));

    }

java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1958) at sun.jdbc.odbc.JdbcOdbcResultSet.reWordAsCountQuery(JdbcOdbcR‌​esultSet. java:6557) at sun.jdbc.odbc.JdbcOdbcResultSet.calculateRowCount(JdbcOdbcRe‌​sultSet.j ava:6350) at sun.jdbc.odbc.JdbcOdbcResultSet.initialize(JdbcOdbcResultSet‌​.java:154) at sun.jdbc.odbc.JdbcOdbcStatement.getResultSet(JdbcOdbcStateme‌​nt.java:4 23) at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStateme‌​nt.java:2 53)at Router.send(Router1.java:37)at Router1.main(Router1.java:54)

5
  • 1
    Can you please post the exception stack trace? Which line is throwing exception? Sep 19, 2013 at 15:13
  • the only line I see that can throw that exceptino would be his System.out.println() statement which attempts to read data from the row's columns...
    – SnakeDoc
    Sep 19, 2013 at 15:34
  • java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1958) at sun.jdbc.odbc.JdbcOdbcResultSet.reWordAsCountQuery(JdbcOdbcResultSet. java:6557) at sun.jdbc.odbc.JdbcOdbcResultSet.calculateRowCount(JdbcOdbcResultSet.j ava:6350) at sun.jdbc.odbc.JdbcOdbcResultSet.initialize(JdbcOdbcResultSet.java:154) at sun.jdbc.odbc.JdbcOdbcStatement.getResultSet(JdbcOdbcStatement.java:4 23) at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStatement.java:2 53)at Router.send(Router1.java:37)at Router1.main(Router1.java:54)
    – Intriguing
    Sep 19, 2013 at 15:37
  • @SnakeDoc S the prinln statemnt reads the columns values i have 6 columns so Why the exception
    – Intriguing
    Sep 19, 2013 at 15:39
  • @Intriguing see my latest update for some code to try
    – SnakeDoc
    Sep 19, 2013 at 15:58

1 Answer 1

2

Your SQL statement is not valid, likely causing the issue (assuming what you pasted in your OP is what you are really using)

select *from Table1

should be

select * from Table1

The * needs to have a space between it and from.

Also, it's worth mentioning that it's best practice with SQL to make your keywords all caps... so your statement re-written as such:

SELECT * FROM Table1

Also best practice to have your tables not begin with capital letters, or have caps in them at all... but this would have to be delt with when creating the database.

Also, just as a learning note:

Your current method will open a new database connection every time you run this method... which will cause significant performance bottlenecks/problems if this method is called often. Instead, it's much better to open the connection as your program starts up, then you can use that same connection over and over to run statements, then when your program is terminating, have it close the database connection. This removes a lot of network activity/overhead in communicating with your database and will yield much better performance and scalability of your code.

Also, it's worth mentioning, look into PreparedStatement. These will allow you to create your statement before you need them, and then just re-use the same statement over and over, which will yield even greater performance, as well as provide more security/safety if your queries will use user-input at some point (PreparedStatement automatically escapes input strings to avoid SQL-Injection attacks).

UPDATE --

Some code to try:

public void send() {

    try {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection cn=DriverManager.getConnection("jdbc:odbc:DSN2");
        Statement st=cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
        ResultSet rs=st.executeQuery("SELECT * FROM Table1");

        // debug section
        ResultSetMetaData rsmd = rs.getMetaData();
        System.out.println("DEBUG: Number of columns = " + rsmd.getColumnCount());
        // end debug

        while(rs.next()) {
            System.out.println(rs.getInt(1)+"\t"+rs.getInt(2)+"\t"+rs.getInt(3)+"\t"+rs.getInt(4)+"\t"+rs.getInt(5)+"\t"+rs.getInt(6));
        }

    } catch(Exception e) {
        e.printStackTrace();
    }
}

This will print a line telling us how many columns are in your ResultSet

6
  • Your Suggestions are Warmly Welcomed.I did accordingly as u said but still the error persists
    – Intriguing
    Sep 19, 2013 at 15:29
  • perhaps there really aren't that many columns in the ResultSet. -- instead of a SELECT * FROM query, perhaps try selecting individual columns-- and the JDBC driver will throw a different exception if you try to SELECT a column that does not exist in the table. So, like: SELECT columnName1, columnName2, columnName3, columnName4 FROM Table1; ... etc.
    – SnakeDoc
    Sep 19, 2013 at 15:31
  • 1
    also, instead of trying rs.absolute(2), intead try just advancing the cursor by one row with rs.next()... .next() will return false if there is no more rows... and trying to read from a row that does not exist will throw an exception similar to what you are seeing.
    – SnakeDoc
    Sep 19, 2013 at 15:33
  • @Intriguing try the latest update in my post. I modified your code a little bit, fixed the invalid sql statement, and removed the .absolute() call and replaced with the .next(). Also added a debug section that will tell us how many columns are being returned by your query, hopefully before the exception is thrown and it crashes out. - may lend some insight as to what's going wrong.
    – SnakeDoc
    Sep 19, 2013 at 16:01
  • YEAH Problem solved.Accordingly i made select statement and got output.Kudos to You.
    – Intriguing
    Sep 19, 2013 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.