Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3546449
  • 博文数量: 715
  • 博客积分: 1860
  • 博客等级: 上尉
  • 技术积分: 7745
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-07 08:51
个人简介

偶尔有空上来看看

文章分类

全部博文(715)

文章存档

2023年(75)

2022年(134)

2021年(238)

2020年(115)

2019年(11)

2018年(9)

2017年(9)

2016年(17)

2015年(7)

2014年(4)

2013年(1)

2012年(11)

2011年(27)

2010年(35)

2009年(11)

2008年(11)

最近访客

分类: Java

2020-04-28 14:17:19


写java代码处理数据的过程跟普通的postgresql数据库一样的。

以下抄自官方培训  如有侵权,将删除

点击(此处)折叠或打开

  1. package com.sequoiadb.postgresql;


  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;


  6. public class Select {
  7.     private static String url = "jdbc:postgresql://127.0.0.1:5432/company";
  8.     private static String username = "sdbadmin";
  9.     private static String password = "";


  10.     public static void main(String[] args) throws SQLException {
  11.         select();
  12.     }


  13.     public static void select() throws SQLException {
  14.         PostgreSQLConnection pgConnection = new PostgreSQLConnection(url, username, password);
  15.         Connection connection = pgConnection.getConnection();
  16.         String sql = "select * from employee";
  17.         PreparedStatement psmt = connection.prepareStatement(sql);
  18.         ResultSet rs = psmt.executeQuery();
  19.         System.out.println("----------------------------------------------------------------------");
  20.         System.out.println("empno \t ename \t age");
  21.         System.out.println("----------------------------------------------------------------------");
  22.         while(rs.next()){
  23.             Integer empno = rs.getInt("empno");
  24.             String ename = rs.getString("ename");
  25.             String age = rs.getString("age");


  26.             System.out.println(empno + "\t" + ename + "\t" + age);
  27.         }
  28.         connection.close();
  29.     }
  30. }

修改

点击(此处)折叠或打开

  1. package com.sequoiadb.postgresql;

  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;

  5. public class Update {
  6.     private static String url = "jdbc:postgresql://127.0.0.1:5432/company";
  7.     private static String username = "sdbadmin";
  8.     private static String password = "";

  9.     public static void main(String[] args) throws SQLException {
  10.         update();
  11.     }

  12.     public static void update() throws SQLException {
  13.         PostgreSQLConnection pgConnection = new PostgreSQLConnection(url, username, passwor
  14. d);
  15.         Connection connection = pgConnection.getConnection();
  16.         String sql = "update employee set age = ? where empno = ?";
  17.         PreparedStatement psmt = connection.prepareStatement(sql);
  18.         psmt.setInt(1, 41);
  19.         psmt.setInt(2, 10004);
  20.         psmt.execute();

  21.         connection.close();
  22.     }
  23. }

阅读(250776) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~