Solved

SQL Exception Error

Posted on 2004-04-05
26
731 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery ajax file upload not working 11 154
Stuck in Bootstrap WysiHtml Editor 3 37
authenticate preemptively soap ui option 3 124
TreeSet comparator example 7 90
When we talk about DevOps toolchains, I sometimes wonder how many people really get what we’re talking about. I don’t know if it’s just semantics or tone or something else, but sometimes I think it just sounds like buzzword sausage. So it’s always …
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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