简单 JDBC 应用程序的示例
简单 JDBC 应用程序演示 JDBC 应用程序需要包括的基本元素。
import java.sql.*; 1
public class EzJava
{
public static void main(String[] args)
{
String urlPrefix = "jdbc:db2:";
String url;
String user;
String password;
String empNo; 2
Connection con;
Statement stmt;
ResultSet rs;
System.out.println ("**** Enter class EzJava");
// Check the that first argument has the correct form for the portion
// of the URL that follows jdbc:db2:,
// as described
// in the Connecting to a data source using the DriverManager
// interface with the IBM Data Server Driver for JDBC and SQLJ topic.
// For example, for IBM Data Server Driver for
// JDBC and SQLJ type 2 connectivity,
// args[0] might be MVS1DB2M. For
// type 4 connectivity, args[0] might
// be //stlmvs1:10110/MVS1DB2M.
if (args.length!=3)
{
System.err.println ("Invalid value. First argument appended to "+
"jdbc:db2: must specify a valid URL.");
System.err.println ("Second argument must be a valid user ID.");
System.err.println ("Third argument must be the password for the user ID.");
System.exit(1);
}
url = urlPrefix + args[0];
user = args[1];
password = args[2];
try
{
// Load the driver
Class.forName("com.ibm.db2.jcc.DB2Driver"); 3a
System.out.println("**** Loaded the JDBC driver");
// Create the connection using the IBM Data Server Driver for JDBC and SQLJ
con = DriverManager.getConnection (url, user, password); 3b
// Commit changes manually
con.setAutoCommit(false);
System.out.println("**** Created a JDBC connection to the data source");
// Create the Statement
stmt = con.createStatement(); 4a
System.out.println("**** Created JDBC Statement object");
// Execute a query and generate a ResultSet instance
rs = stmt.executeQuery("SELECT EMPNO FROM EMPLOYEE"); 4b
System.out.println("**** Created JDBC ResultSet object");
// Print all of the employee numbers to standard output device
while (rs.next()) {
empNo = rs.getString(1);
System.out.println("Employee number = " + empNo);
}
System.out.println("**** Fetched all rows from JDBC ResultSet");
// Close the ResultSet
rs.close();
System.out.println("**** Closed JDBC ResultSet");
// Close the Statement
stmt.close();
System.out.println("**** Closed JDBC Statement");
// Connection must be on a unit-of-work boundary to allow close
con.commit();
System.out.println ( "**** Transaction committed" );
// Close the connection
con.close(); 6
System.out.println("**** Disconnected from data source");
System.out.println("**** JDBC Exit from class EzJava - no errors");
}
catch (ClassNotFoundException e)
{
System.err.println("Could not load JDBC driver");
System.out.println("Exception: " + e);
e.printStackTrace();
}
catch(SQLException ex) 5
{
System.err.println("SQLException information");
while(ex!=null) {
System.err.println ("Error msg: " + ex.getMessage());
System.err.println ("SQLSTATE: " + ex.getSQLState());
System.err.println ("Error code: " + ex.getErrorCode());
ex.printStackTrace();
ex = ex.getNextException(); // For drivers that support chained exceptions
}
}
} // End main
} // End EzJava
图1 注释:
| 注 | 描述 |
|---|---|
| 1 | 此语句导入 java.sql 包,该包中包含 JDBC 核心 API。 如需了解访问其他Java包的相关信息,请参阅“ JDBC 支持的Java包”。 |
| 2 | String 变量 empNo 执行主变量的函数。 即,它用来保存从 SQL 查询检索到的数据。 有关更多信息,请参阅“JDBC 应用程序中的变量”。 |
| 3a 和 3b | 这两组语句演示了如何使用两个可用接口之一来连接到数据源。 更多详情,请参阅“ JDBC 应用程序如何连接数据源”。 如果您使用 JDBC 4.0 或更高版本,那么不需要执行步骤 3a(装入 JDBC 驱动程序)。 |
| 4a 和 4b | 这两组语句演示了如何在 JDBC 中执行 SELECT。 有关如何执行其他 SQL 操作的信息,请参阅“用于执行 SQL 的 JDBC 接口”。 |
| 5 | 这个 try/catch 块演示了 SQLException 类在SQL错误处理中的用法。 有关处理SQL错误的更多信息,请参阅“处理 IBM® Data Server Driver for JDBC and SQLJ 下的SQLException”。 有关处理SQL警告的信息,请参阅“处理 IBM Data Server Driver for JDBC and SQLJ 下的SQL警告”。 |
| 6 | 此语句会将应用程序与数据源断开连接。 请参阅“从 JDBC 应用程序中的数据源断开连接”。 |