1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| import java.sql.*;
public class PreparedStatementExample {
private static final String URL = "jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC"; private static final String USER = "root"; private static final String PASSWORD = "root";
public static void main(String[] args) { insertUser("Alice", "alice@example.com", 25); User user = getUserById(1); System.out.println("查询结果: " + user); updateUserAge(1, 30);
deleteUser(2); }
public static void insertUser(String username, String email, int age) { String sql = "INSERT INTO user (username, email, age) VALUES (?, ?, ?)"; try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); PreparedStatement pstmt = conn.prepareStatement(sql)) { pstmt.setString(1, username); pstmt.setString(2, email); pstmt.setInt(3, age);
int rowsAffected = pstmt.executeUpdate(); System.out.println("插入成功,影响行数: " + rowsAffected);
} catch (SQLException e) { e.printStackTrace(); } }
public static User getUserById(int id) { String sql = "SELECT * FROM user WHERE id = ?"; User user = null;
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); PreparedStatement pstmt = conn.prepareStatement(sql)) { pstmt.setInt(1, id); try (ResultSet rs = pstmt.executeQuery()) { if (rs.next()) { user = new User( rs.getInt("id"), rs.getString("username"), rs.getString("email"), rs.getInt("age") ); } }
} catch (SQLException e) { e.printStackTrace(); } return user; }
public static void updateUserAge(int id, int newAge) { String sql = "UPDATE user SET age = ? WHERE id = ?";
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); PreparedStatement pstmt = conn.prepareStatement(sql)) { pstmt.setInt(1, newAge); pstmt.setInt(2, id);
int rowsAffected = pstmt.executeUpdate(); System.out.println("更新成功,影响行数: " + rowsAffected);
} catch (SQLException e) { e.printStackTrace(); } } public static void deleteUser(int id) { String sql = "DELETE FROM user WHERE id = ?";
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); PreparedStatement pstmt = conn.prepareStatement(sql)) { pstmt.setInt(1, id);
int rowsAffected = pstmt.executeUpdate(); System.out.println("删除成功,影响行数: " + rowsAffected);
} catch (SQLException e) { e.printStackTrace(); } } static class User { private int id; private String username; private String email; private int age;
public User(int id, String username, String email, int age) { this.id = id; this.username = username; this.email = email; this.age = age; }
@Override public String toString() { return String.format("User{id=%d, username='%s', email='%s', age=%d}", id, username, email, age); } } }
|