Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JDBC-ODBC connection

Status
Not open for further replies.

AcnA

Programmer
Apr 9, 2003
11
CY
Hello,
I am using the JDBC - ODBC Connection to access the data in my database designed in Access.
I have the following code:
boolean result = false;
String url = "";
Connection connection;
ResultSet rs;
boolean hasRecords = false;

// setup database connection
try {
url = "jdbc:eek:dbc:imsqti";
Class.forName(" sun.jdbc.odbc.JdbcOdbcDriver");
Connection connect = DriverManager.getConnection(url);
}
catch (ClassNotFoundException cnfex)
{ message('r'," Error Connecting to DataBase");
cnfex.printStackTrace(); }
catch (SQLException sqlex)
{ message('r'," Error Connecting to DataBase");
sqlex.printStackTrace(); }
catch ( Exception ex)
{ message('r'," Error Connecting to DataBase");
ex.printStackTrace(); }

try {
// first error goes here
Statement statement = connection.createStatement();
String query = "SELECT * FROM Teacher WHERE TUsername = '"+
logid + "' AND TPassword = '" + password + "'";
rs = statement.executeQuery(query);
statement.close();
}
catch (SQLException sqlex)
{ message('r'," Error Connecting to DataBase");
sqlex.printStackTrace(); }

try {
// second error goes here
hasRecords = rs.next();
if (hasRecords)
{ result = true;
message('g'," Welcome "+logid); }
else {
result = false;
message('r'," Invalid UserName or Password. Please try again"); }
}
catch (SQLException sqlex)
{ message('r'," Error Connecting to DataBase");
sqlex.printStackTrace(); }

return result;


But i get the error:
variable connection might not have been initialised
for the first bolded line, and
variable connection might not have been initialised
for the second bolded line..

Any ideas?
 
you mean these bolded lines that were not bolded?

Connection connection;
ResultSet rs;


try this
Code:
 Connection connection = null;
  ResultSet rs = null;

I suggest that if you don't understand the significance of initializing variables you need to re-read whatever beginner programming material you might be using. ;-)


-pete
I just can't seem to get back my IntelliSense
 

The 11th line has to be:
connection = DriverManager.getConnection(url);
If you make a new variable in a block, this will be dstroyed once you've left this block.

The reason for the error is that when an Exception should occur on the 11th line connection would still be null. The stack will be printed but the program still runs. The line where you wrote "// first error goess here" will result in a NullPointerException.

The solution is that you put everything in one try catch block. When an exception is thrown while making a connection, you don't have to try making SQL statements.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top