how to execute jruby application on cloudfoundry (vcap)2shinodas/cloudfoundry/20120215...

17
How to connect with DB using JRuby and JDBC JRubyJDBCVCAP上のDBに接続する方法 2012/02/15 () CloudFoundry JP Special Meetup Yoshihiko hara / 原 嘉彦 ( [email protected] ) This time it's DB!! How to execute JRuby Application on CloudFoundry (vcap) 2

Upload: others

Post on 03-Jun-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

How to connect with DB using JRuby and JDBCJRubyとJDBCでVCAP上のDBに接続する方法

2012/02/15 (水)

CloudFoundry JP Special Meetup

Yoshihiko hara / 原 嘉彦 ( [email protected] )

This time it's DB!!

How to execute JRuby Application on CloudFoundry (vcap)2

Page 2: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

Attention! / 注目!

Today, Please memorize this URL and go home!

今日は、このURLを憶えて帰ってください。

http://www.din.or.jp/~shinodas/cloudfoundry/20120215_announcement_document.html

上記の場所で、このドキュメントとサンプルアプリを公開しています。

This document and sample application are exhibited at the above-mentioned place.

Page 3: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

Self introduction / 自己紹介 Who am I? / 何者? : Yoshihiko Hara / 原 嘉彦

twitter → GORO_Neko

mail → [email protected]

My Job? / 日頃何やってるヒト? :I am working for IT company.

I am making Web-application using Java, Ruby, and other languages.

IT 企業で Ruby や Java で Web アプリ作ってます。

CloudFoundryをターゲットにしたアプリ開発の仕事がしたいなぁ

I wish that work of the application development which targeted CloudFoundry can be performed.

Page 4: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

Contents / 目次1. How to connect with DB from JRuby-applicationJRuby JRubyアプリからDBに接続する方法

2. How to get the information for connecting with DB

DBに接続するための情報を得る方法

3. The example of the source code which execute "Create Table" Create Table を行わせるコードの例

4. The example of the source code which execute "Drop Table" Drop Table を行わせるコードの例

5. The example of the source code which execute "Create Data" Create Data を行わせるコードの例

6. The example of the source code which execute "Read Data List" Read Data List を行わせるコードの例

7. The example of the source code which execute "Update Data" Update Data を行わせるコードの例

8. The example of the source code which execute "Delete Data" Delete Data を行わせるコードの例

Page 5: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

0. About How to execute JRuby Application on CloudFoundry (vcap) CloudFoundry(vcap)上でJRubyアプリを実行する方法に関して

Please see the document and source code which are

exhibited on the following pages.

以下のページで公開しているドキュメントとソースコードを見てください。

http://www.din.or.jp/~shinodas/cloudfoundry/20120119_announcement_document.html

It is easy that you execute JRuby Application on CloudFoundry (vcap)!!

簡単だよ!!

Page 6: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

1. How to connect with DB from JRuby-applicationJRuby JRubyアプリからDBに接続する方法

Use JDBC Driver / JDBCドライバを使え → Copy the Jar file of JDBC-Driver to <application-root>/WEB-INF/lib

<application-root>/WEB-INF/lib に JDBC Driver の Jarをコピーする

(1/2)

<application-root>  ┗━WEB-INF     ┣━ web.xml     ┣━ xxx.rb     ┗━lib  ┣━ jruby-complete-x.x.x.jar  ┣━ jruby-rack-x.x.x.jar       ┗━ mysql-connector-java-x.x.x-bin.jar

Jar file of JDBC Driver for MySQL

When DB is PostgreSQL, you use postgresql-x.x-x.jdbc4.jar

DB が PostgreSQL の場合はpostgresql-x.x-x.jdbc4.jar を使います

Page 7: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

1. How to connect with DB from JRuby-applicationJRuby JRubyアプリからDBに接続する方法

How to use JDBC Driver / JDBCドライバの使い方 → Write "declaration using JDBC" to the beginning of a Ruby script

   Ruby スクリプトの冒頭に JDBC を呼び出す宣言を書け

(2/2)

# [Example of MySQL]require 'rubygems'require 'rack'require "java"import com.mysql.jdbc.Driverimport java.sql.DriverManagerimport java.lang.System

# [Example of PostgreSQL]require 'rubygems'require 'rack'require "java"import org.postgresql.Driverimport java.sql.DriverManagerimport java.lang.System

Change the import package name of the driver by DB which you use

使用するDBにより、importすべきドライバーのパッケージ名を変えます

Page 8: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

2. How to get the information for connecting with DB DBに接続するための情報を得る方法 (1/2)

DB on CloudFoundry (vcap) is called "service."

CloudFoundry(vcap)上のDBは「サービス」と呼ばれる  By binding "service", the application can use DB.

サービスをバインドすることで、アプリケーションはDBを利用できる The information for accessing "service", including for example, URL,

a Port number, a table name, a user name, a password for access, etc., is

written in the VCAP_SERVICES environment variable.

「サービス」にアクセスするための情報(例えばURL、Port番号、テーブル名、

アクセス用のユーザ名・パスワード等)は、VCAP_SERVICES環境変数に書き

込まれている。 The value of a VCAP_SERVICES environment variable is JSON form.

VCAP_SERVICES環境変数の値はJSON形式である。

Page 9: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

2. How to get the information for connecting with DB DBに接続するための情報を得る方法

require 'rubygems'require "json"class DBInfo def initialize(env) # Get the information for the connection with MySQL # from "ENV" environment variable. services = JSON.parse(env['VCAP_SERVICES']) mysqlKey = services.keys.select{|s| s =~ /mysql/i}.first mysql = services[mysqlKey].first['credentials'] # Assemble URI for connecting with MySQL @db_url = "jdbc:mysql://#{mysql['host']}:#{mysql['port']}/#{mysql['name']}" @username = mysql['username'] @password = mysql['password'] end attr_accessor :db_url, :username, :passwordend

(2/2)

Example of source code that get the information

for the connection with MySQL from "VCAP_SERVICES" environment variable.

"VCAP_SERVICES" 環境変数から MySQL との接続情報を得るソースコードの例

When parsing the data of JSON form, it is easy if the "json" library of Ruby is used.

JSON形式のデータをパースする場合は、Ruby の "json" ライブラリを利用すると楽です

Page 10: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

3. The example of the source code which execute "Create Table" Create Table を行わせるコードの例

1. Connects with DB / DB に接続する

2. write SQL which execute Create Table using preparedStatement

prepareStatement を用いて Create Table を行う SQL を書く

3. Execute SQL / SQLを実行する

4. Disconnects with DB / DBとの接続を切る

URI of DB # [Example of “Create Table” Code] password for DB Access con = DriverManager.getConnection(db_uri, username, password) st = con.prepareStatement( username for DB Access "create table my_table01 (number INT(4) NOT NULL AUTO_INCREMENT, name text NOT NULL, PRIMARY KEY (number))") st.executeUpdate() st.close() SQL con.close()

Page 11: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

4. The example of the source code which execute "Drop Table" Drop Table を行わせるコードの例

1. Connects with DB / DB に接続する

2. write SQL which execute Drop Table using preparedStatement

prepareStatement を用いて Create Table を行う SQL を書く

3. Execute SQL / SQLを実行する

4. Disconnects with DB / DBとの接続を切る

URI of DB # [Example of “Drop Table” Code] password for DB Access con = DriverManager.getConnection(db_uri, username, password) st = con.prepareStatement("drop table my_table01") st.executeUpdate() username for DB Access st.close() SQL con.close()

Page 12: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

5. The example of the source code which execute "Create Data" Create Data を行わせるコードの例

1. Connects with DB / DB に接続する

2. write SQL which execute Create Data using preparedStatement

prepareStatement を用いて Create Data を行う SQL を書く

3. Execute SQL / SQLを実行する

4. Disconnects with DB / DBとの接続を切る

URI of DB password for DB Access # [Example of “Create Data” Code] con = DriverManager.getConnection(db_uri, username, password) st = con.prepareStatement( "insert into my_table01 (name) values ('#{name}')") username for DB Access st.executeUpdate() st.close() SQL con.close()

Page 13: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

6. The example of the source code which execute "Read Data List" Read Data List を行わせるコードの例

1. Connects with DB / DB に接続する

2. write SQL which execute Read Data List using createStatement

createStatement を用いて Read Data List を行う SQL を書く

3. Execute SQL and get the list of Data

 SQLを実行し Data の一覧を得る

4. Disconnects with DB / DBとの接続を切る

URI of DB password for DB Access # [Example of “Read Data List” Code] con = DriverManager.getConnection(db_uri, username, password) st = con.createStatement() resultset = st.executeQuery("select * from my_table01") username for DB Access st.close() con.close() Data List ( java.sql.ResultSet ) SQL

Page 14: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

7. The example of the source code which execute "Update Data" Update Data を行わせるコードの例

1. Connects with DB / DB に接続する

2. write SQL which execute Read Data List using createStatement

prepareStatement を用いて Update Data を行う SQL を書く

3. Execute SQL / SQLを実行する

4. Disconnects with DB / DBとの接続を切る

URI of DB password for DB Access # [Example of “Update Data” Code] con = DriverManager.getConnection(db_uri, username, password) st = con.prepareStatement(           username for DB Access "update my_table01 set name = '#{name}' where number = #{int_number}") st.executeUpdate() st.close() con.close() SQL

Page 15: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

8. The example of the source code which execute "Delete Data" Delete Data を行わせるコードの例

1. Connects with DB / DB に接続する

2. write SQL which execute Delete Data using preparedStatement

prepareStatement を用いて Delete Data を行う SQL を書く

3. Execute SQL / SQLを実行する

4. Disconnects with DB / DBとの接続を切る

URI of DB username for DB Access # [Example of “Delete Data” Code] password for DB Access con = DriverManager.getConnection(db_uri, username, password) st = con.prepareStatement("delete from my_table01 where number = #{int_number}") st.executeUpdate() st.close() SQL con.close()

Page 16: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

Appendix: URL for Download Page of an indispensable file 付録: 必須ファイルの入手先

Product name Download Page(URL)

JRuby http://jruby.org/download

jruby-rack http://repository.codehaus.org/org/jruby/rack/jruby-rack/

MySQL Connector/J http://dev.mysql.com/downloads/connector/j/

今回の検証では以下の 3 ファイルを使いました・jruby-complete-1.6.5.jar・jruby-rack-0.9.7.jar・mysql-connector-java-5.1.18-bin.jar

The following three files were used in this verification・jruby-complete-1.6.5.jar・jruby-rack-0.9.7.jar・mysql-connector-java-5.1.18-bin.jar

Page 17: How to execute JRuby Application on CloudFoundry (vcap)2shinodas/cloudfoundry/20120215 CloudFoundry_and_ JRuby_2... · How to connect with DB using JRuby and JDBC JRubyとJDBCでVCAP上のDBに接続する方法

Finish / おしまい

御清聴ありがとうございました!!

ぐぅぐぅ

… zzz

Thank you for your listening!!