<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>0.9.0</version> </dependency> import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; String url = "jdbc:pgsql://hostname:port/database_name"; String user = "username"; String password = "password"; try { Connection connection = DriverManager.getConnection(url, user, password); // ... } catch (SQLException e) { e.printStackTrace(); } import java.sql.ResultSet; import java.sql.Statement; try { Statement statement = connection.createStatement(); String query = "SELECT * FROM users"; ResultSet resultSet = statement.executeQuery(query); while (resultSet.next()) { // ... } } catch (SQLException e) { e.printStackTrace(); } try { Statement statement = connection.createStatement(); String insertQuery = "INSERT INTO users (name, email) VALUES ('John', 'john@example.com')"; int rowsAffected = statement.executeUpdate(insertQuery); System.out.println("Rows affected: " + rowsAffected); } catch (SQLException e) { e.printStackTrace(); } try { connection.close(); } catch (SQLException e) { e.printStackTrace(); }


上一篇:
下一篇:
切换中文