Using Java to Operate PostgreSQL
Using Java to operate PostgreSQL, you can connect to the PostgreSQL database through Java's JDBC and execute corresponding SQL statements to achieve data insertion, modification, query, and deletion.
The following are the steps to operate PostgreSQL using Java:
1. Add Maven dependencies (add the following dependencies in the pom.xml file):
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
Version number
</dependency>
</dependencies>
Please replace the 'version number' with the latest PostgreSQL driver version.
2. Import necessary Java classes:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
3. Connect to the PostgreSQL database:
String URL="jdbc: p ostgresql://localhost:5432/ Database name ";
String username="username";
String password="Password";
Connection connection = null;
try {
connection = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
Please replace 'localhost: 5432' with the actual database host and port number, and replace 'database name', 'username', and 'password' with the actual database information.
4. Perform data insertion operation:
String SQL="Insert INTO table name (column 1, column 2, column 3) VALUES (?,?,?)";
try {
PreparedStatement statement = connection.prepareStatement(sql);
Statement. setString (1, value 1);
Statement. setString (2, value 2);
Statement. setString (3, value 3);
statement.executeUpdate();
System. out. println ("Insert successful");
} catch (SQLException e) {
e.printStackTrace();
}
Please replace the 'table name' with the actual table name, and replace the 'column 1', 'column 2', 'column 3' and corresponding 'value 1', 'value 2', and 'value 3' with the actual column and numerical value.
5. Perform data modification operations:
String SQL="UPDATE table name SET column 1=?, column 2=? WHERE condition";
try {
PreparedStatement statement = connection.prepareStatement(sql);
Statement. setString (1, new value 1);
Statement. setString (2, new value 2);
//Set condition parameters
int rowsAffected = statement.executeUpdate();
System. out. println ("Successfully modified, affecting number of rows:"+rowsAffected);
} catch (SQLException e) {
e.printStackTrace();
}
Please replace 'table name', 'column 1', 'column 2', 'condition' and corresponding 'new value 1', 'new value 2' with the actual table name, column name, condition, and numerical value.
6. Perform data query operations:
String SQL="SELECT * From Table Name WHERE Condition";
try {
PreparedStatement statement = connection.prepareStatement(sql);
//Set condition parameters
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
//Process query results
String column1=resultSet. getString ("Column 1");
String column2=resultSet. getString ("Column 2");
// ...
}
} catch (SQLException e) {
e.printStackTrace();
}
Please replace the 'table name', 'condition', and corresponding 'column 1' and 'column 2' with the actual table name, condition, and column name.
7. Perform data deletion operation:
String SQL="DELETE From Table Name WHERE Condition";
try {
PreparedStatement statement = connection.prepareStatement(sql);
//Set condition parameters
int rowsAffected = statement.executeUpdate();
System. out. println ("Successfully deleted, affected rows:"+rowsAffected);
} catch (SQLException e) {
e.printStackTrace();
}
Please replace 'table name' and 'condition' with the actual table name and condition.
8. Close database connection:
try {
if (connection != null) {
connection.close();
System. out. println ("Database connection closed");
}
} catch (SQLException e) {
e.printStackTrace();
}
Complete Java code example:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class PostgreSQLExample {
public static void main(String[] args) {
String URL="jdbc: p ostgresql://localhost:5432/ Database name ";
String username="username";
String password="Password";
Connection connection = null;
try {
connection = DriverManager.getConnection(url, username, password);
System. out. println ("Successfully connected to database");
//Data insertion operation
String insertSQL="Insert INTO table name (column 1, column 2, column 3) VALUES (?,?,?)";
try {
PreparedStatement insertStatement = connection.prepareStatement(insertSQL);
InsertStatement. setString (1, value 1);
InsertStatement. setString (2, value 2);
InsertStatement. setString (3, value 3);
insertStatement.executeUpdate();
System. out. println ("Insert successful");
} catch (SQLException e) {
e.printStackTrace();
}
//Data modification operation
String updateSQL="UPDATE table name SET column 1=?, column 2=? WHERE condition";
try {
PreparedStatement updateStatement = connection.prepareStatement(updateSQL);
UpdateStatement. setString (1, new value 1);
UpdateStatement. setString (2, new value 2);
//Set condition parameters
int rowsAffected = updateStatement.executeUpdate();
System. out. println ("Successfully modified, affecting number of rows:"+rowsAffected);
} catch (SQLException e) {
e.printStackTrace();
}
//Data query operation
String selectSQL="SELECT * From Table Name WHERE Condition";
try {
PreparedStatement selectStatement = connection.prepareStatement(selectSQL);
//Set condition parameters
ResultSet resultSet = selectStatement.executeQuery();
while (resultSet.next()) {
//Process query results
String column1=resultSet. getString ("Column 1");
String column2=resultSet. getString ("Column 2");
// ...
}
} catch (SQLException e) {
e.printStackTrace();
}
//Data deletion operation
String deleteSQL="DELETE From Table Name WHERE Condition";
try {
PreparedStatement deleteStatement = connection.prepareStatement(deleteSQL);
//Set condition parameters
int rowsAffected = deleteStatement.executeUpdate();
System. out. println ("Successfully deleted, affected rows:"+rowsAffected);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (connection != null) {
connection.close();
System. out. println ("Database connection closed");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
Please replace the 'database name', 'username', 'password', 'table name', 'column 1', 'column 2', 'column 3', 'condition', 'value 1', 'value 2', 'value 3', 'new value 1', 'new value 2' with the relevant information of the actual database and table.