剑道第一仙

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

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");
}
}

 

posted on   剑道第一仙  阅读(282)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示