MyEclipse用JDBC连接PostgreSQL
来源:互联网 发布:淘宝有多少家天猫店 编辑:程序博客网 时间:2024/05/21 19:50
最近公司项目需要用到PostgreSQL,于是就折腾一下在阿里云上部署以及相关的开发工作(因为不方便直接使用阿里云演示,所以以下介绍使用的是虚拟机作为远程服务器)。工作中最幸运的事是什么?那就是看着自己以前写的博客,举一反三,完成任务。相关资料:
CentOS 7、阿里云CentOS 6.5安装Postgre 9.6
MyEclipse用JDBC连接Oracle数据库
1、首先,打开MyEclipse Database Explorer
新建连接,选择驱动,填写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。
2、根据图中所示填写好自己的信息,点击Test Driver测试驱动是否连接成功。
3、创建Java工程,将驱动加入Library
4、然后我们可以使用Navicat for PostgreSQL在PostgreSQL中插入一条记录,PostgreSQL的好处就是可以支持JSON格式,本次项目就是看中这一点,因此我插入记录的格式为JSON格式。
因为项目中我只需要通过JDBC对数据库进行insert和select的操作,所以创建表格、删除记录、删除表格等功能请读者自行研究。其实跟前面的差不多,只是SQL语句变一下就行。
通过图片可以看到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看一下表的最新情况
- MyEclipse用JDBC连接PostgreSQL
- JDBC连接postgresql
- jdbc连接postgresql
- jdbc连接Postgresql出错
- MyEclipse用JDBC连接Oracle数据库
- postgresql jdbc 连接数据库测试
- JDBC连接之PostgreSQL数据库
- postgreSQL jdbc(java连接)
- postgreSQL jdbc(java连接)
- JDBC连接数据库教程,postgreSQL
- myeclipse jdbc连接mysql数据库
- JDBC连接DB2、Oracle、MySQL、PostgreSQL
- JDBC连接DB2、Oracle、MySQL、PostgreSQL
- JDBC连接PostgreSQL数据库的若干问题
- Java实例(一) - JDBC连接postgresql
- 连接postgresql数据库的jdbc驱动选择
- java使用jdbc连接postgresql数据库
- JDBC连接postgreSQL数据库的步骤
- 学习PhantomJS笔记
- Spring boot 完美解决ajax跨域请求问题
- 深切悼念陈省身先生,孟道骥[2004-12-7]
- 2015输入一个字符串以#结尾,则输出此字符串中连续出现最长的数字串及其开始的下标;
- rev Codeforces Round #392 (Div. 2) 758E - Broken Tree
- MyEclipse用JDBC连接PostgreSQL
- [Machine Learning & Algorithm] 随机森林(Random Forest)
- linux-tail命令
- OpenCV 读取、修改、保存图像
- LeetCode 400. Nth Digit
- 欢迎使用CSDN-markdown编辑器
- 第六章 生命周期
- Ember 翻译——教程十:服务和功能
- 修改ubuntu的密码、主机名、用户名