Scala and SBT are installed by default and have a basic understanding of SBT and know how to build a project with SBT.
Add dependency
To use the PostgreSQL database in Scala, you need to import PostgreSQL driver-related library files, PostgreSQL library files, can be downloaded to their official website, be sure to download and your Scala, JDK corresponding version. There are now two ways to add this PostgreSQL library when the Scala project is compiled:
1. Download the dependency library yourself, a jar file, and put this file under the lib\ in the SBT project directory
2. Instead of downloading this dependent library yourself, declare dependencies on this file in BUILD.SBT, and in the process of compiling SBT will automatically download the library file and add it. In the SBT help file, tell me how to add this dependent syntax
Librarydependencies + = GroupID% Artifactid% revision% configuration
So now the problem, I only know I need PostgreSQL, where to know what groupid, artifactid these things, to everyone a confluence of many library files of the site, search for PostgreSQL in this site, The GroupID and Artifactid of the PostgreSQL driver Library are given in the search results.
Based on this information, you know that you should write this dependency in the BUILD.SBT file.
Librarydependencies + = "Org.postgresql"% "PostgreSQL"% "9.4-1201-jdbc41"% "provided"
The whole BUILD.SBT is
name := "Task3"
version := "1.0"
scalaVersion := "2.10.5"
libraryDependencies += "org.postgresql" % "postgresql" % "9.4-1201-jdbc41" % "provided"
After this, the compilation should not be a problem, using the SBT package can be said to project into a jar. However, it still does not work, and errors such as class not found occur. Because the location of the PostgreSQL dependent library file needs to be added to the Java classpath, there is no problem with running it.
Use the PostgreSQL basic query
val conn_str = "jdbc: postgresql: // IP address: port number / database name"
classOf [com.mysql.jdbc.Driver]
// Using the previous sentence, there may be warning, because this is an expression, which can be replaced by the following
// Class.forName ("org.postgresql.Driver"). NewInstance
val conn = DriverManager.getConnection (conn_str, "user name", "password")
val conn = DriverManager.getConnection (conn_str)
try {
// Configure to be Read Only
val statement = conn.createStatement (ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
// Execute Query
val rs = statement.executeQuery ("SELECT quote FROM quotes LIMIT 5")
// Iterate Over ResultSet
while (rs.next) {
println (rs.getString ("quote"))
}
}
finally {
conn.close
}
Table Operations (Insert,update,delete)
val dbc = "jdbc: mysql: // localhost: 3306 / DBNAME? user = DBUSER & password = DBPWD"
classOf [com.mysql.jdbc.Driver]
// This is another way of writing, write the username and password together
val conn = DriverManager.getConnection (dbc)
val statement = conn.createStatement (ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)
// do database insert
try {
val prep = conn.prepareStatement ("INSERT INTO quotes (quote, author) VALUES (?,?)")
prep.setString (1, "Nothing great was ever achieved without enthusiasm.")
prep.setString (2, "Ralph Waldo Emerson")
prep.executeUpdate
}
finally {
conn.close
}
accessing PostgreSQL in Scala (using SBT)