Class.forName("org.apache.derby.jdbc.ClientDriver");
次に、以下の記述でデータベースを指定します。
con = DriverManager.getConnection("jdbc:derby://localhost:1527/SampleDB;
create=true;user=me;password=mine;");//1行で書いてください
上記のデータベース指定はjdbc:oracle:thin:@データベースのIPアドレス:ポート名:DBインスタンス名,スキーマ,パスワードというように指定をします。
import java.sql.*;
public class oracleDbConnect {
public static void main(String[] args){
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
con = DriverManager.getConnection("
jdbc:derby://localhost:1527/SampleDB;
create=true;user=me;password=mine;");
stmt = con.prepareStatement("select * from tbl_salary");
rs = stmt.executeQuery();
while(rs.next()){
System.out.print(rs.getInt(1));
System.out.print(rs.getInt(2));
System.out.print(rs.getInt(3));
}
}catch(SQLException e){
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(rs != null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(stmt != null){
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(con != null){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}