亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java使用JDBC連接postgresql數(shù)據(jù)庫示例

 更新時間:2019年01月23日 11:30:04   作者:韓大貓  
這篇文章主要介紹了Java使用JDBC連接postgresql數(shù)據(jù)庫,結(jié)合實(shí)例形式分析了jdbc連接postgresql數(shù)據(jù)庫及數(shù)值插入、更新、查詢等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Java使用JDBC連接postgresql數(shù)據(jù)庫。分享給大家供大家參考,具體如下:

package tool;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PsqlConnectionTool {
 private String url = "jdbc:postgresql://xxx.xxx.xxx.xxx:5432/testdb";
 private String username = "postgres";
 private String password = "postgres";
 private Connection connection = null;
 public Connection getConn() {
  try {
   Class.forName("org.postgresql.Driver").newInstance();
   connection = DriverManager.getConnection(url, username, password);
  } catch (InstantiationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return connection;
 }
 public ResultSet query(Connection conn, String sql) {
  PreparedStatement pStatement = null;
  ResultSet rs = null;
  try {
   pStatement = conn.prepareStatement(sql);
   rs = pStatement.executeQuery();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return rs;
 }
 public boolean queryUpdate(Connection conn, String sql) {
  PreparedStatement pStatement = null;
  int rs = 0;
  try {
   pStatement = conn.prepareStatement(sql);
   rs = pStatement.executeUpdate();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  if (rs > 0) {
   return true;
  }
  return false;
 }
 public static void main(String[] args) throws SQLException {
  PsqlConnectionTool pgtool = new PsqlConnectionTool();
  Connection myconn = pgtool.getConn();
  pgtool.queryUpdate(myconn, "insert into test values (1,'smoon','man')");
  ResultSet rs = pgtool.query(myconn, "select * from test");
  while(rs.next()){
   int id = rs.getInt("id");
   String name = rs.getString("name");
   String gender = rs.getString("gender");
   System.out.println("id:"+id+" 姓名:"+name+" 性別:"+gender);
   myconn.close();
  }
 }
}

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java使用JDBC操作數(shù)據(jù)庫技巧總結(jié)》、《Java+MySQL數(shù)據(jù)庫程序設(shè)計總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java文件與目錄操作技巧匯總》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總

希望本文所述對大家java程序設(shè)計有所幫助。

相關(guān)文章

  • 淺析Java模板方法的一種使用方式

    淺析Java模板方法的一種使用方式

    模板方法說白了就是將一段代碼模板化,將通用的代碼段抽取出來,并提供一些自定義的接口去定制的特定位置的某些業(yè)務(wù)功能。本文主要來和大家聊聊它的一種使用方式,希望對大家有所幫助
    2023-02-02
  • springboot整合Excel填充數(shù)據(jù)代碼示例

    springboot整合Excel填充數(shù)據(jù)代碼示例

    這篇文章主要給大家介紹了關(guān)于springboot整合Excel填充數(shù)據(jù)的相關(guān)資料,文中通過代碼示例介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用springboot具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • java多線程編程之Synchronized關(guān)鍵字詳解

    java多線程編程之Synchronized關(guān)鍵字詳解

    這篇文章主要為大家詳細(xì)介紹了java多線程編程之Synchronized關(guān)鍵字,感興趣的朋友可以參考一下
    2016-05-05
  • SpringCloud-Hystrix組件使用方法

    SpringCloud-Hystrix組件使用方法

    這篇文章主要介紹了SpringCloud-Hystrix組件使用方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Spring中的AOP動態(tài)代理源碼詳解

    Spring中的AOP動態(tài)代理源碼詳解

    這篇文章主要介紹了Spring中的AOP動態(tài)代理源碼詳解,AOP即面向切面編程也稱面向方面編程,它是面向?qū)ο缶幊蘋OP的一種補(bǔ)充,目前已成為一種比較成熟的編程方式,本文就其源碼進(jìn)行解析,需要的朋友可以參考下
    2023-09-09
  • springboot?yml配置文件值注入方式

    springboot?yml配置文件值注入方式

    這篇文章主要介紹了springboot?yml配置文件值注入方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java多線程下解決資源競爭的7種方法詳解

    Java多線程下解決資源競爭的7種方法詳解

    這篇文章主要介紹了Java多線程下解決資源競爭的7種方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • 詳解如何利用jasypt實(shí)現(xiàn)配置文件加密

    詳解如何利用jasypt實(shí)現(xiàn)配置文件加密

    Jasypt?(Java?Simplified?Encryption)?是一個?java?庫,它允許開發(fā)人員以最小的成本將基本的加密功能添加到項目中,而無需深入了解密碼學(xué)的工作原理。本文將利用jasypt實(shí)現(xiàn)配置文件加密,感興趣的可以學(xué)習(xí)一下
    2022-07-07
  • Java實(shí)現(xiàn)快速并查集

    Java實(shí)現(xiàn)快速并查集

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)快速并查集,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • 新手了解java 數(shù)組基礎(chǔ)知識

    新手了解java 數(shù)組基礎(chǔ)知識

    這篇文章主要介紹了Java 數(shù)組分析及簡單實(shí)例的相關(guān)資料,在Java中它就是對象,一個比較特殊的對象,需要的朋友可以參考下,希望可以對你有所幫助
    2021-07-07

最新評論