Access postgresql (using sbt) and scalapostgresql in scala

Source: Internet
Author: User

Access postgresql (using sbt) and scalapostgresql in scala

Scala and SBT have been installed by default, and have a basic understanding of sbt, know how to build a project with sbt.

Add dependency

To use a postgresql database in scala, You need to import the postgresql Driver-related library files and postgresql library files, which can be downloaded on its official website, be sure to download the version corresponding to your scala and Jdk. There are two ways to add this postgresql library during scala project Compilation:
1. Download the dependent library by yourself. It is a jar file and put this file under the lib \ directory of the sbt project directory.
2. You do not need to download this dependent library by yourself. Declare the dependency file in build. sbt. sbt will automatically download this library file during compilation and add this library. The sbt help file shows how to add the dependency syntax.

LibraryDependencies + = groupID % artifactID % revision % configuration

Now, the question is, I only know what postgresql is needed, where do I know what groupID and artifactID are? I will give you a website that integrates many library files and search for postgresql on this website, the groupID and artifactID of the postgresql driver library are provided in the search results.

Based on this information, we know that the dependency should be written in the build. sbt file.

LibraryDependencies + = "org. postgresql" % "postgresql" % "9.4-1201-jdbc41" % "provided"

The entire build. sbt is

name := "Task3"version := "1.0"scalaVersion := "2.10.5"libraryDependencies += "org.postgresql" % "postgresql" % "9.4-1201-jdbc41" % "provided"

After this is written, the compilation should not go wrong. You can use the sbt package to package the project into a jar. But it still cannot be run. An error such as class not found may occur during running. Because you also need to add the location of the postgresql dependent library file to the Java classpath, so that the running will not go wrong.

Use postgresql basic query
Val conn_str = "jdbc: postgresql: // ip address: Port Number/Database Name" classOf [com. mysql. jdbc. driver] // the previous sentence may contain a warning. Because this is an expression, you can replace it with the following // Class. forName ("org. postgresql. driver "). newInstanceval 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.exe cuteQuery ("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 to write the user name 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.exe cuteUpdate} finally {conn. close}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.