Archive for 10月 19th, 2012
-
Hello, JDBC Type1(Tcl/Java) World!
Posted on 10月 19th, 2012 by cx20
JDBC Type1
JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
Type1 は JDBC と ODBC の API をマップさせたブリッジドライバである。
以下は Tcl/Java による JDBC ライブラリの使用例となっている。ソースコード(Tcl/Java + JDBC Type1 + ODBC + Jet データベース)
package require java java::import -package java.sql DriverManager java::call Class forName "sun.jdbc.odbc.JdbcOdbcDriver" set conn [java::call DriverManager getConnection "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=.\hello.mdb" "" ""] set stmt [$conn createStatement] set rs [$stmt executeQuery "SELECT 'Hello, JDBC Type1 World!' AS Message"] while { [$rs next] } { puts [$rs {getString int} 1] } $rs close $stmt close $conn close
ソースコード(Tcl/Java + JDBC Type1 + ODBC + ACE データベース)
package require java java::import -package java.sql DriverManager java::call Class forName "sun.jdbc.odbc.JdbcOdbcDriver" set conn [java::call DriverManager getConnection "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=.\hello.accdb" "" ""] set stmt [$conn createStatement] set rs [$stmt executeQuery "SELECT 'Hello, JDBC Type1 World!' AS Message"] while { [$rs next] } { puts [$rs {getString int} 1] } $rs close $stmt close $conn close
ソースコード(Tcl/Java + JDBC Type1 + ODBC + SQL Server)
package require java java::import -package java.sql DriverManager java::call Class forName "sun.jdbc.odbc.JdbcOdbcDriver" set conn [java::call DriverManager getConnection "jdbc:odbc:Driver={SQL Server};SERVER=(local)" "sa" "P@ssW0rd"] set stmt [$conn createStatement] set rs [$stmt executeQuery "SELECT 'Hello, JDBC Type1 World!' AS Message"] while { [$rs next] } { puts [$rs {getString int} 1] } $rs close $stmt close $conn close
ソースコード(Tcl/Java + JDBC Type1 + ODBC + Oracle)
package require java java::import -package java.sql DriverManager java::call Class forName "sun.jdbc.odbc.JdbcOdbcDriver" set conn [java::call DriverManager getConnection "jdbc:odbc:Driver={Oracle in OraDb11g_home1};DBQ=ORCL" "scott" "tiger"] set stmt [$conn createStatement] set rs [$stmt executeQuery "SELECT 'Hello, JDBC Type1 World!' AS Message FROM DUAL"] while { [$rs next] } { puts [$rs {getString int} 1] } $rs close $stmt close $conn close
ソースコード(Tcl/Java + JDBC Type1 + ODBC + MySQL)
package require java java::import -package java.sql DriverManager java::call Class forName "sun.jdbc.odbc.JdbcOdbcDriver" set conn [java::call DriverManager getConnection "bc:odbc:Driver={MySQL ODBC 5.1 Driver};Server=localhost" "root" "P@ssW0rd"] set stmt [$conn createStatement] set rs [$stmt executeQuery "SELECT 'Hello, JDBC Type1 World!' AS Message"] while { [$rs next] } { puts [$rs {getString int} 1] } $rs close $stmt close $conn close
実行方法
C:¥> jaclsh Hello.tcl
実行結果
Hello, JDBC Type1 World!