示例: 使用 JDBC 驱动程序连接到服务器示例:使用未指定系统名称的 URL
此示例会提示用户输入他或她要连接的系统的名称。
// Connect to unnamed system.
// User receives prompt to type system name.
Connection c = DriverManager.getConnection("jdbc:as400:");示例: 连接到服务器数据库; 未指定缺省 SQL 模式或属性
// Connect to system 'mySystem'. No
// default SQL schema or properties are specified.
Connection c = DriverManager.getConnection("jdbc:as400://mySystem");示例: 连接到服务器数据库; 指定了缺省 SQL 模式
// Connect to system 'mySys2'. The
// default SQL schema 'myschema' is specified.
Connection c2 = DriverManager.getConnection("jdbc:as400://mySys2/mySchema");示例: 连接到服务器数据库并使用 java.util.Properties 来指定属性
Java程序可以通过使用 java.util.Properties 界面或通过指定 URL 属性来指定一组 JDBC 属性。 请查看 IBM Toolbox for Java JDBC 属性 ,了解支持的属性列表。
例如,要使用 "属性" 接口指定属性,请使用以下代码作为示例:
// Create a properties object.
Properties p = new Properties();
// Set the properties for the connection.
p.put("naming", "sql");
p.put("errors", "full");
// Connect using the properties object.
Connection c = DriverManager.getConnection("jdbc:as400://mySystem",p);示例:连接到服务器数据库,并使用统一资源定位符( URL )指定属性
// Connect using properties. The
// properties are set on the URL
// instead of through a properties object.
Connection c = DriverManager.getConnection(
"jdbc:as400://mySystem;naming=sql;errors=full");示例: 连接到服务器数据库并指定用户标识和密码
// Connect using properties on the
// URL and specifying a user ID and password
Connection c = DriverManager.getConnection(
"jdbc:as400://mySystem;naming=sql;errors=full",
"auser",
"apassword");示例: 从数据库断开连接
要与服务器断开连接,请在 "连接" 对象上使用 close () 方法。 使用以下语句来关闭先前示例中创建的连接:
c.close();