import java.sql.*; public class JdbcExample { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/mydatabase"; String username = "root"; String password = "password"; try (Connection connection = DriverManager.getConnection(url, username, password)) { Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM customers"); while (resultSet.next()) { System.out.println("ID: " + resultSet.getInt("id")); System.out.println("Name: " + resultSet.getString("name")); System.out.println("Email: " + resultSet.getString("email")); System.out.println("Address: " + resultSet.getString("address")); System.out.println("----------------------------"); } int rowsUpdated = statement.executeUpdate("UPDATE customers SET email='newemail@example.com' WHERE id=1"); System.out.println(rowsUpdated + " rows updated."); } catch (SQLException e) { e.printStackTrace(); } } }


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