Introduction to PostgreSQL (including Java, Scala connection code)

Source: Internet
Author: User
Tags postgresql


1. Download the installation package


Official website: http://www.postgresql.org/download/According to their own needs, download the installation package,



I downloaded a 32-bit version of Windows. Http://get.enterprisedb.com/postgresql/postgresql-9.4.0-1-windows.exe


2. Installation


Execute the download package and install it, please remember the password you entered, such as "PG"


3. Open database, CREATE TABLE, insert data


After installation is complete, find PostgreSQL 9.4 (x86) \pgadmin III on the Start menu, open Pgadmin III, click on the node, enter the password "PG"






After connection









Click, enter the bottom statement in the newly opened window, create the table, insert the data


CREATE TABLE users (
     uid    serial PRIMARY KEY ,
     name   varchar(40) NOT NULL CHECK (name <> ‘‘),
     pwd varchar(40) NOT NULL CHECK (name <> ‘‘),
     gender char(1),
     email varchar(40),
     birthday date     
);
INSERT INTO users VALUES  (1, ‘zxh‘, ‘zxhpwd‘, ‘M‘, ‘[email protected]‘, ‘1984-07-13‘);  
INSERT INTO users VALUES  (2, ‘zxh2‘, ‘zxh2pwd‘, ‘F‘, ‘[email protected]‘, ‘1984-07-13‘);


Expand the directory tree, select the table "Users", click "View data








4. Download driver





Also found in open






Open psqljdbc,--"Download"--and find the following





5. Java Link PostgreSQL
// psqlJDBC
// Publisher: PostgreSQL Global Development Group
// Driver address: http://jdbc.postgresql.org/download.html => http://jdbc.postgresql.org/download/postgresql-9.3-1102.jdbc41.jar
// Local download: http://files.cnblogs.com/piaolingzxh/postgresql-9.3-1102.jdbc41.jar.zip
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

public class postgres_jdbc {
public static void main (String [] args) {
try {
Class.forName ("org.postgresql.Driver"). NewInstance ();
String url = "jdbc: postgresql: // localhost: 5432 / postgres";
Connection con = DriverManager.getConnection (url, "postgres", "pg");
Statement st = con.createStatement ();
String sql = "select * from users";
ResultSet rs = st.executeQuery (sql);
ResultSetMetaData rsmd = rs.getMetaData ();
int columnCount = rsmd.getColumnCount ();
while (rs.next ()) {
for (int i = 1; i <= columnCount; i ++) {
System.out.print (rs.getString (i) + "\ t");
}
System.out.println ();
}
rs.close ();
st.close ();
con.close ();

} catch (Exception ee) {
System.out.print (ee.getMessage ());
}
}
}
6. Scala Connection Database
// psqlJDBC
// Publisher: PostgreSQL Global Development Group
// Driver address: http://jdbc.postgresql.org/download.html => http://jdbc.postgresql.org/download/postgresql-9.3-1102.jdbc41.jar
// Local download: http://files.cnblogs.com/piaolingzxh/postgresql-9.3-1102.jdbc41.jar.zip
import java.sql. {Connection, DriverManager, ResultSet};

object postgres_jdbc {
  val conn_str = "jdbc: postgresql: // localhost: 5432 / postgres"
  classOf [org.postgresql.Driver]
  def main (args: Array [String]) {
    //classOf[org.postgresql.Driver]

    val conn = DriverManager.getConnection (conn_str, "postgres", "pg")
    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 * FROM users")
      var columnCount = rs.getMetaData (). getColumnCount ();
      // Iterate Over ResultSet
      while (rs.next) {
        for (i <-1 to columnCount) {
          System.out.print (rs.getString (i) + "\ t");
        }
        System.out.println ();
      }
    } finally {
      conn.close
    }
  }
} 
7. PostgreSQL 8.1 Chinese documents


http://www.php100.com/manual/PostgreSQL8/






Introduction to PostgreSQL (including Java, Scala connection code)


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.