Skip to content

Instantly share code, notes, and snippets.

@kasramp
Last active July 8, 2023 21:17
Show Gist options
  • Save kasramp/07722add72d8d0013982 to your computer and use it in GitHub Desktop.
Save kasramp/07722add72d8d0013982 to your computer and use it in GitHub Desktop.
JDBC connection pool test connection
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SampleProgram {
public static void main(String[] args) throws SQLException {
DataSourceConfig ds = configureConnection();
String statement = "SELECT id FROM test_tbl";
try (Connection con = ds.getConnection()) {
try (PreparedStatement stmt = con.prepareStatement(statement)) {
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
System.out.println("Id is : " + id);
}
rs.close();
}
}
}
private static DataSourceConfig configureConnection() {
DataSourceConfig ds = new DataSourceConfig();
ds.setHostName("localhost");
ds.setPortNumber("2222");
ds.setUserName("test");
ds.setPassword("test");
ds.setDbName("testDB");
ds.setupPool();
return ds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment