java exception [message #91005] |
Tue, 05 February 2002 12:49 |
sokeh
Messages: 77 Registered: August 2000
|
Member |
|
|
Hello Java gurus!
I am an absolute newbie in this area and my first java problem is not going very well. I have written the below code but I am running into an exception -
Public class FirstCode must be defined in a file called "FirstCode.java". Please take a look at the code and tell me what is wrong and how to fix it.
Thanks in advance!
import java.sql.*;
public class FirstCode {
static String theJdbcDriverClass = "sun.jdbc.odbc.JdbcOdbcDriver";
static String theDatabaseUrl = "jdbc:odbc:EmpDB";
static String theUserName = "scott";
static String thePassword = "tiger";
public static void main( String args[[]] ) {
Connection myConnection = null;
Statement myStatement = null;
ResultSet myResult = null;
try {
// Load the JDBC driver
Class.forName( theJdbcDriverClass );
// Get a connection to the database
myConnection = DriverManager.getConnection(
theDatabaseUrl,
theUserName, thePassword );
// Create a statement to do some work
myStatement = myConnection.createStatement();
// execute a query and get a set of results
myResult = myStatement.executeQuery("SELECT * FROM EMP");
while( myResult.next() ) {
System.out.println("Name: " + myResult.getString("ENAME") );
System.out.println("Job: " + myResult.getString("JOB") );
System.out.println("");
}
}
catch( SQLException sqlError ) {
int cnt=1;
while( sqlError != null ) {
System.out.println( "Error " + cnt + ": " );
System.out.println( " SQLState: " + sqlError.getSQLState() );
System.out.println( " Message: " + sqlError.getMessage() );
System.out.println( " Vendor: " + sqlError.getErrorCode() );
System.out.println( "" );
sqlError = sqlError.getNextException ();
}
}
catch( Exception e ) {
e.printStackTrace();
}
finally {
try {
if( myResult != null ) { myResult.close(); }
if( myStatement != null ) { myStatement.close(); }
if( myConnection != null ) { myConnection.close(); }
}
catch( Exception e ) { e.printStackTrace(); }
}
}
}
|
|
|
|
|