最終更新: chipstar_light 2010年08月24日(火) 13:06:17履歴
- Java 6から追加されたSQLデータベース機能
- 別途OracleなどのDBMSを用意しなくとも、Java本体だけでRDBを利用することができる
- 元々はApache Foundationで開発が行われていた「Derby」というpure Javaのデータベース
- DBサーバとして起動しクライアント&サーバ方式でアクセス可能
- SQLiteなどの組み込みDBとは違い、複数ユーザからの同時アクセス可能
次のjarファイルをクラスパスに通す。※デフォルトのインストール場所の場合
C:\Program Files\Sun\JavaDB\lib\derby.jar
public static void main(String[] args) throws Exception { Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { Properties prop = new Properties(); prop.put("user", "ユーザID"); prop.put("password", "パスワード"); connection = DriverManager.getConnection("jdbc:derby:C:\\Derby\\mydatabase", prop); statement = connection.createStatement(); resultSet = statement.executeQuery("select * from sampleteable"); while (resultSet.next()) { int number = resultSet.getInt("column1"); String name = resultSet.getString("column2"); System.out.println(number + "|" + name ); } } finally { if (resultSet != null) resultSet.close(); if (statement != null) statement.close(); if (connection != null) connection.close(); } }
JDBC接続文字列の末尾に"create=true"をつけて接続をする
Properties prop = new Properties(); prop.put("user", "ユーザID"); prop.put("password", "パスワード"); Connection connection = DriverManager.getConnection("jdbc:derby:C:\\Derby\\mydatabase;create=true", prop);接続時に指定したユーザID、パスワードがデータベースにログインするユーザのIDとパスワードになる
binディレクトリ化の"startNetworkServer.bat"を起動する。
※デフォルトのインストール場所の場合
※デフォルトのインストール場所の場合
C:\Program Files\Sun\JavaDB\bin\startNetworkServer.bat
次のjarファイルをクラスパスに通す。※デフォルトのインストール場所の場合
C:\Program Files\Sun\JavaDB\lib\derby.jar C:\Program Files\Sun\JavaDB\lib\derbyclient.jar
public static void main(String[] args) throws Exception { Class.forName("org.apache.derby.jdbc.ClientDriver"); Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { Properties prop = new Properties(); prop.put("user", "ユーザID"); prop.put("password", "パスワード"); connection = DriverManager.getConnection("jdbc:derby://localhost:1527/C:/Derby/mydatabase", prop); statement = connection.createStatement(); resultSet = statement.executeQuery("select * from sampleteable"); while (resultSet.next()) { int number = resultSet.getInt("column1"); String name = resultSet.getString("column2"); System.out.println(number + "|" + name ); } } finally { if (resultSet != null) resultSet.close(); if (statement != null) statement.close(); if (connection != null) connection.close(); } }
コメントをかく