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