Solved

SQL Exception Error

Posted on 2004-04-05
26
733 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 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