Use PostgreSQL JDBC4 driver to query and update

Use PostgreSQL JDBC4 driver to query and update PostgreSQL is a powerful and mature open source relationship database management system. The JDBC (Java database connection) is a standard Java API used to communicate with the database.By combining the PostgreSQL JDBC4 driver, we can perform data query and update operations in Java applications.This article will introduce how to connect to the database with PostgreSQL JDBC4 driver and perform common data operations. 1. Introduce the driver library First, you need to download and introduce the PostgreSQL JDBC4 driver library.In the construction path of the Java project, add the .jar file to the dependencies. 2. Connect to the database In the Java code, you can use the `DriverManager.getConnection` method to establish a connection with the Postgresql database.The URL, user name and password of the database are needed. import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class PostgreSQLJDBCExample { public static void main(String[] args) { // Database connection details String url = "jdbc:postgresql://localhost:5432/mydatabase"; String username = "postgres"; String password = "password"; try { // Establish connection Connection connection = DriverManager.getConnection(url, username, password); System.out.println("Connected to the PostgreSQL database!"); // Perform data operations here // Close connection connection.close(); System.out.println("Connection closed."); } catch (SQLException e) { System.out.println("Connection failed."); e.printStackTrace(); } } } Make sure you replace the `url`,` username` and `Password` to adapt to the actual database connection settings. 3. Execute the query operation You can use the `Statement` or` PreparedStatement` to execute the SQL query statement.`Statement` is used to perform static SQL query, and` PreparedStatement` is used to execute pre -compilation SQL query with parameters. import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class PostgreSQLJDBCExample { public static void main(String[] args) { // Database connection details... try { Connection connection = DriverManager.getConnection(url, username, password); // Create statement Statement statement = connection.createStatement(); // Execute query ResultSet resultSet = statement.executeQuery("SELECT * FROM mytable"); // Process results while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); System.out.println("ID: " + id + ", Name: " + name); } // Close statement and result set resultSet.close(); statement.close(); // Close connection connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } The above code executes a simple query and prints the results to the console. 4. Execute the update operation In Java, you can use the `ExecuteupDate` method to perform data update operations, such as inserting, updating or deleting. import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class PostgreSQLJDBCExample { public static void main(String[] args) { // Database connection details... try { Connection connection = DriverManager.getConnection(url, username, password); // Create statement Statement statement = connection.createStatement(); // Execute update int rowsUpdated = statement.executeUpdate("UPDATE mytable SET name = 'John' WHERE id = 1"); // Check the number of rows updated System.out.println(rowsUpdated + " rows updated."); // Close statement statement.close(); // Close connection connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } The above code performs a simple update operation, updating the name of the ID in the `mytable` table as 1 to" John ". By using the PostgreSQL JDBC4 driver, we can easily connect to the Postgresql database in the Java application and perform data query and update operations.The above example code can be used as the basis for entry, and you can adjust and expand according to your needs.