I have succesfully set up a Connection and a System DSN and conected to an SQL Server database. I have succesfully created a statement returning RecordSet and executed it.
The problem is that when I try and get the data from it, it appears to be empty and gives me '[Microsoft][ODBC Driver Manager] Invalid cursor state' . My code is :
package pacJDBC;
import java.sql.*;
//This class explores the concepts of JDBC and Database access
//using Java
public class DBAccess
{
///constructors
public DBAccess()
{
}
public static void main(String[] args)
{
DBAccess dbaccess = new DBAccess();
dbaccess.execute();
}
private Connection connect()
{
Connection con = null;
try
{
//JDBc driver (comes with J2 SDK 1.4)
String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
//JDBC url to use with JDBC driver. Uses a System DSBN (test1)
String url = "jdbc:odbc:test1";
//database login credentials
String username = "sa";
String password = "";
// Load the JDBC driver
Class.forName(driverName);
// Create a connection to the database
con = DriverManager.getConnection(url, username, password);
}
catch (ClassNotFoundException e)
{
// Could not find the database driver
System.out.println("Could not find the driver");
}
catch (SQLException e)
{
// Could not connect to the database
System.out.println("Could not connect to the database");
}
return con;
}
private void execute()
{
String strSQL;
Connection con = null;
Statement stmt;
ResultSet rst;
strSQL = "Select * from Categories";
con = connect();
try
{
//create a static, scrollable, insensitive statement
stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rst = stmt.executeQuery(strSQL);
System.out.println(rst.getString("CategoryName"));
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
}