Solved

SQL Exception Error

Posted on 2004-04-05
26
732 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xjc and jaxb 2 93
constuctor calling other constructor 2 76
java beans and EJBs 5 192
Adjusting width and height of dropdown produced using select n option HTML tags 4 78
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
This article outlines why you need to choose a backup solution that protects your entire environment – including your VMware ESXi and Microsoft Hyper-V virtualization hosts – not just your virtual machines.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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