首页 > 数据库技术 > 详细

postgresql 入门(含java、scala连接代码)

时间:2015-01-04 16:56:43      阅读:394      评论:0      收藏:0      [点我收藏+]

1、下载安装包

官网:http://www.postgresql.org/download/ 按自己需求,下载安装包,

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

2、安装

执行下载包,进行安装,请一定记住你输入的密码,如“pg”

3、打开数据库,创建表,插入数据

安装完成后,在开始菜单找到PostgreSQL 9.4 (x86)\pgAdmin III,打开 pgAdmin III,点击下图节点,输入密码“pg”

技术分享

连接后如图

技术分享

技术分享

点击 技术分享,在新打开的窗口输入下边语句,创建表,插入数据

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”,点“技术分享”查看数据

技术分享

技术分享

4、下载驱动

技术分享

在打开也找到

技术分享

打开psqljdbc,--“download”--然后找到下面

技术分享

5、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());
		}
	}
}

6、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
    }
  }
}

7、PostgreSQL 8.1 中文文档

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

 

postgresql 入门(含java、scala连接代码)

原文:http://www.cnblogs.com/piaolingzxh/p/4201216.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!