Re: sqlexception
- From: David Harper <devnull@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 18 Feb 2006 13:47:45 GMT
Anna Kubiak wrote:
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);
}
}
}
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
.
- Follow-Ups:
- Re: sqlexception
- From: Alun Harford
- Re: sqlexception
- From: Anna Kubiak
- Re: sqlexception
- References:
- sqlexception
- From: Anna Kubiak
- sqlexception
- Prev by Date: sqlexception
- Next by Date: Re: sqlexception
- Previous by thread: sqlexception
- Next by thread: Re: sqlexception
- Index(es):
Relevant Pages
|
|