当前位置: 代码迷 >> Java相关 >> 菜菜有个数据库的东西不明白
  详细解决方案

菜菜有个数据库的东西不明白

热度:528   发布时间:2004-08-09 09:49:00.0
菜菜有个数据库的东西不明白

public class TableDisplay extends JFrame { private Connection connection; private JTable table; public TableDisplay() { // The URL specifying the Books database to which // this program connects using JDBC to connect to a // Microsoft ODBC database. String url = "jdbc:odbc:Books"; String username = "anonymous"; String password = "guest";

// Load the driver to allow connection to the database try { Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

connection = DriverManager.getConnection( url, username, password ); } catch ( ClassNotFoundException cnfex ) { System.err.println( "Failed to load JDBC/ODBC driver." ); cnfex.printStackTrace(); System.exit( 1 ); // terminate program } catch ( SQLException sqlex ) { System.err.println( "Unable to connect" ); sqlex.printStackTrace(); }

这个程序多次出现的try、catch是什么意思?版主,我仔细看书了,但书上没有说。。。。。。问一下不过份吧

搜索更多相关的解决方案: 菜菜  数据库  

----------------解决方案--------------------------------------------------------

private void displayResultSet( ResultSet rs ) throws SQLException {

这当中的throws是什么意思?也没找到


----------------解决方案--------------------------------------------------------

^o^那你应该多找几本书看看啊,呵呵,不过我觉得书本可能讲得不详细,但是绝对不会没有例外处理的讲解

throws就是抛出异常的意思,告诉编译器这个方法会有异常,调用它的地方需要给出处理的代码,也就是要

try { displayResultSet(rs); } catch (SQLException sqle) { }

通常有throws的方法内都会有throw语句,用来在需要抛出异常的地方抛出异常,这些都是要自己悟出来的,我学的时候书上也没讲解清楚,但是我反复看了书上的例子悟出来了


----------------解决方案--------------------------------------------------------

楼主同志,呵呵,一般java学习的书都会有一章的篇幅去介绍java的异常捕捉处理机制,而且我相信你对于在方法上去throws 和 try catch有什么不同也不太理解,

去sun的Exception lesson 看看吧:http://java.sun.com/docs/books/tutorial/essential/exceptions/index

这几句说明也不错:

Java divides exceptions into two categories:

  1. General exceptions
  2. Run-time exceptions

The general exceptions must be handled. That is, a try-catch must either be nested around the call to the method that throws the exception or the method must explicitly indicate with throws that it can generate this exception. (Then other methods that invoke this method must then catch the exception or throw it up the line.) The compiler will throw an error message if it detects an uncaught exception and will not compile the file.

The run-time exceptions do not have to be caught. This avoids requiring that a try-catch be place around, for example, every integer division operation to catch a divide by zero or around every array variable to watch for indices going out of bounds.

However, you should handle possible run-time exceptions if you think there is a reasonable chance of one occurring. In the above string parsing example, the NumberFormatException extends (we discuss subclassing in Chapter 4) RuntimeException so it is optional to catch this error.

You can use multiple catch clauses to catch the different kinds of exceptions that code can throw as shown in this snippet:

try { ... some code... } catch ( NumberFormatException e) { ... } catch (IOException e) { ... } catch (Exception e) { ... } finally // optional { ...this code always executed even if no exceptions... }

Here there are two catch statements for Exception subclasses (subclassing will be discussing in Chapter 4),and one for any other Exception instance. Regardless of whether the code generates an exception or not, the finally code block will always execute.

Exception handling is thus based on try-catch operation, the throws and throw keywords, and the Exception class and its subclasses.


----------------解决方案--------------------------------------------------------
  相关解决方案