Last active
July 8, 2023 21:17
-
-
Save kasramp/07722add72d8d0013982 to your computer and use it in GitHub Desktop.
JDBC connection pool test connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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