

If you were looking for a simple Scala JDBC connection example, I hope this short article was helpful.

Val url = "jdbc:mysql://localhost/drupal7" Val url = "jdbc:mysql://hostname/database_name"Īnd if I was instead connecting to a database named drupal7, the MySQL URL would look like this instead: If you're new to JDBC and the MySQL URL shown above looks weird because I'm accessing the "mysql" database in the MySQL database server, remember that the general MySQL connection URL looks like this: Println("host, user = " + host + ", " + user)Īs you can see, this Scala JDBC database connection example looks just like Java JDBC, which you can verify from my very old JDBC connection example and JDBC SQL SELECT example. Val resultSet = statement.executeQuery("SELECT host, user FROM user") Val statement = connection.createStatement() create the statement, and run the select query there's probably a better way to do thisĬonnection = DriverManager.getConnection(url, username, password) connect to the database named "mysql" on the localhost * A Scala JDBC connection example by Alvin Alexander, In this example I'm connecting to a MySQL database server on my local computer, and then running a SQL SELECT query against the user table of the mysql database:
Mysql jdbc select example code#
Without any further introduction, here's the source code for a complete Scala class (an object, actually) that connects to a MySQL database using nothing but plain old JDBC. I just ran a simple JDBC connection and SQL SELECT test, and everything seems to work just as it does in Java. If you want to use a SQL database with your Scala applications, it's good to know you can still use the traditional Java JDBC programming library to access databases. Scala JDBC FAQ: How can I use the Java JDBC API in my Scala application? Contact me at (al) at valleyprogramming (dot) com for details. For an example of how I loaded the CSV into mySQL for Spark SQL tutorials, check this YouTube video and subscribe to our channel.

The trips table was populated with the Uber NYC data used in Spark SQL Python CSV tutorial. Want to work together? Your business can now hire me (Alvin Alexander) for small Scala and Flutter side projects. The Spark SQL with MySQL JDBC example assumes a mysql db named uber with table called trips. While Executing the executeQuery() method, we need to keep in mind some important facts. As we already discussed in the previous tutorials steps to create jdbc example, to do the select operation in JDBC we need to use the executeQuery() method.
