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

Recommended Answers

All 6 Replies

there are two lines where you call that method; you caught the exception for the second one, but not the first

I'm not clear as to what is the first method. I thought that I had a Try-Catch block for each method. Can you tell me how to modify my code to make it compile.
Thank you.

there are two lines of

Class.forName ("oracle.jdbc.driver.OracleDriver");

in your code

Thank you for the suggestion.
I commented out Line 8 and that eliminated the two compiling errors.
However, I now have another compiling error:

C:\Documents and Settings\J. Seader\Desktop\Java Applications\NewProfileTest.java:24: unreported exception java.sql.SQLException; must be caught or declared to be thrown
("jdbc:oracle:thin:@10.52.38.91:1521:PROFILE","newprofi1e","newprofile");
^
1 error

Tool completed with exit code 1

What else do I need to do?
Thank you

You can do it like this

import java.sql.*;
import java.math.*;

public class NewProfileTest 
{

	public static void main(String[] args) 
	{	
		Connection con = null;
	
		Statement stmt;
		
		try 
		{
			Class.forName ("oracle.jdbc.driver.OracleDriver");
			con = DriverManager.getConnection
				("jdbc:oracle:thin:@10.25.83.19:1512:PROFILE","newprofile","newprofile");
			stmt = con.createStatement();
			stmt.close();
			con. close();
		}
		catch(java.lang.ClassNotFoundException e) 
		{
			System.err.print("ClassNotFoundException: ");
			System.err.println(e.getMessage());
		}
		catch(SQLException ex)
		{
			System.err.println("sQLException: " + ex.getMessage());
		}
	}
}

or leave it as you have it and remove line 8 Class.forName ("oracle.jdbc.driver.OracleDriver"); plus line 10 onli declaration Connection con =null;

Thank you.
The revised code compiles successfully.
I will now try queries to the database.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.