在lift中使用mysql

Boot.scala
import _root_.net.liftweb.common._
import _root_.net.liftweb.util._
import _root_.net.liftweb.http._
import _root_.net.liftweb.sitemap._
import java.sql.{DriverManager, Connection}
import com.desu.model.Message
import net.liftweb.mapper._

class
Boot {
    def boot {
        if (!DB.jndiJdbcConnAvailable_?) {
            DB.defineConnectionManager(DefaultConnectionIdentifier, DBVendor)
        }
        // where to search snippet
        LiftRules.addToPackages("com.desu")
        Schemifier.schemify(true,    Schemifier.infoF _, Message)

        // Build SiteMap
        val entries = Menu(Loc("Home", List("index"), "Home")) ::
            Menu(Loc("update", List("update"), "The Update Page")) ::
            Nil
        LiftRules.setSiteMap(SiteMap(entries:_*))
    }
}

object DBVendor extends ConnectionManager {
def newConnection(name: ConnectionIdentifier): Box[Connection] = {
     try {
         Class.forName("com.mysql.jdbc.Driver")
         val conn = DriverManager.getConnection("jdbc:mysql://localhost/wstock?user=root&password=")
         Full(conn)
     } catch {
         case e : Exception => e.printStackTrace; Empty
     }

}

def releaseConnection(conn: Connection) {conn.close}

}
pom.xml

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.12</version>
</dependency>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章