Link to home
Start Free TrialLog in
Avatar of gdlp2004
gdlp2004

asked on

SQL Exception Error

Can anyone inform me what this error means.

unreported exception: java.sql.SQLException; must be caught or declared to be thrown
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

you are using some SQL method which throws a java.sql.SQLException and you are not catching it..
post your code and we shd be able to help you out
Avatar of gdlp2004
gdlp2004

ASKER

<%!
public String doSomthing() {
Connection conn2 = null;
 try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn2 = DriverManager.getConnection(
        "jdbc:oracle:thin:@divodb002:1521:salest01",
         "public_gdlp",
        "public");
 
      }
 
catch(SQLException e) {
   System.out.println("SQLException: " + e.getMessage() + "<BR>");
   while((e = e.getNextException()) != null)
      System.out.println(e.getMessage() + "<BR>");
                      }
ResultSet r5;
String strMyString = "";
String udc = "Select udc FROM public_gdlp.udc where treeid = 2 and udcseq = 1";
 
  Statement stmt2 = conn2.createStatement();    
  r5 = stmt2.executeQuery(udc);
 while (r5.next()) {
strMyString = r5.getString("countless");
}
return strMyString;
}
%>

Here is the code that is giving me the error
stmt.executeQuery will also throw the exception...
chage the code to be..

<%!
public String doSomthing() {
Connection conn2 = null;
String strMyString = "";
 try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn2 = DriverManager.getConnection(
        "jdbc:oracle:thin:@divodb002:1521:salest01",
         "public_gdlp",
        "public");
 
     ResultSet r5;
String udc = "Select udc FROM public_gdlp.udc where treeid = 2 and udcseq = 1";
 
  Statement stmt2 = conn2.createStatement();    
  r5 = stmt2.executeQuery(udc);
 while (r5.next()) {
strMyString = r5.getString("countless");
}
 }
 
catch(SQLException e) {
   System.out.println("SQLException: " + e.getMessage() + "<BR>");
   while((e = e.getNextException()) != null)
      System.out.println(e.getMessage() + "<BR>");
                      }

return strMyString;
}
%>
>> while (r5.next()) {
this could throw sql exception and you are not catching it.

try change
>>public String doSomthing() {
to
public String doSomthing() throws SQLException {
oops, Kuldeepchaturvedi also work!
:-) I figured that you were resorting to your old habit of reading the question and then start typing the suggestion/solution..
.......

Ok, well that fixed all but one of those errors.  I am still getting one more pointing to the line starting class.forname....
actually, i'm a little slow. I opened the Q, type the half, then pick up a call or two. then finished the rest. :-)
>>>>Ok, well that fixed all but one of those errors.  I am still getting one more pointing to the line starting class.forname....
 whats the error?/
it's the same error i was getting before

unreported exception: java.sql.SQLException; must be caught or declared to be thrown
use this:

public String doSomthing() throws java.sql.SQLException, ClassNotFoundException {
what solution you adopted?? mine or kenneth's??
can you post your modified code..?? because now all you method is inside a try block so its not suppose to throw a SQLException any more..
<%!
public String doSomthing() {
Connection conn2 = null;
String strMyString = "";
 try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn2 = DriverManager.getConnection(
        "jdbc:oracle:thin:@divodb002:1521:salest01",
         "public_gdlp",
        "public");
 
     ResultSet r5;
String udc = "Select udc FROM public_gdlp.udc where treeid = 2 and udcseq = 1";
 
  Statement stmt2 = conn2.createStatement();    
  r5 = stmt2.executeQuery(udc);
 while (r5.next()) {
strMyString = r5.getString("countless");
}
 }
 
catch(SQLException e) {
   System.out.println("SQLException: " + e.getMessage() + "<BR>");
   while((e = e.getNextException()) != null)
      System.out.println(e.getMessage() + "<BR>");
                      }

return strMyString;
}
%>

here is the adjusted code
I also used your code and not kenneths
Okay this is bound to work...

<%!
public String doSomthing() {
Connection conn2 = null;
String strMyString = "";
 try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn2 = DriverManager.getConnection(
        "jdbc:oracle:thin:@divodb002:1521:salest01",
         "public_gdlp",
        "public");
 
     ResultSet r5;
String udc = "Select udc FROM public_gdlp.udc where treeid = 2 and udcseq = 1";
 
  Statement stmt2 = conn2.createStatement();    
  r5 = stmt2.executeQuery(udc);
 while (r5.next()) {
strMyString = r5.getString("countless");
}
 }
 
catch(SQLException e) {
   System.out.println("SQLException: " + e.getMessage() + "<BR>");
   while((e = e.getNextException()) != null)
      System.out.println(e.getMessage() + "<BR>");
                      }
catch(ClassNotFoundException cnfe) {
   System.out.println("ClassNotFoundException: " + cnfe.getMessage() + "<BR>");
   while((cnfe = cnfe.getNextException()) != null)
      System.out.println(cnfe.getMessage() + "<BR>");
                      }
catch(Exception e1) { //this is the mother of all Exception..... nobody can bypass this one..:-)
   System.out.println("GeneralException: " + e1.getMessage() + "<BR>");
   while((e1 = e1.getNextException()) != null)
      System.out.println(e1.getMessage() + "<BR>");
                      }

return strMyString;
}
%>
this fixed the Sql exceptions, now I get an exception that says the method getNextException is not found in class java.lang.exception
catch(SQLException e) {
   System.out.println("SQLException: " + e.getMessage() + "<BR>");
   while((e = e.getNextException()) != null)
      System.out.println(e.getMessage() + "<BR>");
                      }
catch(ClassNotFoundException cnfe) {
   System.out.println("ClassNotFoundException: " + cnfe.getMessage() + "<BR>");
                      }
catch(Exception e1) { //this is the mother of all Exception..... nobody can bypass this one..:-)
   System.out.println("GeneralException: " + e1.getMessage() + "<BR>");
                      }
dumb me!!!
shd have seen it...
there is no method in class Exception with name getNextException...!!
btw, system.out.println will not put anything on your page so the "<BR>" is pointless.

for the simplist code, just throw it as I suggested, because you don't do anything meaningful in the catch block.
sorry, should read as
... because you are not doing anything ...
this should work, (you don't need public):
<%!
private String doSomthing() throws java.sql.SQLException, ClassNotFoundException {
Connection conn2 = null;
String strMyString = "";
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn2 = DriverManager.getConnection(
        "jdbc:oracle:thin:@divodb002:1521:salest01",
         "public_gdlp",
        "public");
 
     ResultSet r5;
String udc = "Select udc FROM public_gdlp.udc where treeid = 2 and udcseq = 1";
 
  Statement stmt2 = conn2.createStatement();    
  r5 = stmt2.executeQuery(udc);
 while (r5.next()) {
strMyString = r5.getString("countless");
}
return strMyString;
}
%>
<%!
public String doSomthing() {
Connection conn2 = null;
String strMyString = "";
 try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn2 = DriverManager.getConnection(
        "jdbc:oracle:thin:@divodb002:1521:salest01",
         "public_gdlp",
        "public");
 
     ResultSet r5;
String udc = "Select udc FROM public_gdlp.udc where treeid = 2 and udcseq = 1";
 
  Statement stmt2 = conn2.createStatement();    
  r5 = stmt2.executeQuery(udc);
 while (r5.next()) {
strMyString = r5.getString("countless");
}
 }
 
catch(SQLException e) {
   System.out.println("SQLException: " + e.getMessage());
                               }
catch(ClassNotFoundException cnfe) {
   System.out.println("ClassNotFoundException: " + cnfe.getMessage());
                               }
catch(Exception e1) { //this is the mother of all Exception..... nobody can bypass this one..:-)
   System.out.println("GeneralException: " + e1.getMessage() );
                               }

return strMyString;
}
%>
That code you worked fine.  But now if I try to change the system.out.println to out.println it tells me that the variable "out" is not a variable in the class
the out is not available in methods defined in jsp page. try the code I posted above. there is no point to catch an exception and just display it, the server will do that for you. you'll be better of using a jsp error page, that will come later.

for now, just throw the exception as I told you.
ASKER CERTIFIED SOLUTION
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ooops...
this time I got slow in typing....:-)