最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java連接PostgreSql數(shù)據(jù)庫及基本使用方式

 更新時間:2023年03月01日 11:01:37   作者:_碼農(nóng)耕地人  
這篇文章主要介紹了Java連接PostgreSql數(shù)據(jù)庫及基本使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

我是應(yīng)用Java封裝的思想將所有的方法封裝到了一個類里。

一)準備工作

1.下載鏈接需要的jar包

選擇最新版本即可。

2.下載之后添加到模塊里

3.創(chuàng)建一個工具類Util

書寫空參構(gòu)造,用于對數(shù)據(jù)庫的全部操作。

二)連接

所需內(nèi)容:數(shù)據(jù)庫名,端口號,數(shù)據(jù)庫地址,數(shù)據(jù)庫用戶名,密碼

public static Connection Connect(){
        Connection c = null;
        try {
 
            Class.forName("org.postgresql.Driver");
            c = DriverManager
                    .getConnection("jdbc:postgresql://服務(wù)器地址,本機寫127.0.0.1:服務(wù)器端口號,默認5432/鏈接的數(shù)據(jù)庫名",
                            "數(shù)據(jù)庫用戶名", "數(shù)據(jù)庫密碼");
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getClass().getName()+": "+e.getMessage());
            System.exit(0);
        }
        System.out.println("Opened database successfully");
        return c; //記得返回一下這個對象,后面一直在用
    }

三)查詢

普通版本查詢:數(shù)據(jù)庫有三個字段,時間time、地點location、溫度temperature

public static void select() {
        //與數(shù)據(jù)庫建立鏈接
        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);
            }
            //關(guān)流操作
            rs.close();
            stmt.close();
            c.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

下圖中這種方法屬于進階方法,只需要輸入sql語句即可將任意表中的數(shù)據(jù)都按照map集合的方式返回

public static List<HashMap<String,Object>> Select(String sql){
        //1、與數(shù)據(jù)庫建立鏈接
        Connection c = Util.Connect();
        //2、創(chuàng)建操作對象
        Statement stmt = null;
        //3、創(chuàng)建返回最終查詢的數(shù)據(jù)集合
        List<HashMap<String ,Object>> list=new ArrayList<>();
        try {
            //2.1、初始化操作對象
            stmt = c.createStatement();
            //4、執(zhí)行需要執(zhí)行的sql語句
            ResultSet rs = stmt.executeQuery(sql);
            //3.1開始封裝返回的對象
            ResultSetMetaData metaData = rs.getMetaData();//獲取全部列名
            int columnCount = metaData.getColumnCount();//列的數(shù)量
            //5、讀取數(shù)據(jù)
            while (rs.next()) {
                HashMap<String,Object> map=new HashMap<>();
                for (int i = 1; i <= columnCount; i++) {
                    //getColumnName獲取列名
                    String name = metaData.getColumnName(i);
                    //獲取對應(yīng)的元素
                    Object object = rs.getObject(i);
                    map.put(name,object);
                }
                list.add(map);
            }
            //6、關(guān)流操作
            rs.close();
            stmt.close();
            c.close();
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return list;
    }

四)添加

返回值是bool類型,表示是否添加成功。

***需要比查詢多添加一句***

connect.setAutoCommit(false);
    public static Boolean Insert(String sql){
        //1、與數(shù)據(jù)庫建立鏈接
        Connection connect = Util.Connect();
        //2、創(chuàng)建操作對象
        Statement stmt = null;
        int count = 0;
        try {
            //2.1、初始化創(chuàng)建對象
            stmt=connect.createStatement();
            //3、添加特殊語句。
            connect.setAutoCommit(false);//之前不用
            //4、執(zhí)行添加操作
            count = stmt.executeUpdate(sql);
            
            //5、關(guān)流
            stmt.close();
            connect.commit();
            connect.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count!=0;
    }

五)刪除數(shù)據(jù)

public void Delete(String sql) {
        //1、鏈接數(shù)據(jù)庫
        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;
 
    //構(gòu)造方法
    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;
    }
 
    //關(guān)流操作
    public void close() {
        Connection c = this.connect;
        try {
            c.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
 
    //查詢
    public List<HashMap<String, Object>> Select(String sql) {
        //1、與數(shù)據(jù)庫建立鏈接
        Connection c = this.connect;
        //2、創(chuàng)建操作對象
        Statement stmt = null;
        //3、創(chuàng)建返回最終查詢的數(shù)據(jù)集合
        List<HashMap<String, Object>> list = new ArrayList<>();
        try {
            //2.1、初始化操作對象
            stmt = c.createStatement();
            //4、執(zhí)行需要執(zhí)行的sql語句
            ResultSet rs = stmt.executeQuery(sql);
            //3.1開始封裝返回的對象
            ResultSetMetaData metaData = rs.getMetaData();//獲取全部列名
            int columnCount = metaData.getColumnCount();//列的數(shù)量
            //5、讀取數(shù)據(jù)
            while (rs.next()) {
                HashMap<String, Object> map = new HashMap<>();
                for (int i = 1; i <= columnCount; i++) {
                    //getColumnName獲取列名
                    String name = metaData.getColumnName(i);
                    //獲取對應(yīng)的元素
                    Object object = rs.getObject(i);
                    map.put(name, object);
                }
                list.add(map);
            }
            //6、關(guān)流操作
            rs.close();
            stmt.close();
            //c.close();
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return list;
    }
 
    //插入操作
    public Boolean Insert(String sql) {
        //1、與數(shù)據(jù)庫建立鏈接
        Connection connect = this.connect;
        //2、創(chuàng)建操作對象
        Statement stmt = null;
        int count = 0;
        try {
            //2.1、初始化創(chuàng)建對象
            stmt = connect.createStatement();
            //3、添加特殊語句。
            connect.setAutoCommit(false);//之前不用
            //4、執(zhí)行添加操作
            count = stmt.executeUpdate(sql);
 
            //5、關(guān)流
            stmt.close();
            connect.commit();
            //connect.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count != 0;
    }
 
    //刪除
    public void Delete(String sql) {
        //1、鏈接數(shù)據(jù)庫
        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) {
        //構(gòu)造方法
        Util util=new Util("用戶名","密碼",
                "ip地址","數(shù)據(jù)庫名","端口號");
        //插入語法
        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");  
        //關(guān)流
        util.close();
        System.out.println(select);
    }

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA編寫JavaWeb出現(xiàn)亂碼問題解決方案

    IDEA編寫JavaWeb出現(xiàn)亂碼問題解決方案

    這篇文章主要介紹了IDEA編寫JavaWeb出現(xiàn)亂碼問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot深入刨析數(shù)據(jù)層技術(shù)

    SpringBoot深入刨析數(shù)據(jù)層技術(shù)

    這篇文章主要介紹了SpringBoot數(shù)據(jù)層技術(shù)的解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • Java實現(xiàn)幾十萬條數(shù)據(jù)插入實例教程(30萬條數(shù)據(jù)插入MySQL僅需13秒)

    Java實現(xiàn)幾十萬條數(shù)據(jù)插入實例教程(30萬條數(shù)據(jù)插入MySQL僅需13秒)

    這篇文章主要給大家介紹了關(guān)于Java如何實現(xiàn)幾十萬條數(shù)據(jù)插入的相關(guān)資料,30萬條數(shù)據(jù)插入MySQL僅需13秒,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2023-04-04
  • Java性能工具JMeter實現(xiàn)上傳與下載腳本編寫

    Java性能工具JMeter實現(xiàn)上傳與下載腳本編寫

    性能測試工作中,文件上傳也是經(jīng)常見的性能壓測場景之一,那么 JMeter 文件上傳下載腳本怎么做,本文詳細的來介紹一下,感興趣的可以了解一下
    2021-07-07
  • Java算法之堆排序代碼示例

    Java算法之堆排序代碼示例

    這篇文章主要介紹了Java算法之堆排序代碼示例,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Netty分布式固定長度解碼器實現(xiàn)原理剖析

    Netty分布式固定長度解碼器實現(xiàn)原理剖析

    這篇文章主要為大家介紹了Netty分布式固定長度解碼器原理剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03
  • java集合Collection常用方法解讀

    java集合Collection常用方法解讀

    這篇文章主要介紹了java集合Collection常用方法解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • mybatis 遍歷foreach中or拼接的操作

    mybatis 遍歷foreach中or拼接的操作

    這篇文章主要介紹了mybatis 遍歷foreach中or拼接的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Springboot中加入druid連接池

    Springboot中加入druid連接池

    這篇文章主要介紹了Springboot中加入druid連接池,Druid是目前最好的數(shù)據(jù)庫連接池。在功能、性能、擴展性方面,都超過其他數(shù)據(jù)庫連接池,同時加入了日志監(jiān)控,下面來看看文章的具體內(nèi)容吧
    2022-01-01
  • Java泛型映射不同的值類型詳解及實例代碼

    Java泛型映射不同的值類型詳解及實例代碼

    這篇文章主要介紹了Java泛型映射不同的值類型詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02

最新評論

嘉义市| 西充县| 南华县| 景东| 天全县| 安康市| 渑池县| 鞍山市| 巧家县| 宿松县| 民乐县| 商河县| 海晏县| 海城市| 龙海市| 那曲县| 南宫市| 惠安县| 马关县| 湖南省| 白玉县| 银川市| 苏尼特右旗| 大渡口区| 河间市| 乐陵市| 高要市| 安陆市| 双桥区| 鄢陵县| 益阳市| 马关县| 台前县| 成安县| 枣阳市| 台山市| 郁南县| 九龙坡区| 上犹县| 南通市| 抚松县|