Java链接PostgreSql数据库及基本使用
Posted _码农耕地人
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java链接PostgreSql数据库及基本使用相关的知识,希望对你有一定的参考价值。
我是应用Java封装的思想将所有的方法封装到了一个类里。
一)准备工作:
1、下载链接需要的jar包。链接
选择最新版本即可。
2、下载之后添加到模块里:
3、创建一个工具类Util,书写空参构造,用于对数据库的全部操作。
二)链接
所需内容:数据库名,端口号,数据库地址,数据库用户名,密码
public static Connection Connect()
Connection c = null;
try
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://服务器地址,本机写127.0.0.1:服务器端口号,默认5432/链接的数据库名",
"数据库用户名", "数据库密码");
catch (Exception e)
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
System.out.println("Opened database successfully");
return c; //记得返回一下这个对象,后面一直在用
二)查询:
普通版本查询:数据库有三个字段,时间time、地点location、温度temperature
public static void select()
//与数据库建立链接
Connection c = Util.Connect();
Statement stmt = null;
try
stmt = c.createStatement();
String sql = "SELECT* FROM tmps;";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next())
Date date = rs.getDate(1);
String location = rs.getString("location");
float temperature = rs.getFloat("temperature");
System.out.println("date" + date);
System.out.println("location:" + location);
System.out.println("temperature:" + temperature);
//关流操作
rs.close();
stmt.close();
c.close();
catch (SQLException throwables)
throwables.printStackTrace();
下图中这种方法属于进阶方法,只需要输入sql语句即可将任意表中的数据都按照map集合的方式返回
public static List<HashMap<String,Object>> Select(String sql)
//1、与数据库建立链接
Connection c = Util.Connect();
//2、创建操作对象
Statement stmt = null;
//3、创建返回最终查询的数据集合
List<HashMap<String ,Object>> list=new ArrayList<>();
try
//2.1、初始化操作对象
stmt = c.createStatement();
//4、执行需要执行的sql语句
ResultSet rs = stmt.executeQuery(sql);
//3.1开始封装返回的对象
ResultSetMetaData metaData = rs.getMetaData();//获取全部列名
int columnCount = metaData.getColumnCount();//列的数量
//5、读取数据
while (rs.next())
HashMap<String,Object> map=new HashMap<>();
for (int i = 1; i <= columnCount; i++)
//getColumnName获取列名
String name = metaData.getColumnName(i);
//获取对应的元素
Object object = rs.getObject(i);
map.put(name,object);
list.add(map);
//6、关流操作
rs.close();
stmt.close();
c.close();
catch (SQLException throwable)
throwable.printStackTrace();
return list;
三)添加:
返回值是bool类型,表示是否添加成功。
***需要比查询多添加一句***
connect.setAutoCommit(false);
public static Boolean Insert(String sql)
//1、与数据库建立链接
Connection connect = Util.Connect();
//2、创建操作对象
Statement stmt = null;
int count = 0;
try
//2.1、初始化创建对象
stmt=connect.createStatement();
//3、添加特殊语句。
connect.setAutoCommit(false);//之前不用
//4、执行添加操作
count = stmt.executeUpdate(sql);
//5、关流
stmt.close();
connect.commit();
connect.close();
catch (SQLException throwables)
throwables.printStackTrace();
return count!=0;
四)删除数据:
public void Delete(String sql)
//1、链接数据库
Connection c = this.Connect();
Statement stmt = null;
try
c.setAutoCommit(false);
stmt = c.createStatement();
stmt.executeUpdate(sql);
c.commit();
c.close()
stmt.close();
catch (SQLException throwable)
throwable.printStackTrace();
五)封装之后的代码总和:
封装类:
package postSQL.Util;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Util
private final Connection connect;
private final String userName;
private final String passWord;
private final String ipAddress;
private final String databaseName;
private final String port;
//构造方法
public Util(String userName, String passWord, String ipAddress, String databaseName, String port)
this.userName = userName;
this.passWord = passWord;
this.ipAddress = ipAddress;
this.databaseName = databaseName;
this.port = port;
this.connect = this.Connect();
//建立链接
private Connection Connect()
Connection c = null;
try
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://" + this.ipAddress + ":" + this.port + "/" + this.databaseName,
this.userName, this.passWord);
catch (Exception e)
e.printStackTrace();
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
return c;
//关流操作
public void close()
Connection c = this.connect;
try
c.close();
catch (SQLException throwables)
throwables.printStackTrace();
//查询
public List<HashMap<String, Object>> Select(String sql)
//1、与数据库建立链接
Connection c = this.connect;
//2、创建操作对象
Statement stmt = null;
//3、创建返回最终查询的数据集合
List<HashMap<String, Object>> list = new ArrayList<>();
try
//2.1、初始化操作对象
stmt = c.createStatement();
//4、执行需要执行的sql语句
ResultSet rs = stmt.executeQuery(sql);
//3.1开始封装返回的对象
ResultSetMetaData metaData = rs.getMetaData();//获取全部列名
int columnCount = metaData.getColumnCount();//列的数量
//5、读取数据
while (rs.next())
HashMap<String, Object> map = new HashMap<>();
for (int i = 1; i <= columnCount; i++)
//getColumnName获取列名
String name = metaData.getColumnName(i);
//获取对应的元素
Object object = rs.getObject(i);
map.put(name, object);
list.add(map);
//6、关流操作
rs.close();
stmt.close();
//c.close();
catch (SQLException throwable)
throwable.printStackTrace();
return list;
//插入操作
public Boolean Insert(String sql)
//1、与数据库建立链接
Connection connect = this.connect;
//2、创建操作对象
Statement stmt = null;
int count = 0;
try
//2.1、初始化创建对象
stmt = connect.createStatement();
//3、添加特殊语句。
connect.setAutoCommit(false);//之前不用
//4、执行添加操作
count = stmt.executeUpdate(sql);
//5、关流
stmt.close();
connect.commit();
//connect.close();
catch (SQLException throwables)
throwables.printStackTrace();
return count != 0;
//删除
public void Delete(String sql)
//1、链接数据库
Connection c = this.Connect();
Statement stmt = null;
try
c.setAutoCommit(false);
stmt = c.createStatement();
stmt.executeUpdate(sql);
c.commit();
stmt.close();
catch (SQLException throwable)
throwable.printStackTrace();
使用测试类:
public static void main(String[] args)
//构造方法
Util util=new Util("用户名","密码",
"ip地址","数据库名","端口号");
//插入语法
Boolean insert = util.Insert("insert into tmps (time,location,temperature)" +
" values('2022-1-1 16:00:00','场景八',112.3);"); //插入
//删除
util.Delete("delete from tmps t where t.location='场景七' ");
//查询
List<HashMap<String, Object>> select = util.Select("select * from tmps");
//关流
util.close();
System.out.println(select);
Navicat 入门使用方法
Navicat 多重连接数据库的管理工具,支持连接到(MySQL、Oracle、PostgreSQL、SQLite 、MariaDB )多类数据库,也支持多类数据库的管理和使用
1.Navicat 主工具栏:Navicat 为主工具栏包含(连接、用户、表、查询、备份、计划及更),作用于用户访问基本对象和功能
2.连接树:链接树是链接数据库对象及数据库的基本途径
3.选项卡栏:选项卡栏是让用户随意切换卡式窗口和对象列表
4.对象列表工具栏:对象列表工具栏作用于,提供了其它控制项,用户能够用它来操作对象
5.对象列表:对象列表就是能显示对象列表,例如表、视图、查询等
6.对象信息:对象信息能够显示Navicat 对象的详细信息和服务器对象信息
以上是关于Java链接PostgreSql数据库及基本使用的主要内容,如果未能解决你的问题,请参考以下文章