• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

returning a result set

 
justin wall
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble returning a resultset in this method. It says that my resutset might not have been initialized. Thanks,
Justin
public static ResultSet runQuery(String q)
{
ResultSet RS;
try
{

// The newInstance() call is a work around for some
// broken Java implementations

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

}
catch (Exception E)
{
System.err.println("Unable to load driver.");
E.printStackTrace();
}
try
{
// jdbc:mysql://[hostname][:port]/dbname[?param1=value1][¶m2=value2]...
Connection C = DriverManager.getConnection(
"jdbc:mysql://xx.xx.com/tparser?user=xxxl&password=xxxxi");

// Do something with the Connection
try
{
// Use some connection we've already created

Statement Stmt = C.createStatement();

RS = Stmt.executeQuery(q);



/* Clean up after ourselves*/
RS.close();
Stmt.close();
C.close();
}
catch (SQLException E)
{
System.out.println("SQLException: " + E.getMessage());
System.out.println("SQLState: " + E.getSQLState());
System.out.println("VendorError: " + E.getErrorCode());
}

}
catch(SQLException E)
{
System.out.println("SQLException: " + E.getMessage());
System.out.println("SQLState: " + E.getSQLState());
System.out.println("VendorError: " + E.getErrorCode());
}


return RS;

}
------------------
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try ResultSet RS = null;
Bosun
 
justin wall
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! That was it. In Java, do you have to initalize every variable with a value when you declare it?
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables have to be initialized before they are used. This is local because it's declared within a method.

Bosun
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic