MyEclipse用JDBC连接PostgreSQL

最近公司项目需要用到PostgreSQL,于是就折腾一下在阿里云上部署以及相关的开发工作(因为不方便直接使用阿里云演示,所以以下介绍使用的是虚拟机作为远程服务器)。工作中最幸运的事是什么?那就是看着自己以前写的博客,举一反三,完成任务。相关资料:
CentOS 7、阿里云CentOS 6.5安装Postgre 9.6
MyEclipse用JDBC连接Oracle数据库
1、首先,打开MyEclipse Database Explorer
Database
新建连接,选择驱动,填写PostgreSQL的地址、端口号、登录名和密码。选择JDBC路径。下载链接:https://jdbc.postgresql.org/download.html
版本说明:
This is the current version of the driver. Unless you have unusual requirements (running old applications or JVMs), this is the driver you should be using. It supports Postgresql 7.2 or newer and requires a 1.6 or newer JVM. It contains support for SSL and the javax.sql package. If you are using the 1.6 then you should use the JDBC4 version. If you are using 1.7 then you should use the JDBC41 version. If you are using 1.8 then you should use the JDBC42 versionIf you are using a java version older than 1.6 then you will need to use a JDBC3 version of the driver, which will by necessity not be current
大致意思就是,JDBC4、JDBC41、JDBC42对应Java1.6、Java1.7、Java1.8。
connect
2、根据图中所示填写好自己的信息,点击Test Driver测试驱动是否连接成功。
(此时可以通过左边工作台直接打开数据库)


3、创建Java工程,将驱动加入Library
build path
add
finish
4、然后我们可以使用Navicat for PostgreSQL在PostgreSQL中插入一条记录,PostgreSQL的好处就是可以支持JSON格式,本次项目就是看中这一点,因此我插入记录的格式为JSON格式。
JSON
因为项目中我只需要通过JDBC对数据库进行insert和select的操作,所以创建表格、删除记录、删除表格等功能请读者自行研究。其实跟前面的差不多,只是SQL语句变一下就行。
code
通过图片可以看到Navicat for PostgreSQL插入的JSON格式的记录,因为程序是先执行insert再执行select,所以这里还能看到一条后插入的记录。
完整代码:

package PostgreSQL_Conn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import net.sf.json.JSONObject;

public class PostgreSQL_Conn {

    static Connection m_aTC_connection = null;
    static Statement m_aTC_statement = null; 

    private static void dbConnect(){    
        try {
            Class.forName("org.postgresql.Driver");
            m_aTC_connection = DriverManager.getConnection(
               "jdbc:postgresql://192.168.133.135:5432/postgres","postgres", "admin");
            m_aTC_statement = m_aTC_connection.createStatement(); 
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
    private static void dbInsert(){
        dbConnect();
        JSONObject t_aTC_json = new JSONObject();
        t_aTC_json.put("China", "BeiJIng");
        t_aTC_json.put("USA", "Washington DC");
        t_aTC_json.put("UK", "London");        
        String t_str_sql = "INSERT INTO public.dbtest (f_id,f_time,f_deviceid,f_json) " + 
        "VALUES (2,'201701191707','2','" + t_aTC_json + "');";
        try {
            m_aTC_statement.executeUpdate(t_str_sql);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        dbClose();
    }
    private static void dbSelect(){
        dbConnect();
         ResultSet t_aTC_rs;
        try {
            String t_str_sql = "select * from public.dbtest";
            t_aTC_rs = m_aTC_statement.executeQuery(t_str_sql);
            while(t_aTC_rs.next())
             {
               System.out.println(t_aTC_rs.getString("f_json"));  
             }
             t_aTC_rs.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }      
        dbClose();
    }
    private static void dbClose(){
        if(m_aTC_connection != null)
        {
            try {
                m_aTC_statement.close();
                m_aTC_connection.close();           
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        dbInsert();
        dbSelect();     

这里说明一个情况,PostgreSQL默认表是在public模式下面的,所以定位一个表用的是public.Tablename,这一点跟Oracle、MySQL有点不一样。
最后可以再次打开Navicat for PostgreSQL看一下表的最新情况
latest

猜你喜欢

转载自blog.csdn.net/u012138272/article/details/54646207