概要
この付録には、Java Database Connectivity(JDBC)を使用して SQL データベースに接続し、SQL 文を作成および実行してから、結果を取得する Java プログラムを作成するために必要なファイルが含まれています。 表 A-1 に、サンプル ファイルを示します。
表 A-1 サンプル Java プログラム内のファイル
|
|
例 A-1 JTest.java |
データベースに接続し、文を作成および実行してから、結果を取得するメイン プログラム。 |
例 A-2 ConnectionPoolManager.java |
データベースに接続するための ConnectionPoolManager クラスの定義。 |
例 A-3 Switch.java |
文を作成するための switch クラスの定義。 |
例 A-4 db.properties |
データベースに固有の定義。これらの値は、使用する JDBC ドライバ、およびアクセスするデータベースの URL、ユーザ、およびパスワードを反映するように設定する必要があります。 |
Wikipedia と『JDBC API Guide』で JDBC の概要を参照するには、次のリンクにアクセスしてください。
• http://en.wikipedia.org/wiki/JDBC
• http://download.oracle.com/javase/1.5.0/docs/guide/jdbc/
JDBC API をインストールすると、文で使用するデータ型をインストール ディレクトリ ファイル constant-values.html で検索できます。
例 A-1 JTest.java
public static void main(String args[]) {
SQLWarning warning = null;
ResultSet results = null;
Properties prop = new Properties(); // contains contents of propertyFileName
String drivername = null;
String drivername2 = null;
System.out.println(“Java Test”);
// Get the Connection Props.
InputStream is = new BufferedInputStream(new FileInputStream(new File(“db.properties”)));
name = prop.getProperty(“DS.name”);
URL = prop.getProperty(“DS.url”);
user = prop.getProperty(“DS.user”);
password = prop.getProperty(“DS.password”);
drivername = prop.getProperty(“DS.driver”);
drivername2 = prop.getProperty(“DS.driver2”);
System.out.println(name);
System.out.println(drivername);
Driver d = (Driver)Class.forName(drivername).newInstance();
if ( drivername2 != null) {
Driver d2 = (Driver)Class.forName(drivername2).newInstance();
System.out.println(drivername2);
con = DriverManager.getConnection(URL,user,password);
// GET CONNECTION WARNINGS
warning = con.getWarnings();
System.out.println(“No Warnings”);
while (warning != null) {
System.out.println(“Warning: “+warning);
warning = warning.getNextWarning();
stmt = con.createStatement();
ret = stmt.execute(“select * from EMP”);
results = stmt.getResultSet();
updateCount = stmt.getUpdateCount();
StringBuffer buf = new StringBuffer();
ResultSetMetaData rsmd = results.getMetaData();
int numCols = rsmd.getColumnCount();
// get column header info
for (i=1; i <= numCols; i++){
if (i > 1) buf.append(“,”);
buf.append(rsmd.getColumnLabel(i));
// break it off at 100 rows max
while (results.next() && rowcount < 100){
// Loop through each column, getting the column
for (i=1; i <= numCols; i++) {
if (i > 1) buf.append(“,”);
buf.append(results.getString(i));
DISCLAIMER: The sample code is not supported under any DataDirect Technologies support program or service. The sample code is provided on an “AS IS” basis. DataDirect Technologies makes no warranties, express or implied, and disclaims all implied warranties including, without limitation, the implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample code is borne by the user. In no event shall DataDirect Technologies, its employees, or anyone else involved in the creation, production, or delivery of the code be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample code, even if DataDirect Technologies has been advised of the possibility of such damages.
例 A-2 ConnectionPoolManager.java
public class ConnectionPoolManager {
//specify which database schema will be used
private final static String Alias = “dbname”;
//specify the database vendor libary that implements JDBC api
private final static String DbDriver = “org.hsqldb.jdbcDriver”;
//other attributes for connecting database
private static String DbUrl = “jdbc:hsqldb:hsql://localhost”;
private static String DbUser = “db_username”;
private static String DbPass = “_db_password”;
private final static String[] DbFiles = {
public static ConnectionPoolManager Instance;
public static ConnectionPoolManager getInstance(){
Class.forName(_DbDriver).newInstance();
Instance= new ConnectionPoolManager(300);
Instance.addAlias(Alias, DbDriver, DbUrl, DbUser, DbPass, 6, 300, 10, 10);
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(“jdbc:bitmechanic:pool:”+ Alias, null, null);
public static void returnConnection(Connection conn) throws SQLException {
例 A-3 Switch.java
public final class Switch {
final static String QuerySQLByFabricID =
“select id, wwn, ip_address, is_mds, type, is_managable, non_mds_model, sys_name, sys_contact, sys_location, sys_uptime, active_sup_slot, conn_unit_status, standby_sup_state, feature_flag, is_license_violation, version, is_present, serial_number, unmanagable_cause, last_scan_time, num_ports, is_trap_registered, is_syslog_registered, standby_sup_slot, module_index_offset from switch where fabric_id=?”;
public static ArrayList loadFromDB(long fabricId)
Connection con = ConnectionPoolManager.getInstance().getConnection();
PreparedStatement stat = con.prepareStatement(QuerySQLByFabricID);
stat.setLong(1, fabricId);
rs = stat.executeQuery();
ArrayList al = new ArrayList();
//parsing result set and put items to the list
} catch (SQLException ex) {
ConnectionManager.getInstance().returnConnection(con);
例 A-4 db.properties
DS.driver=com.ddtek.jdbc.oracle.OracleDriver
DS.url=jdbc:datadirect:oracle://servername:1521;SID=ORASID
//DS.driver=com.ddtek.jdbc.sequelink.SequeLinkDriver
//DS.url=jdbc:sequelink://servername:19996
//DS.driver=com.ddtek.jdbcspy.SpyDriver
//DS.driver2=com.ddtek.jdbc.oracle.OracleDriver
//DS.url=jdbc:spy:{jdbc:datadirect:oracle://servername:1521;SID=ORASID;user=uid;password=pwd};log=(file)C:\\temp\\spy.log