In Java, database connections enable data storage, management, and access. After the connection is established, data can be transferred through insert, update, and delete operations, and data information can be obtained by executing queries. Specific steps include: 1. Establishing a database connection; 2. Inserting, updating or deleting data; 3. Executing queries; 4. Traversing the result set. In addition, the article provides practical cases to demonstrate how to store and obtain user information.
Java Database Connection: Easy Data Transfer and Query
Introduction
In Java applications, database connections are critical for storing, managing, and accessing data. This article walks you through the steps to establish a database connection, transfer data, and perform basic queries.
Establish database connection
// 导入必要库 import java.sql.*; // 定义数据库凭证 String url = "jdbc:mysql://localhost:3306/mydb"; String username = "root"; String password = "password"; // 建立连接 Connection conn = DriverManager.getConnection(url, username, password);
Data transmission
Insert data
// 创建 PreparedStatement 以防注入攻击 String query = "INSERT INTO users (name, email) VALUES (?, ?)"; PreparedStatement ps = conn.prepareStatement(query); // 设置参数 ps.setString(1, "John Doe"); ps.setString(2, "johndoe@example.com"); // 执行插入操作 ps.executeUpdate();
Update data
// 创建 PreparedStatement String query = "UPDATE users SET name = ? WHERE id = ?"; PreparedStatement ps = conn.prepareStatement(query); // 设置参数 ps.setString(1, "Jane Doe"); ps.setInt(2, 1); // 执行更新操作 ps.executeUpdate();
Delete data
// 创建 PreparedStatement String query = "DELETE FROM users WHERE id = ?"; PreparedStatement ps = conn.prepareStatement(query); // 设置参数 ps.setInt(1, 1); // 执行删除操作 ps.executeUpdate();
Data query
Execute query
// 创建 Statement 对象 Statement stmt = conn.createStatement(); // 执行查询 ResultSet rs = stmt.executeQuery("SELECT * FROM users");
Traversing the result set
// 遍历结果集 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String email = rs.getString("email"); System.out.println("ID: " + id + ", Name: " + name + ", Email: " + email); }
Practical case
Storing user information
// 获取用户输入 String name = scanner.nextLine(); String email = scanner.nextLine(); // 插入数据 String query = "INSERT INTO users (name, email) VALUES (?, ?)"; PreparedStatement ps = conn.prepareStatement(query); ps.setString(1, name); ps.setString(2, email); ps.executeUpdate(); // 通知用户 System.out.println("User added successfully!");
Get all user information
// 执行查询 Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM users"); // 遍历结果集 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String email = rs.getString("email"); System.out.println("ID: " + id + ", Name: " + name + ", Email: " + email); }
The above is the detailed content of How does Java database connection perform data transmission and query?. For more information, please follow other related articles on the PHP Chinese website!