<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
import java.sql.*;
public class Main {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost:5432/mydatabase";
String username = "myuser";
String password = "mypassword";
try {
Connection connection = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.sql.*;
public class Main {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost:5432/mydatabase";
String username = "myuser";
String password = "mypassword";
try {
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM mytable");
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("ID: " + id + ", Name: " + name);
}
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.*;
public class Main {
public static void main(String[] args) {
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:postgresql://localhost:5432/mydatabase");
config.setUsername("myuser");
config.setPassword("mypassword");
HikariDataSource dataSource = new HikariDataSource(config);
try {
Connection connection = dataSource.getConnection();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
dataSource.close();
}
}