Oracle開発関連情報 | |||
|
import java.sql.*; public class Sample01 { public static void main(String[] arg) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException ex) { System.err.println("ERROR: JDBC Driverが見つかりません。"); ex.printStackTrace(); System.exit(1); } try { conn = DriverManager.getConnection( "jdbc:oracle:thin:@il-51:1521:o815","wang-admin","wang-admin"); } catch (SQLException ex) { System.err.println("ERROR: DBMSとの接続ができません。"); ex.printStackTrace(); System.exit(1); } try { stmt = conn.createStatement(); rs = stmt.executeQuery("select * from books"); } catch (SQLException ex) { System.err.println("ERROR: SQL文に誤りがあります。"); ex.printStackTrace(); System.exit(1); } try { int id; String title; while (rs.next()) { id = rs.getInt("id"); title = rs.getString("title"); System.out.println("No. " + id + " : タイトル " + title); } stmt.close(); conn.close(); } catch (SQLException ex) { System.err.println("ERROR: SQLの実行結果が誤りがあります。"); ex.printStackTrace(); System.exit(1); } } } |
実行結果が下記のようになりました。
No. 0 : タイトル JDBC and JAVA No. 1 : タイトル DATABASE SYSTEM |