pg数据库增删改查操作
pom.xml添加依赖:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.4.2</version>
</dependency>
package demo.pg;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class PGtest {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Connection c = Connect();
// createTable(c);
// insertTable(c);
// selectTable(c);
selectADVTable(c);
c.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
}
public static Connection Connect() {
Connection c = null;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://xxxx:5555/qrdbutf", "postgres", "postgres");
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
return c; //
}
public static void createTable(Connection c) {
try {
String sqlDrop = "drop table if exists pg_test01";
String sql = "CREATE TABLE pg_test01 " + "(ID INT PRIMARY KEY NOT NULL,"
+ " NAME TEXT NOT NULL, " + " AGE INT NOT NULL, "
+ " ADDRESS CHAR(50), " + " SALARY REAL)";
Statement statement = c.createStatement();
statement.executeUpdate(sqlDrop);
statement.executeUpdate(sql);
statement.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("create table successfully");
}
public static void insertTable(Connection c) {
try {
String sql = "insert into pg_test01 values(0,'pg',18,'addr',888888888) ";
Statement statement = c.createStatement();
statement.executeUpdate(sql);
statement.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("insert table successfully");
}
public static void selectTable(Connection c) {
try {
Statement statement = c.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM pg_test01;");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String address = rs.getString("address");
float salary = rs.getFloat("salary");
System.out.println("ID = " + id);
System.out.println("NAME = " + name);
System.out.println("AGE = " + age);
System.out.println("ADDRESS = " + address);
System.out.println("SALARY = " + salary);
System.out.println();
}
rs.close();
statement.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("select table successfully");
}
public static void selectADVTable(Connection c) {
try {
Statement statement = c.createStatement();
String s = "SELECT * FROM tmp_pg;";
ResultSet rs = statement.executeQuery(s);
ResultSetMetaData m=rs.getMetaData();
int columns=m.getColumnCount();
for(int i=1;i<=columns;i++) {
System.out.print(m.getColumnName(i)+" ");
}
System.out.println();
while(rs.next()){
for(int i=1;i<=columns;i++){
if(rs.getString(i)==null) {
System.out.print("|"+rs.getString(i));
}else {
// System.out.print("|"+rs.getString(i).replace(" ",""));
System.out.print("|"+rs.getString(i));
}
}
System.out.println();
}
rs.close();
statement.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
System.out.println("select table successfully");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义