1.サンプルプログラム
import java.sql.*; class sample01 { public void tblslt() throws Exception { String user = "scott"; String pass = "tiger"; String server = "localhost"; String sid = "orcl"; Connection conn = null; Statement stmt = null; ResultSet rset = null; Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection( "jdbc:oracle:thin:@" + server + ":1521:" + sid,user,pass); stmt = conn.createStatement(); rset = stmt.executeQuery("select deptno,dname,loc from dept"); while(rset.next()) { System.out.printf("deptno = [%s]\n",rset.getString(1)); System.out.printf("dname = [%s]\n",rset.getString(2)); System.out.printf("loc = [%s]\n",rset.getString(3)); } conn.close(); } public static void main(String args[]) throws Exception { sample01 obj = new sample01(); obj.tblslt(); } }2.コンパイルと実行
$ ls -l 合計 8 -rw-rw-r-- 1 inst12c inst12c 902 3月 29 16:03 sample01.java $ javac sample01.java $ ls -l 合計 16 -rw-r--r-- 1 inst12c ibst12c 1763 3月 29 16:04 sample01.class -rw-rw-r-- 1 ibst12c ibst12c 902 3月 29 16:03 sample01.java $ java -classpath $ORACLE_HOME/jdbc/lib/ojdbc6.jar:. sample01 deptno = [10] dname = [ACCOUNTING] loc = [NEW YORK] deptno = [20] dname = [RESEARCH] loc = [DALLAS] deptno = [30] dname = [SALES] loc = [CHICAGO] deptno = [40] dname = [OPERATIONS] loc = [BOSTON]
0 件のコメント:
コメントを投稿