Writing SQL in Coderpad

I have an interview coming up in which I will write code in Coderpad. What is the syntax to write SQL in Coderpad? (note Coderpad with an "r", not Codepad).

First, you select a language for Coderpad. I am selecting Scala.

Here is a database they provide:

+--------------------+-----------+---------------+ | Table Name | Row Count | Table Size KB | +--------------------+-----------+---------------+ | departments | 5 | 16.0000 | | employees | 6 | 32.0000 | | employees_projects | 5 | 48.0000 | | projects | 3 | 16.0000 | +--------------------+-----------+---------------+ 

Here is Scala code that does work:

object Solution extends App { for (i <- 0 until 5) println("Hello, World!") } 

I want to run a simple SQL query.

SELECT * FROM employees; 

Note: I can write complex SQL in Spark with Scala. I have written hundreds of Databricks Scala notebooks. So, I do not need a primer on Scala or SQL.

I simply need the correct SYNTAX to write any simple SQL query in CoderPad just to get me started. Thank you.

In other words, how do I insert a SQL statement into a Scala Object?

I tried:

object Solution extends App { //for (i <- 0 until 5) println("Hello, World!") val myTest = SQL("select * from employees").as(mapping *) myTest.take(1) } 

But get this error:

Solution.scala:6: error: not found: value SQL val myTest = SQL("select * from employees").as(mapping *) ^ Solution.scala:6: error: not found: value mapping val myTest = SQL("select * from employees").as(mapping *) 
5

2 Answers

I'm the guy that makes CoderPad. Currently, you can't connect to MySQL databases in other language environments. That's something we want to do eventually, though.

0

You'll need to connecto to your database just as you would do that in Java

They should provide you the database information to connect, so you need to do the following steps (writing from memory, sorry is something is missing)

import the required classes:

import java.sql.DriverManager import java.sql.Connection 

Then define the properties to connect:

val driver = "com.mysql.jdbc.Driver" val url = "jdbc:mysql://whateverServer/whateverDatabase" val username = "user" val password = "password" 

Create the driver and then create the connection:

Class.forName(driver) val connection = DriverManager.getConnection(url, username, password) 

Then the statement and then run the query itself:

val statement = connection.createStatement() val resultSet = statement.executeQuery("select * from employees") 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like