Tuesday, August 27, 2013

Tutorial For Mysql In Scala

While programming languages such as PHP are developed with MySQL integration in mind, the Scala programming language require additional functionality. To connect Scala to a MySQL database, you must use Java as well. With Java and the Java MySQL connector, you can invoke a MySQL connection within the Scala language. With the Java connector installed, you can import the connector in Scala to invoke it. These languages are most typically used on Linux or Unix systems.


Instructions


1. Download MySQL Connector/J to your root folder. Open the Terminal, then type "tar xzvf mysqlconnector.tar.gz" (using the actual file name instead). Type "cd mysqlconnector," press "Enter," then type "./configure" and press "Enter." Type "make" and press "Enter." Type "make install" and press "Enter."


2. Open the Scala script that you want to use to connect to a MySQL database, in your preferred coding editor.


3. Type the lines of code:


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


val conn_str = "jdbc:mysql://localhost:3306/DBNAME?user=DBUSER&password=DBPWD"


classOf[com.mysql.jdbc.Driver]


val conn = DriverManager.getConnection(conn_str)








try {


val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,


ResultSet.CONCUR_READ_ONLY)


4. Replace the "val conn_str" line with the appropriate details for your database location and path. Type, on the following line, the query you want to use in the MySQL database. This is specific for each script.


5. Close each section of the script, then close the connection by using the commands:


finally {


conn.close








}

Tags: press Enter, MySQL database, Enter Type, Enter Type make, press Enter Type