• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HELP! Connection to MS Access DB Trouble

 
Kassi Hill
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help. I am trying to connect to a Access database class. I know there is some test data in the table that I am trying to select from but when I run the program I get a error message that says: SQLE NOT EXECUTED: No Row Count Produced.
I have put my code below hoping maybe one of you more experienced ranchers can he me out. Thank you.
package testbanktesting;
/**
* Testing Classes
* @version 1.0
*/
import java.sql.*;
import java.io.*;
public class DataAccess {
private final String url = "jdbc dbc:TestBank";
private final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
private Connection conn;
private ResultSet rs;
private Statement stmt;
public DataAccess() {
}
private void openConnection ()throws ClassNotFoundException{
//this is the method used to connect to the database using hte JDBC dbc bridge
String sql = "SELECT * FROM Question";
try{
//load the driver
Class.forName(driver);
//Connect
conn = DriverManager.getConnection(url,"","");
stmt = conn.createStatement();//Create Statement Object
stmt.executeUpdate(sql);
// boolean returnsResultSet = stmt.execute(sql);
while (rs.next()); //get informatoin and load into variables
int qID = (rs.getInt("qID"));
String qText = (rs.getString("qText"));
// boolean qType = (rs.getBoolean("qType"));
String qType =(rs.getString("qType"));
int qCorrect = (rs.getInt("qCorrect"));;//chek for results and add them to result set.
System.out.println(qID);
System.out.println(qText);
System.out.println(qType);
System.out.println(qCorrect);

}catch(SQLException sqle) {
System.err.println("SQL NOT Executed: " + sqle);
}catch(java.lang.Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
DataAccess dataAccess1 = new DataAccess();
try{
dataAccess1.openConnection();
}catch(Exception sqle) {
System.err.println("SQL NOT Executed: " + sqle);
}
}
}
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Two things in two lines. First, you aren't executing a SQL UPDATE, so you should use Statement.executeQuery. Second, where do you set the variable rs?
 
Kassi Hill
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had been setting the variable rs as a member variable at the top:
private ResultSet rs
I did this because when I finall get this to work, it will be the "DataAccess" class for a larger program that will be using a three tiered approach, where other classes will call for the information they need so the class will need to return an result set, which is way I had originally written it as a member variable.
I am now getting an invalid curser position message. Java really hates me.
Thank you for your help!!
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kassi Hill:

I am now getting an invalid curser position message. Java really hates me.

Java doesn't hate you. It is simply particular about how it is used.
Again, where do you set the variable rs to a usable value?
 
Kassi Hill
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry for going on before. I am setting the ResultSet var as a private ResultSet rs in the start of the class, at the very top. Is it okay to define it here?? or now that I think about it, I am defining it at the top but not setting it any where, is this my problem??
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic