写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();
-
}
-
}
阅读(250713) | 评论(0) | 转发(0) |