Hello, JDBC Type4(JRuby) World!
Posted on 10月 9th, 2012 by cx20
JDBC Type4
JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
Type4 は DBMS のクライアントライブラリを使用しない Pure Java の DBMS ドライバである。
以下は JRuby による JDBC ライブラリの使用例となっている。
ソースコード(JRuby + JDBC Type4 + SQL Server)
require 'rubygems'
require 'java'
require 'sqljdbc4.jar'
Java::com.microsoft.sqlserver.jdbc.SQLServerDriver
conn = java.sql.DriverManager.get_connection("jdbc:sqlserver://;serverName=localhost", "sa", "P@ssW0rd")
stmt = conn.create_statement
rs = stmt.execute_query("SELECT 'Hello, JDBC Type4 World!' AS Message")
while (rs.next) do
puts rs.getString(1)
end
rs.close
stmt.close
conn.close |
require 'rubygems'
require 'java'
require 'sqljdbc4.jar'
Java::com.microsoft.sqlserver.jdbc.SQLServerDriver
conn = java.sql.DriverManager.get_connection("jdbc:sqlserver://;serverName=localhost", "sa", "P@ssW0rd")
stmt = conn.create_statement
rs = stmt.execute_query("SELECT 'Hello, JDBC Type4 World!' AS Message")
while (rs.next) do
puts rs.getString(1)
end
rs.close
stmt.close
conn.close
ソースコード(Scala + JDBC Type4 + Oracle)
require 'rubygems'
require 'java'
require 'ojdbc6.jar'
Java::oracle.jdbc.driver.OracleDriver
conn = java.sql.DriverManager.get_connection("jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger")
stmt = conn.create_statement
rs = stmt.execute_query("SELECT 'Hello, JDBC Type4 World!' AS Message FROM DUAL")
while (rs.next) do
puts rs.getString(1)
end
rs.close
stmt.close
conn.close |
require 'rubygems'
require 'java'
require 'ojdbc6.jar'
Java::oracle.jdbc.driver.OracleDriver
conn = java.sql.DriverManager.get_connection("jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger")
stmt = conn.create_statement
rs = stmt.execute_query("SELECT 'Hello, JDBC Type4 World!' AS Message FROM DUAL")
while (rs.next) do
puts rs.getString(1)
end
rs.close
stmt.close
conn.close
ソースコード(JRuby + JDBC Type4 + MySQL)
require 'rubygems'
require 'java'
require 'mysql-connector-java-5.1.22-bin.jar'
Java::com.mysql.jdbc.Driver
conn = java.sql.DriverManager.get_connection("jdbc:mysql://localhost:3306", "root", "P@ssW0rd")
stmt = conn.create_statement
rs = stmt.execute_query("SELECT 'Hello, JDBC Type4 World!' AS Message")
while (rs.next) do
puts rs.getString(1)
end
rs.close
stmt.close
conn.close |
require 'rubygems'
require 'java'
require 'mysql-connector-java-5.1.22-bin.jar'
Java::com.mysql.jdbc.Driver
conn = java.sql.DriverManager.get_connection("jdbc:mysql://localhost:3306", "root", "P@ssW0rd")
stmt = conn.create_statement
rs = stmt.execute_query("SELECT 'Hello, JDBC Type4 World!' AS Message")
while (rs.next) do
puts rs.getString(1)
end
rs.close
stmt.close
conn.close
実行方法
実行結果
Tags: JDBC, JDBC Type4
Categories: JDBC, JDBC Type1, JDBC Type4, JRuby