java 原生jdbc 操作数据库

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class Main {
    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)) {
            String insertQuery = "INSERT INTO mytable (column1, column2) VALUES (?, ?)";
            PreparedStatement insertStatement = connection.prepareStatement(insertQuery);
            insertStatement.setString(1, "value1");
            insertStatement.setString(2, "value2");
            int rowsInserted = insertStatement.executeUpdate();
            System.out.println(rowsInserted + " rows inserted.");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // 删除数据
        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            String deleteQuery = "DELETE FROM mytable WHERE id = ?";
            PreparedStatement deleteStatement = connection.prepareStatement(deleteQuery);
            deleteStatement.setInt(1, 1);
            int rowsDeleted = deleteStatement.executeUpdate();
            System.out.println(rowsDeleted + " rows deleted.");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // 更新数据
        try (Connection connection = DriverManager.getConnection(url, username, password)) {
            String updateQuery = "UPDATE mytable SET column1 = ? WHERE id = ?";
            PreparedStatement updateStatement = connection.prepareStatement(updateQuery);
            updateStatement.setString(1, "new value");
            updateStatement.setInt(2, 2);
            int rowsUpdated = updateStatement.executeUpdate();
            System.out.println(rowsUpdated + " rows updated.");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
    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)) {
            String selectQuery = "SELECT * FROM mytable";
            PreparedStatement selectStatement = connection.prepareStatement(selectQuery);
            ResultSet resultSet = selectStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String column1 = resultSet.getString("column1");
                String column2 = resultSet.getString("column2");
                System.out.println("id: " + id + ", column1: " + column1 + ", column2: " + column2);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java)