Для следующих ниже примеров организации разных вариантов связи с БД через JDBC нужно подготовить файл StaffByJDBC.java с общим для всех примеров текстом:
import java.sql.*; import oracle.jdbc.driver.*; public class StaffByJDBC { public static void main(String[] args) { String url = null; if (args.length > 0) { if (args[0].compareToIgnoreCase("thin") == 0) { url = "jdbc:oracle:thin:@localhost:1521:TEACHER"; } else if (args[0].compareToIgnoreCase("oci") == 0) { url = "jdbc:oracle:oci8:@TEACHER"; } else if (args[0].compareToIgnoreCase("kprb") == 0) { url = "jdbc:oracle:kprb:"; } } if (url == null) { System.out.println("usage: StaffByJDBC [thin|oci]"); return; } try { DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver()); } catch (Exception e) { return; } try { Connection cn = DriverManager.getConnection (url,"scott","tiger"); Statement st = cn.createStatement(); ResultSet rs = st.executeQuery ("SELECT empno, ename FROM emp"); while (rs.next()) { System.out.println("Number=" + rs.getString(1) + " " + "Name=" + rs.getString(2)); } st.close(); cn.close(); } catch (Exception e) { return; } finally { System.out.println("All that happened"); } } } |