postgresql 入門(含java、scala鏈接代碼)

一、下載安裝包

官網:http://www.postgresql.org/download/ 按本身需求,下載安裝包,php

我下載的windows版32位的。http://get.enterprisedb.com/postgresql/postgresql-9.4.0-1-windows.exehtml

二、安裝

執行下載包,進行安裝,請必定記住你輸入的密碼,如「pg」java

三、打開數據庫,建立表,插入數據

安裝完成後,在開始菜單找到PostgreSQL 9.4 (x86)\pgAdmin III,打開 pgAdmin III,點擊下圖節點,輸入密碼「pg」sql

鏈接後如圖數據庫

點擊 ,在新打開的窗口輸入下邊語句,建立表,插入數據windows

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', 'piaolingzxh@qq.com', '1984-07-13');  
INSERT INTO users VALUES  (2, 'zxh2', 'zxh2pwd', 'F', 'piaolingzxh@qq2.com', '1984-07-13');

展開目錄樹, 選中表「users」,點「」查看數據post

四、下載驅動

在打開也找到ui

打開psqljdbc,--「download」--而後找到下面url

五、java連接postgresql

//psqlJDBC 
//Publisher :	PostgreSQL Global Development Group
//驅動地址:http://jdbc.postgresql.org/download.html => http://jdbc.postgresql.org/download/postgresql-9.3-1102.jdbc41.jar
//本地下載: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());
		}
	}
}

六、scala鏈接數據庫

//psqlJDBC 
//Publisher : PostgreSQL Global Development Group
//驅動地址:http://jdbc.postgresql.org/download.html => http://jdbc.postgresql.org/download/postgresql-9.3-1102.jdbc41.jar
//本地下載: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
    }
  }
}

七、PostgreSQL 8.1 中文文檔

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

相關文章
相關標籤/搜索