写java代码处理数据的过程跟普通的postgresql数据库一样的。
以下抄自官方培训 如有侵权,将删除
-
package com.sequoiadb.postgresql;
-
-
-
import java.sql.Connection;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
-
-
public class Select {
-
private static String url = "jdbc:postgresql://127.0.0.1:5432/company";
-
private static String username = "sdbadmin";
-
private static String password = "";
-
-
-
public static void main(String[] args) throws SQLException {
-
select();
-
}
-
-
-
public static void select() throws SQLException {
-
PostgreSQLConnection pgConnection = new PostgreSQLConnection(url, username, password);
-
Connection connection = pgConnection.getConnection();
-
String sql = "select * from employee";
-
PreparedStatement psmt = connection.prepareStatement(sql);
-
ResultSet rs = psmt.executeQuery();
-
System.out.println("----------------------------------------------------------------------");
-
System.out.println("empno \t ename \t age");
-
System.out.println("----------------------------------------------------------------------");
-
while(rs.next()){
-
Integer empno = rs.getInt("empno");
-
String ename = rs.getString("ename");
-
String age = rs.getString("age");
-
-
-
System.out.println(empno + "\t" + ename + "\t" + age);
-
}
-
connection.close();
-
}
-
}
修改
-
package com.sequoiadb.postgresql;
-
-
import java.sql.Connection;
-
import java.sql.PreparedStatement;
-
import java.sql.SQLException;
-
-
public class Update {
-
private static String url = "jdbc:postgresql://127.0.0.1:5432/company";
-
private static String username = "sdbadmin";
-
private static String password = "";
-
-
public static void main(String[] args) throws SQLException {
-
update();
-
}
-
-
public static void update() throws SQLException {
-
PostgreSQLConnection pgConnection = new PostgreSQLConnection(url, username, passwor
-
d);
-
Connection connection = pgConnection.getConnection();
-
String sql = "update employee set age = ? where empno = ?";
-
PreparedStatement psmt = connection.prepareStatement(sql);
-
psmt.setInt(1, 41);
-
psmt.setInt(2, 10004);
-
psmt.execute();
-
-
connection.close();
-
}
-
}
阅读(250840) | 评论(0) | 转发(0) |