MySQL
Web framework を使ってMySQLにアクセスします。
使用するのはseedoフレームワークとの組み合わせ。
seedoフレームワークは、seedo1.2.2.jarを WEB-INF/libディレクトリのなかに入れる。
WEB-INF/classesディレクトリのなかにenv.properties ファイルを入れる。
DB接続の定義は、MySQLのJDBCドライバを使う。
connection-jndi を off にすることで、seedoフレームワーク独自のコネクションプールを使う。
TOMCATのコネクションプールを使う場合は、connection-jndiはon にして、
jndi-name = jdbc/mysql にする。
TOMCAT/conf/context.xml ファイルにDB接続定義を記述してもよい。
env.properties ファイル
driver = com.mysql.jdbc.Driver #driver = oracle.jdbc.driver.OracleDriver dsn = jdbc:mysql://127.0.0.1:3306/seedo?useUnicode=true&characterEncoding=UTF-8 #dsn = jdbc:oracle:thin:@127.0.0.1:1521:ORCL user = scott password = tiger connection-jndi = off jndi-name = jdbc/mysql #jndi-name = jdbc/myoracle
EMP表に対してSELECT文を発行します。ENAMEフィールドでソートします。
MySQLにテスト用のDBを作る手順は、過去のBLOGにあるので参考に。
HelloWorldDb1.scala のソースコード
package ews import java.util.{HashMap,Date} import scala.collection.JavaConversions._ import scala.collection.immutable._ import scala.xml._ import seedo.database._ class HelloWorldDb1 { var tag:String = null // Replaced original tag def setTag(tg:String): Unit = {tag = tg} var parameterMap:HashMap[String,Array[String]] = null // Parameter from request of servlet def setParameterMap(pm:HashMap[String,Array[String]]) : Unit = {parameterMap = pm} // One record -> TD tag def recx(record:Array[Any]) :Node = { var nodes = Queue.empty[Node] record.foreach{(fx) => nodes += <TD>{fx}</TD>} return <TR>{nodes}</TR> } // Function without argument def tb : String = { val sql = new Db val re = sql.executeQuery("select * from emp order by ename") sql.close var nodes = Queue.empty[Node] re.foreach{(rec) => nodes += recx(rec)} val xml = <table>{nodes}</table> return "HelloWorldDb1 " + new Date + xml.toString } }
HTMLファイルは、helloworlddb1.html
今回は、DIVタグ内の2つのSPANタグの中身は丸ごと、SELECT文の検索結果に置換されます。
<!doctype html> <HTML> <HEAD> <title>Home</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </HEAD> <BODY> <h2>Example4</h2> The current time is <div class="seedo:ews.HelloWorldDb1:tb"> <div>The current time is <span class="seedo:ews.HelloWorld3:tb:999999999">now</span>.</div> <div>The current time is <span class="seedo:ews.HelloWorld3:tb:こんばんわ">now</span>.</div> </div>. </BODY> </HTML>
結果
URLは、
http://127.0.0.1:8080/toro/html/helloworlddb1.html?sw=4
結果は見ての通り。
コメントを残す
コメントを投稿するにはログインしてください。