Solved

SQL Exception Error

Posted on 2004-04-05
26
730 Views
Last Modified: 2010-04-01
Can anyone inform me what this error means.

unreported exception: java.sql.SQLException; must be caught or declared to be thrown
0
Comment
Question by:gdlp2004
  • 10
  • 9
  • 7
26 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10760171
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
0
 

Author Comment

by:gdlp2004
ID: 10760308
<%!
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
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10760343
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;
}
%>
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 10760386
>> 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 {
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 10760395
oops, Kuldeepchaturvedi also work!
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10760411
:-) I figured that you were resorting to your old habit of reading the question and then start typing the suggestion/solution..
.......

0
 

Author Comment

by:gdlp2004
ID: 10760418
Ok, well that fixed all but one of those errors.  I am still getting one more pointing to the line starting class.forname....
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 10760443
actually, i'm a little slow. I opened the Q, type the half, then pick up a call or two. then finished the rest. :-)
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10760452
>>>>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?/
0
 

Author Comment

by:gdlp2004
ID: 10760478
it's the same error i was getting before

unreported exception: java.sql.SQLException; must be caught or declared to be thrown
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 10760499
use this:

public String doSomthing() throws java.sql.SQLException, ClassNotFoundException {
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10760502
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..
0
 

Author Comment

by:gdlp2004
ID: 10760513
<%!
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
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:gdlp2004
ID: 10760518
I also used your code and not kenneths
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10760550
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;
}
%>
0
 

Author Comment

by:gdlp2004
ID: 10760654
this fixed the Sql exceptions, now I get an exception that says the method getNextException is not found in class java.lang.exception
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 10760685
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>");
                      }
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10760695
dumb me!!!
shd have seen it...
there is no method in class Exception with name getNextException...!!
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 10760702
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.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 10760716
sorry, should read as
... because you are not doing anything ...
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 10760726
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;
}
%>
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10760746
<%!
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;
}
%>
0
 

Author Comment

by:gdlp2004
ID: 10760931
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
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 10761159
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.
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 250 total points
ID: 10761167
which it will indeed say....

now there are two options..
go by Kenneth's way i.e.

method is written to throw the exception...
<%!
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;
}
%>


and then in jsp where you are actually calling this method.. there we put it in a try catch block..

like
try{
String rtval=dosomething();
}catch(Exception e)
{
out.write("GeneralException: " + e1.getMessage() );

}

this will solve the trouble u have..
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10761172
Ooops...
this time I got slow in typing....:-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now