I need to query a database on a server. As a first step, I have written the following Java code to just make sure I can access the database. I thought that the Try-Catch blocks would take care of the exception problem, but apparently they do not because when I try to compile the code, I get two errors that are listed below the Java code. Can someone give me a corrected code that will compile?
-------------------------------------------------------------
Java Code:
import java.sql.*;
import java.math.*;
public class NewProfileTest {
public static void main(String[] args) {
Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection
("jdbc:oracle:thin:@10.25.83.19:1512:PROFILE","newprofi1e","newprofile");
Statement stmt;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection
("jdbc:oracle:thin:@10.25.83.19:1512:PROFILE","newprofile","newprofile");
stmt = con.createStatement();
stmt.close();
con. close();
}
catch(SQLException ex) {
System.err.println("sQLException: " + ex.getMessage());
}
}
}
------------------------------------------------------------------
Compile Error Messages
C:\Documents and Settings\J. Seader\Desktop\NewProfileTest.java:8: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
Class.forName ("oracle.jdbc.driver.OracleDriver");
^
C:\Documents and Settings\J. Seader\Desktop\NewProfileTest.java:11: unreported exception java.sql.SQLException; must be caught or declared to be thrown
("jdbc:oracle:thin:@10.25.83.19:1512:PROFILE","newprofi1e","newprofile");
^
2 errors
Tool completed with exit code 1
--------------------------------------------------------
Thank you,
Old FORTRAN programmer