加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL新手入门

发布时间:2020-12-13 18:15:14 所属栏目:百科 来源:网络整理
导读:PostgreSQL是一款开源的数据库,从mysql被收购之后逐渐得到了广泛应用,可以说市面上的公司选择使用它,的确是一个大胆、创新的想法,postgresql拥有众多数据库的特性如orcale的sequence,还拥有很多商业数据库不具备的数据类型如几何类型,IP类型,并且也支

PostgreSQL是一款开源的数据库,从mysql被收购之后逐渐得到了广泛应用,可以说市面上的公司选择使用它,的确是一个大胆、创新的想法,postgresql拥有众多数据库的特性如orcale的sequence,还拥有很多商业数据库不具备的数据类型如几何类型,IP类型,并且也支持视图、触发器、数据库约束。如果觉得mysql太单调可以试试PostgreSQL,一定大有收获。

一 postgresql数据库以及客户端的安装

postgresql的下载地址是:http://www.postgresql.org/download/

pgadmin的下载地址是:http://www.pgadmin.org/download/

一般情况下postgresql可以成功安装,如果安装不了或者安装之后服务无法启动,可以检查一下电脑上是不是安装过mysql cluster数据库集群,如果安装过先卸载,重启电脑之后再安装一次就没问题了,至于pgadmin点几次下一步就能安装成功了


二 pgadmin的简单使用

1 连接数据库

填写相应信息,点确定即可

2 数据库与表的创建

也可以使用客户端工具添加主键、外键等


3 添加用户


三 使用jdbc操作postgresql

准备:pgsql的jdbc开发包(jar包)-------不能缺少

1 jdbc工具类

package org.lxh;

import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;

public class DBManager {
	public static final String DBDRIVER = "org.postgresql.Driver";
	public static final String DBURL = "jdbc:postgresql://localhost/mypgsql";
	public static final String DBUSER = "postgres";
	public static final String DBPASS = "root";
	private Connection conn = null;
	

	public Connection Creatconn() {

		try {
			Class.forName(DBDRIVER);

			conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
			return conn;

		} catch (Exception fe) {
			System.err.println("Creatconn(): " + fe.getMessage());
			return null;
		}

	}

	public void Release() throws SQLException {
		
		if (conn != null) {
			conn.close();
		}

	}

}

2 crud操作
package org.lxh;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

public class MyJunit {

	/**插入数据
	 * @throws Exception
	 */
	@Test
	public void testInsert() throws Exception {
		DBManager d=new  DBManager();
		Connection conn=d.Creatconn();
	    String sql="insert into student(name,password) values(?,?)";
	    PreparedStatement pstmt=conn.prepareStatement(sql);
	    pstmt.setString(1,"林志玲");
	    pstmt.setString(2,"123456");
	    int flag=pstmt.executeUpdate();
	    if(flag>0){
	    	System.out.println("数据插入成功");
	    }else{
	    	System.out.println("数据插入失败");
	    }
	    pstmt.close();
		d.Release();
	}
	/**删除记录
	 * @throws Exception
	 */
	@Test
	public void testDelete() throws Exception {
		DBManager d=new  DBManager();
		Connection conn=d.Creatconn();
	    String sql="delete from student where id=?";
	    PreparedStatement pstmt=conn.prepareStatement(sql);
	    pstmt.setInt(1,0);
	    int flag=pstmt.executeUpdate();
	    if(flag>0){
	    	System.out.println("数据删除成功");
	    }else{
	    	System.out.println("数据删除失败");
	    }
	    pstmt.close();
		d.Release();
	}
	/**修改记录
	 * @throws Exception
	 */
	@Test
	public void testUpdate() throws Exception {
		DBManager d=new  DBManager();
		Connection conn=d.Creatconn();
	    String sql="update student set name=? where id=?";
	    PreparedStatement pstmt=conn.prepareStatement(sql);
	    pstmt.setString(1,"苏有朋");
	    pstmt.setInt(2,2);
	    int flag=pstmt.executeUpdate();
	    if(flag>0){
	    	System.out.println("数据修改成功");
	    }else{
	    	System.out.println("数据修改失败");
	    }
	    pstmt.close();
		d.Release();
	}
	/**查询
	 * @throws Exception
	 */
	@Test
	public void testQuery() throws Exception {
		DBManager d=new  DBManager();
		Connection conn=d.Creatconn();
	    String sql="select * from student";
	    PreparedStatement pstmt=conn.prepareStatement(sql);
	    ResultSet rs=pstmt.executeQuery();
	    while(rs.next()){
	    	System.out.println("姓名:"+rs.getString("name")+",密码:"+rs.getString("password"));
	    }
	    pstmt.close();
		d.Release();
	}
}



注意事项:postgresql对大小写敏感,表、视图、序列的命令尽量使用小写,也不要使用关键字如user等。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读