postgresql java demo_java读写postgresql数据库(转)

Installation

Before we start using PostgreSQL in our Java programs we need to make sure that we have PostgreSQL JDBC and Java set up on the machine. You can check Java tutorial for Java installation on your machine. Now let us check how to set up PostgreSQL JDBC driver.

Download latest version of postgresql-(VERSION).jdbc.jar from postgresql-jdbc repository.

Add downloaded jar file postgresql-(VERSION).jdbc.jar in your class path, or you can use it along with -classpath option as explained below in examples.

Following section assumes you have little knowledge about Java JDBC concepts. If you don't, then it is suggested to spent half and hour with JDBC Tutorial to become comfortable with concepts explained below.

Connecting To Database

Following Java code shows how to connect to an existing database. If database does not exist, then it will be created and finally a database object will be returned.

importjava.sql.Connection;importjava.sql.DriverManager;public classPostgreSQLJDBC {public static voidmain(String args[]) {

Connection c= null;try{

Class.forName("org.postgresql.Driver");

c=DriverManager

.getConnection("jdbc:postgresql://localhost:5432/testdb","postgres", "123");

}catch(Exception e) {

e.printStackTrace();

System.err.println(e.getClass().getName()+": "+e.getMessage());

System.exit(0);

}

System.out.println("Opened database successfully");

}

}

Before you compile and run above program, find pg_hba.conf file in your PostgreSQL installation directory and add the following line:

# IPv4 local connections:

host all all127.0.0.1/32 md5

You can start/restart postgres server in case it is not running using the following command:

[root@host]# service postgresql restart

Stopping postgresql service: [ OK ]

Starting postgresql service: [ OK ]

Now, let's compile and run above program to get connection with testdb. Here, we are using postgres as user ID and 123 as password to access the database. You can change this as per your database configuration and setup. We are also assuming current version of JDBC driver postgresql-9.2-1002.jdbc3.jar is available in the current path

C:\JavaPostgresIntegration>javac PostgreSQLJDBC.java

C:\JavaPostgresIntegration>java -cp c:\tools\postgresql-9.2-1002.jdbc3.jar;C:\JavaPostgresIntegration PostgreSQLJDBC

Open database successfully

Create a Table</

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用JDBC驱动程序来连接和操作PostgreSQL数据库。要存取PostgreSQL二进制类型bytea,可以使用PreparedStatement对象的setBytes()方法来设置二进制数据,并使用ResultSet对象的getBytes()方法来获取二进制数据。 以下是一个示例代码,演示了如何存取bytea类型的数据: ```java import java.sql.*; public class PostgresBinaryExample { public static void main(String[] args) throws SQLException { //连接PostgreSQL数据库 String url = "jdbc:postgresql://localhost/test"; String user = "postgres"; String password = "password"; Connection conn = DriverManager.getConnection(url, user, password); //创建表 Statement stmt = conn.createStatement(); String createTable = "CREATE TABLE binary_data (id SERIAL PRIMARY KEY, data BYTEA)"; stmt.executeUpdate(createTable); //插入二进制数据 byte[] binaryData = {0x01, 0x02, 0x03}; PreparedStatement pstmt = conn.prepareStatement("INSERT INTO binary_data (data) VALUES (?)"); pstmt.setBytes(1, binaryData); pstmt.executeUpdate(); //查询二进制数据 ResultSet rs = stmt.executeQuery("SELECT data FROM binary_data"); if (rs.next()) { byte[] retrievedData = rs.getBytes("data"); System.out.println("Retrieved data: " + Arrays.toString(retrievedData)); } //清理资源 rs.close(); pstmt.close(); stmt.executeUpdate("DROP TABLE binary_data"); stmt.close(); conn.close(); } } ``` 在上面的代码中,我们首先连接PostgreSQL数据库,然后创建一个名为binary_data的表,其中包含一个名为data的bytea列。接着,我们插入一个包含三个字节的二进制数据。最后,我们查询该数据并输出到控制台,最后清理资源。 注意,上述代码只是一个示例,实际应用中需要更多的错误处理和异常处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值