import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; public class JdbcConnectionPool { private static HikariDataSource dataSource; static { HikariConfig config = new HikariConfig(); config.setJdbcUrl("jdbc:cdap://localhost:11015/my_namespace"); config.setUsername("admin"); config.setPassword("admin"); dataSource = new HikariDataSource(config); } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } } import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JdbcQueryExample { public void queryData() throws SQLException { Connection connection = JdbcConnectionPool.getConnection(); String query = "SELECT * FROM my_table WHERE col1 = ?"; PreparedStatement statement = connection.prepareStatement(query); statement.setString(1, "some_value"); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { } resultSet.close(); statement.close(); connection.close(); } } import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JdbcFetchSizeExample { public void fetchData() throws SQLException { Connection connection = JdbcConnectionPool.getConnection(); String query = "SELECT * FROM my_table"; PreparedStatement statement = connection.prepareStatement(query); statement.setFetchSize(100); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { } resultSet.close(); statement.close(); connection.close(); } }


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