MySQL and Scala – Simple selects

So after about an hour of research and experimenting, I found out how to make Scala work with MySQL without too much hassle, I found a really helpful guide here. Going down the SBT route, I first created a project and all that, then inside of the build folder I created a Scala class that ensured that when I started building, I would have the right dependencies downloaded and ready.

import sbt._

class MyDataEngineProject(info: ProjectInfo)
    extends DefaultProject(info) {
  // Declare MySQL connector Dependency
  val mysql = "mysql" % "mysql-connector-java" % "5.1.17"
}

Once you have got this in project/build/MyDataEngineProject.scala, you are ready to start the connection stuff. In src/main/scala you can start creating your application here. I use the “def main” approach so when i start from the command line i simply pass in my parameters and I’m ready to go:

import java.sql.{Connection, DriverManager, SQLException, ResultSet}

object DataEngine {

	private var driverLoaded = false

	private def loadDriver()  {
		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance
			driverLoaded = true
		}catch{
			case e: Exception  => {
			    println("ERROR: Driver not available: " + e.getMessage)
			    throw e
			}
		}
	}

	def main(args: Array[String]) {
		if (args.length < 1){
			println("No arguments provided, exitting...")
			return
		}
		println ("Attempting to load MySQL JBDC Driver...")
		println
		this.synchronized {
		  if(! driverLoaded) loadDriver()
		}

		val conString = "jdbc:mysql://localhost:3306/db_name?user="+args(0)+"&password="+args(1)
		classOf[com.mysql.jdbc.Driver]
		val con = DriverManager.getConnection(conString)
		try{
			val handle = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
			val rs = handle.executeQuery("select * from auth limit 1")
			println ("Connection established...")
			while(rs.next){
				println("User Handle: "+rs.getString("handle"))
			}
		}catch{
			case e: Exception  => {
		    	println("ERROR: No connection: " + e.getMessage)
		    	throw e
		  	}
			case _ => println("A problem!")
		}finally{
			println("Closing connection and exiting...")
			con.close()
		}
  	}
}

Save the file as Main.scala and you can just type in run with your db username and password as space-separated arguments and you’re ready to go. Of course you will need to change some bits in here to reflect your database schema, but aside that it’s that simple. In my next post I will work on inserting data and updating data as well.

Moving to Scala

So after about 8 – 9 years of PHP and enjoying it thoroughly, I have decided to default to Scala pronounced {skah-lah}. I am not going to go into any reasons why I am moving but all I can say is that, I think the Lift Framework is amazing and Scala is a lot more elegant in my opinion, very expressive and perhaps my biggest reason is that I would like to expand those horizons, (I was getting a bit too comfortable in this space).

Using images in LaTeX

Note to self and perhaps others who might be wondering why your images are not displaying in your tex document.

Despite using the correct syntax:

 

 

\begin{figure}[htb]
    \begin{center}
        \includegraphics{image.png}
    \end{center}
    \caption{Fig 1: image}
    \label{fig1}
\end{figure}


You may find a box with with only the image path being displayed as your image. Please check that you are not using the “draft” argument at the top of your document when specifying your document class. A bit like

\documentclass[12pt,a4paper,draft]{article}

Make sure there is no “draft” in there so that it reads

\documentclass[12pt,a4paper]{article}

Spent about half an hour trying to figure out why everyone else’s images were displaying except mine.