杰瑞科技汇

Java中如何执行SQL insert语句?

Of course! Here is a comprehensive guide on how to perform SQL INSERT operations from Java, covering the best practices, different methods, and complete examples.

Java中如何执行SQL insert语句?-图1
(图片来源网络,侵删)

The Core Concept: JDBC

To interact with a database from Java, you use JDBC (Java Database Connectivity). It's an API that defines how a Java program can communicate with a database. The general steps are always the same:

  1. Load the JDBC Driver: (For older JDBC versions, this step is sometimes optional).
  2. Establish a Connection: Create a connection to your specific database using a connection string, username, and password.
  3. Create a Statement: Create an object that will carry your SQL command to the database.
  4. Execute the Query: Use the Statement object to run your SQL INSERT command.
  5. Process the Results: The executeUpdate() method for INSERT returns an integer indicating the number of rows affected.
  6. Close Resources: Crucially, close the Statement and Connection objects to free up database resources.

Method 1: The Classic Statement (Not Recommended for Dynamic Data)

This method is simple but vulnerable to SQL Injection and should not be used if your data comes from an external source (like a user).

Example: Inserting Static Data

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcInsertExample {
    // Database connection details
    private static final String DB_URL = "jdbc:mysql://localhost:3306/your_database";
    private static final String USER = "your_username";
    private static final String PASS = "your_password";
    public static void main(String[] args) {
        // Use try-with-resources to ensure the connection is closed automatically
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             Statement stmt = conn.createStatement()) {
            // The SQL INSERT statement
            // Note: We are using static values here, which is okay for this simple example.
            String sql = "INSERT INTO employees (id, name, position, salary) " +
                         "VALUES (101, 'Alice Smith', 'Software Engineer', 80000.00)";
            // Execute the update
            int rowsAffected = stmt.executeUpdate(sql);
            System.out.println(rowsAffected + " row(s) inserted successfully.");
        } catch (SQLException e) {
            System.err.println("Database error: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Why is this bad? If you tried to build the SQL string from user input, it would be dangerous. For example:

// DANGEROUS - DO NOT DO THIS!
String userInputName = "Robert'); DROP TABLE employees; --";
String sql = "INSERT INTO employees (name) VALUES ('" + userInputName + "')";

This could lead to catastrophic data loss. This is where PreparedStatement comes in.

Java中如何执行SQL insert语句?-图2
(图片来源网络,侵删)

Method 2: The Best Practice - PreparedStatement (Recommended)

A PreparedStatement pre-compiles the SQL statement and sends the data separately. This is the standard, safe, and often more efficient way to execute SQL with dynamic data.

Key Advantages:

  • Prevents SQL Injection: It treats user input as data, not as part of the SQL command.
  • More Efficient: The database can cache the execution plan for the prepared statement.
  • Easier to Read: Code is cleaner and more maintainable.

Example: Inserting Dynamic Data

Let's insert a new employee whose details come from variables.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JdbcPreparedInsertExample {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/your_database";
    private static final String USER = "your_username";
    private static final String PASS = "your_password";
    public static void main(String[] args) {
        // Data to be inserted (could come from user input, a form, etc.)
        int id = 102;
        String name = "Bob Johnson";
        String position = "Project Manager";
        double salary = 95000.50;
        // The SQL query with placeholders (?)
        String sql = "INSERT INTO employees (id, name, position, salary) VALUES (?, ?, ?, ?)";
        // Use try-with-resources for Connection and PreparedStatement
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            // Set the values for the placeholders
            // setInt(columnIndex, value)
            // setString(columnIndex, value)
            // setDouble(columnIndex, value)
            // Note: Column index starts at 1, not 0.
            pstmt.setInt(1, id);
            pstmt.setString(2, name);
            pstmt.setString(3, position);
            pstmt.setDouble(4, salary);
            // Execute the update
            int rowsAffected = pstmt.executeUpdate();
            System.out.println(rowsAffected + " row(s) inserted successfully.");
        } catch (SQLException e) {
            System.err.println("Database error: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Method 3: Getting the Auto-Generated Key (Very Common)

Most databases automatically generate a primary key (like an id) for a new row. You often need to get this generated key back to use it in your application (e.g., to insert related data into another table).

Java中如何执行SQL insert语句?-图3
(图片来源网络,侵删)

To do this, you use a second version of prepareStatement().

Example: Inserting and Retrieving the Generated ID

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcInsertAndGetKeyExample {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/your_database";
    private static final String USER = "your_username";
    private static final String PASS = "your_password";
    public static void main(String[] args) {
        String sql = "INSERT INTO employees (name, position, salary) VALUES (?, ?, ?)";
        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             // The second parameter tells the driver to return generated keys
             PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            pstmt.setString(1, "Charlie Brown");
            pstmt.setString(2, "UX Designer");
            pstmt.setDouble(3, 75000.00);
            int rowsAffected = pstmt.executeUpdate();
            if (rowsAffected > 0) {
                System.out.println(rowsAffected + " row(s) inserted.");
                // Get the generated keys
                try (var rs = pstmt.getGeneratedKeys()) {
                    if (rs.next()) {
                        // The first column of the result set contains the generated ID
                        long generatedId = rs.getLong(1);
                        System.out.println("Successfully inserted! Generated ID: " + generatedId);
                    }
                }
            }
        } catch (SQLException e) {
            System.err.println("Database error: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Complete Project Setup (Maven Example)

To run these examples, you need the appropriate JDBC driver for your database in your project's classpath.

Add the Dependency (pom.xml)

For a Maven project, add the dependency for your database. Here's an example for MySQL:

<dependencies>
    <!-- MySQL Connector/J -->
    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <version>8.0.33</version> <!-- Use the latest version -->
    </dependency>
</dependencies>

For PostgreSQL, it would be:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.6.0</version> <!-- Use the latest version -->
</dependency>

Create the Table

You'll need a table to insert into. Here's the SQL for the employees table used in the examples:

CREATE TABLE employees (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    position VARCHAR(100),
    salary DECIMAL(10, 2)
);

Summary: Best Practices

  1. Always Use PreparedStatement: It's the standard for safety and performance.
  2. Use Try-with-Resources: This ensures that your Connection, Statement, and ResultSet objects are always closed, even if an exception occurs. This prevents resource leaks.
  3. Handle Exceptions Gracefully: Catch SQLException and provide meaningful error messages or log them.
  4. Use Transactions for Multiple Operations: If you need to insert data into multiple tables and they must succeed or fail together, wrap them in a transaction.
    conn.setAutoCommit(false); // Start transaction
    try {
        // ... execute multiple INSERT statements ...
        conn.commit(); // Commit if all are successful
    } catch (SQLException e) {
        conn.rollback(); // Roll back if any statement fails
    }
分享:
扫描分享到社交APP
上一篇
下一篇