Re: sqlexception



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
.



Relevant Pages

  • Re: Datenbank in JTable
    ... static JTable getAllCustomers() { ... ResultSet resultSet = null; ... Connection connection = DAToDB.getConnection; ... catch (SQLException sqlex) { ...
    (de.comp.lang.java)
  • Re: jdbc query
    ... resultset is not getting executed. ... You can use the execute around idiom to tidy this lot up: ... final EmployerTeam employerTeam ... throws SQLException { ...
    (comp.lang.java.programmer)
  • Re: Invalid cursor state - Am I completely stupid?
    ... > cursor state SQLException whenever I access the Resultset.get* ... > executes and the Resultset exists as I can read the ResultSetMetaData ... wrote above, there is a permission problem, it may be that the empty ...
    (comp.lang.java.programmer)
  • Re: ASE 12.5 & ResultSet & JAVA
    ... The program stops when I try to read the values! ... Connection is established, resultSet are returened, but I cannot read that values! ... do the first columns in the table have ... I don't understand why I cannot catch SQLException! ...
    (comp.databases.sybase)
  • Dates in a date range
    ... Is there a way that I can get a resultset that contains unique dates in ... declare @start_date as datetime ... Prev by Date: ...
    (comp.databases.ms-sqlserver)