| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 973 人关注过本帖
标题:菜菜有个数据库的东西不明白
只看楼主 加入收藏
轩辕惊鸿
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2004-8-8
收藏
 问题点数:0 回复次数:3 
菜菜有个数据库的东西不明白

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是什么意思?版主,我仔细看书了,但书上没有说。。。。。。问一下不过份吧

搜索更多相关主题的帖子: 菜菜 数据库 
2004-08-09 09:49
轩辕惊鸿
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2004-8-8
收藏
得分:0 

private void displayResultSet( ResultSet rs ) throws SQLException {

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

2004-08-09 11:21
tempnetbar
Rank: 2
等 级:新手上路
威 望:4
帖 子:582
专家分:4
注 册:2004-5-5
收藏
得分:0 

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

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

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

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


相信勤能补拙! 喜欢用好用的就永远学不到有用的。
2004-08-09 18:23
tailys
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2004-8-16
收藏
得分:0 

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

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

这几句说明也不错:

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.

2004-08-17 00:50
快速回复:菜菜有个数据库的东西不明白
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017332 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved