Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Night 1 --- I can interact with MySQL via JDBC.
- Loading branch information
0 parents
commit c140fa6
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
scala -classpath mysql-connector-java-5.1.7/mysql-connector-java-5.1.7-bin.jar scorm.scala |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |