<dependency> <groupId>org.ethereum</groupId> <artifactId>ethereumj-jdbc-driver</artifactId> <version>1.5.0</version> </dependency> String jdbcUrl = "jdbc:ethereum://localhost:8545"; String username = "your_ethereum_node_username"; String password = "your_ethereum_node_password"; try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) { } catch (SQLException e) { e.printStackTrace(); } String query = "SELECT balance FROM accounts WHERE address = ?"; String address = "0x123456789abcdef"; try (PreparedStatement statement = connection.prepareStatement(query)) { statement.setString(1, address); try (ResultSet resultSet = statement.executeQuery()) { if (resultSet.next()) { BigInteger balance = resultSet.getBigDecimal("balance").toBigInteger(); System.out.println("Account balance: " + balance); } } } catch (SQLException e) { e.printStackTrace(); } String query = "INSERT INTO transactions (from_address, to_address, value) VALUES (?, ?, ?)"; String fromAddress = "0xabcdef123456789"; String toAddress = "0xfedcba9876543210"; BigInteger value = BigInteger.valueOf(1000000000000000000L); // 1 Ether try (PreparedStatement statement = connection.prepareStatement(query)) { statement.setString(1, fromAddress); statement.setString(2, toAddress); statement.setBigDecimal(3, new BigDecimal(value)); int rowsAffected = statement.executeUpdate(); if (rowsAffected > 0) { System.out.println("Transaction sent successfully!"); } else { System.out.println("Failed to send transaction."); } } catch (SQLException e) { e.printStackTrace(); }


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