HowTo configure lift with MySQL

From Lift

Jump to: navigation, search

When creating a new lift webapp with Maven, Derby is added by default. Using MySQL is a viable alternative, eg. when Jetty fails to work with the embedded Derby DB after restarting the webapp.

To use MySQL, first install MySQL and create a database, eg. via Command Line tool and "create database name-of-yourdatabase".

Next we have to include the mysql-connector: Edit your pom.xml and add this:

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

NOTE: The version namespace: <version>5.0.8</version> is for the myql-connector-java NOT your MySQL version number.

You can replace the derby dependency if you don't need it otherwise.

Next edit bootstrap.liftweb.Boot.scala:

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

All you need to change is the driver name and the connection url, replace "name-of-your-database" and "mysql-password". Thats it.

The imports we need in bootstrap.liftweb.Boot.scala are:

import net.liftweb.mapper.{ConnectionManager,ConnectionIdentifier}
import java.sql.{Connection, DriverManager}
Personal tools