<dependencies> ... <dependency> <groupId>org.adbcj</groupId> <artifactId>adbcj</artifactId> <version>0.8.11</version> </dependency> ... </dependencies> import org.adbcj.Connection; import org.adbcj.ConnectionManager; import org.adbcj.ConnectionManagerProvider; import org.adbcj.DbException; public class DatabaseConnectionManager { private Connection connection; public void connect() { ConnectionManager connectionManager = ConnectionManagerProvider.createConnectionManager( "adbcj:mysql://localhost:3306/mydatabase?user=myuser&password=mypassword" ); connectionManager.connect().handle((conn, throwable) -> { if (throwable != null) { System.err.println("Failed to connect to the database: " + throwable.getMessage()); } else { System.out.println("Successfully connected to the database"); connection = conn; } return null; }); } public Connection getConnection() { return connection; } } import org.adbcj.Record; public class DatabaseQueryExecutor { private DatabaseConnectionManager connectionManager; public DatabaseQueryExecutor(DatabaseConnectionManager connectionManager) { this.connectionManager = connectionManager; } public void executeQuery(String sql) { connectionManager.getConnection().executeQuery(sql).thenAccept(result -> { for (Record record : result) { System.out.println("Fetched record: " + record); } }).exceptionally(throwable -> { System.err.println("Failed to execute query: " + throwable.getMessage()); return null; }); } }


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