Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / February 2006

Tip: Looking for answers? Try searching our database.

sqlexception

Thread view: 
Anna Kubiak - 18 Feb 2006 13:21 GMT
I have such a code. what's wrong? i got an error "unreported
java.sql.Sql.Exception ; must be caught or declared"

public class serwis_t extends java.applet.Applet {

ResultSet rs;

public static void main (String [] arg ) throws Exception{
   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String DBURL = "jdbc:odbc:serwis";
Connection conn = DriverManager.getConnection(DBURL);

Statement st = conn.createStatement();
String qry = "select * from aparaty ";
ResultSet rs = st.executeQuery(qry);
       }
public void paint(Graphics g) {
while (rs.next()) {
   g.drawString(rs.getString(1), 10,10);
}
   }
       }
David Harper - 18 Feb 2006 13:47 GMT
> I have such a code. what's wrong? i got an error "unreported
> java.sql.Sql.Exception ; must be caught or declared"
[quoted text clipped - 19 lines]
>     }
>         }

The calls to getConnection, createStatement and executeQuery can all
throw an SQLException, yet your code does not enclose them in a
try-catch block nor does the method which calls them declare that it
throws an SQLException itself.

Actually, I'm not sure that it's permitted to have the "main" method
declare that it throws exceptions, so your only option is to enclose the
calls to JDBC methods inside a try-catch block:

  try {
    Connection conn = DriverManager.getConnection(DBURL);

    Statement st = conn.createStatement();
    String qry = "select * from aparaty ";
    ResultSet rs = st.executeQuery(qry);
  }
  catch (SQLException sqle) {
    // This code will execute if any of your JDBC methods throws
    // a SQLException.

    // This is *not* the way to handle such an event in production
    // code!
    sqle.printStackTrace();
    System.exit(1);
  }

In production code, you would enclose each JDBC call inside its own
try-catch block, so that your applet can fail in a manner that is both
graceful and appropriate to the error that has occurred.

David Harper
Cambridge, England
Anna Kubiak - 18 Feb 2006 14:40 GMT
Thank you! it helped me a lot. but....

do i ask for too much???? how to print table ( my variable rs as ResultSet)
in applet window ? I ran my code (after adding try and catch everywhere i
could) and got a lot of errors which i do not understand! Window opened ,
but empty :-(

Thanks again,
Anna
Warsaw, Poland
Alun Harford - 19 Feb 2006 01:56 GMT
> Actually, I'm not sure that it's permitted to have the "main" method
> declare that it throws exceptions,

Of cause it can. It's a very normal thing to do.

Alun Harford
Bjorn Abelli - 18 Feb 2006 14:36 GMT
"Anna Kubiak" wrote...

>I have such a code. what's wrong?

Apart from the error you got, I would say there's many more things wrong...
;-)

> i got an error "unreported java.sql.Sql.Exception ; must be caught or
> declared"

That error refers to the calls to "rs.next()" and "rs.getString(1)" inside
the method "paint".

And the error says it all.

Either you must enclose those calls in try-catch-block that catches
SQLException, or the method itself must be declared to throw SQLExceptions.

However, you should think more about what actually happens in your code. If
you really want to run it as an applet, you should structure the code to do
what you want in another way than to make use of the "main" method. Even if
you want to run it as an application, you should think again about what it's
supposed to do.

But back to the "erroprs". One rule of thumb I have, is to separate the
calls to the DB from what you do with the data in the application.

As a ResultSet is "connected" to the database, you'll probably run into
several problems when the app leaves the "main" method, with dangling
connections, just because you haven't closed them or the ResultSet.

A simple suggestion to start off with:

Think of the data as "disconnected" from the DB.

Instead of using the ResultSet as an instance field, use another type of
collection, which you populate with the data you want to use.

Then use that collection and its data in the paint-method, instead of using
the ResultSet in it.

// Bjorn A
Anna Kubiak - 18 Feb 2006 14:57 GMT
Hi Bjorn,

I've changed a lot in my code. Threw void main and used void start ()
instead. yes, i would like to run it as applet.

I am too weak in Java to know what a collection is :-(

Anyway thank you,
anna
Oliver Wong - 20 Feb 2006 21:46 GMT
> I am too weak in Java to know what a collection is :-(

   You might want to read
http://java.sun.com/docs/books/tutorial/collections/index.html and
http://java.sun.com/j2se/1.5.0/docs/guide/collections/index.html

   - Oliver
Roedy Green - 21 Feb 2006 19:39 GMT
On Sat, 18 Feb 2006 15:57:09 +0100, "Anna Kubiak"
<anna.maria.k@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>I am too weak in Java to know what a collection is

see http://mindprod.com/jgloss/collection.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.