Home > Java > javaTutorial > How does Java database connection perform data transmission and query?

How does Java database connection perform data transmission and query?

PHPz
Release: 2024-04-16 15:09:01
Original
1183 people have browsed it

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.

How does Java database connection perform data transmission and query?

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);
Copy after login

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();
Copy after login

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();
Copy after login

Delete data

// 创建 PreparedStatement 
String query = "DELETE FROM users WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(query);

// 设置参数
ps.setInt(1, 1);

// 执行删除操作
ps.executeUpdate();
Copy after login

Data query

Execute query

// 创建 Statement 对象
Statement stmt = conn.createStatement();

// 执行查询
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
Copy after login

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);
}
Copy after login

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!");
Copy after login

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);
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template