HI to all! I am facing some exceptions in my database connectivity programs. I am using jdk7, window7 and Ms Access 2007. I am giving code of programs and exeption.
import java.sql.*;
public class ResultSetEx
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:personDSN";
Connection con = DriverManager.getConnection(url);
// creating prepare statement by passing
// sql and ResultSet's constants so that the
//ResultSet that will produce as a result of executing
// query will be scrollable and updatable
String sql = "SELECT *FROM persons";
PreparedStatement pStmt = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
// execute query
ResultSet rs = pStmt.executeQuery();
// moving cursor forward i.e first row
rs.next();
// printing column "name" value of current row(first)
System.out.println("moving cursor forward");
String name = rs.getString("name");
System.out.println(name);
// moving cursor forward i.e on to second row
rs.next();
// moving cursor backward i.e to first row
rs.previous();
// printing column "name" value for of current row(first)
System.out.println("moving cursor backward");
name = rs.getString("name");
System.out.println(name);
// close the connection
con.close();
}
catch(Exception sqlEx)
{
System.out.println(sqlEx);
}
}// end main
}// end class
Following exception is occuring in above program:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
program2:
import java.sql.*;
public class ResultSetEx2
{
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:personDSN";
Connection con = DriverManager.getConnection(url);
// creating prepare statement by passing
// sql and ResultSet's constants so that the
//ResultSet that will produce as a result of executing
// query will be scrollable and updatable
String sql = "SELECT *FROM persons";
PreparedStatement pStmt = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
// execute query
ResultSet rs = pStmt.executeQuery();
// moving cursor to second row
rs.absolute(2);
// update address column of 2 row in rs
rs.updateString("address", "model town");
// update the row in database
rs.updateRow();
// close the connection
con.close();
}
catch(Exception sqlEx)
{
System.out.println(sqlEx);
}
}// end main
}// end class
Following exception is occuring;
java.lang.StringIndexOutOfBoundsException: String index out of range: -1