Skip to content

Commit

Permalink
Night 1 --- I can interact with MySQL via JDBC.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtrupiano committed Apr 7, 2009
0 parents commit c140fa6
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README
@@ -0,0 +1,12 @@
DETAILS

I'm writing an ORM in Scala as an exercise for the Bmore Hackers language study group. As a disclaimer, I am in the process of learning Scala. Do not interpret any of this code as a best practice.

INSTRUCTIONS
./run.sh expects the mysql JDBC adapter to be installed in the scorm directory. Download http://dev.mysql.com/downloads/connector/j/5.1.html and unpack it so that all of the contents sit in the mysql-connector-java-5.1.7/ directory. Then you can run the script with ./run.sh

* Before you can run the script, you'll need to create the database. mysqladmin -u root create scorm
* Next, run "scala -classpath mysql-connector-java-5.1.7/mysql-connector-java-5.1.7-bin.jar jdbc.scala" --- this is the sample file that we started the project with. It will create a table articles with id, title and body fields. It will also insert a single record into the database.
* Now you can run scorm.scala (via ./run.sh). This will simply do the equivalent of Article.find(1) (as in ActiveRecord) and print out the details.

_Everything_ is hard-coded at this point.
63 changes: 63 additions & 0 deletions jdbc.scala
@@ -0,0 +1,63 @@
import java.sql._

// Load the JDBC Driver
Class.forName("com.mysql.jdbc.Driver").newInstance()

// Establish a connection to a database called test on localhost
// this will login as root with a blank password, so adjust as necessary
// The database should exist, so create from the commmand line with:
// mysqladmin -u root create test
val con = DriverManager.getConnection("jdbc:mysql:///scorm", "root", "")

try {

if(!con.isClosed()) {
println("Successfully connected to MySQL server using TCP/IP...")
}

// To execute a SQL Query, you have to create Statement object
// and then call executeUpdate on it, passing the SQL query as a String
var stmt = con.createStatement()
stmt.executeUpdate("DROP TABLE IF EXISTS articles")
stmt.executeUpdate("CREATE TABLE articles ("+
"id int(11) NOT NULL auto_increment, "+
"title varchar(255), " +
"body tinytext, "+
"PRIMARY KEY (id))")
stmt.close()

// A prepared statement allows you to do use query parameters
var pstmt = con.prepareStatement(
"INSERT INTO articles (title, body) VALUES (?, ?)",
Statement.RETURN_GENERATED_KEYS)

// Set the values for the query parameters
// Yes, JDBC uses a 1-based index, instead of 0-based
pstmt.setString(1, "First Post!")
pstmt.setString(2, "Blah Blah Blah")

// Execute the insert
pstmt.execute()

// Generated Keys will give us access to the ID that was created by the INSERT
var rs = pstmt.getGeneratedKeys()
if(rs.next()) {
println("Created Article #"+rs.getInt(1))
}

pstmt = con.prepareStatement("SELECT * FROM articles")

// When you call executeQuery, it returns a ResultSet
// which you can iterate over to get each row
rs = pstmt.executeQuery()
while(rs.next()) {
println("Article id="+rs.getInt("id")+
" title="+rs.getString("title")+
" body="+rs.getString("body"))
}

} catch {
case e => e.printStackTrace()
} finally {
con.close()
}
2 changes: 2 additions & 0 deletions run.sh
@@ -0,0 +1,2 @@
#!/bin/sh
scala -classpath mysql-connector-java-5.1.7/mysql-connector-java-5.1.7-bin.jar scorm.scala
40 changes: 40 additions & 0 deletions scorm.scala
@@ -0,0 +1,40 @@
import java.sql._

// Load the JDBC Driver
Class.forName("com.mysql.jdbc.Driver").newInstance()

class Article(
var values: List[String]
) {
val fields = List("id", "title", "body")

// Empty Constructor sets all values to nothing
def this() = this(List("", "", ""))

def print = {
println("id = " + this.values.apply(0) + ", title = " + this.values.apply(1) + ", body = " + this.values.apply(2))
}
}

object ArticleFinder {
def find(id : Int) : Article = {
// Open the connection
val con = DriverManager.getConnection("jdbc:mysql:///scorm", "root", "")

var pstmt = con.prepareStatement("SELECT * FROM articles WHERE id = ?")
pstmt.setString(1, id.toString)

// When you call executeQuery, it returns a ResultSet
// which you can iterate over to get each row
val rs = pstmt.executeQuery()
rs.next()
var lst : List[String] = List(rs.getInt("id").toString, rs.getString("title"), rs.getString("body"))

// Close the connection
con.close()

new Article(lst)
}
}

ArticleFinder.find(1).print

0 comments on commit c140fa6

Please sign in to comment.