jdbc连接MySQL增删改查
时间: 2023-12-19 21:32:33 浏览: 28
以下是jdbc连接MySQL进行增删改查的示例:
1. 连接MySQL数据库
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySQLConnection {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/zlr?serverTimezone=GMT&useSSL=false";
String username = "your_username";
String password = "your_password";
try {
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("Connected to MySQL database!");
} catch (SQLException e) {
System.out.println("Connection failed! Error: " + e.getMessage());
}
}
}
```
2. 插入数据
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertData {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/zlr?serverTimezone=GMT&useSSL=false";
String username = "your_username";
String password = "your_password";
try {
Connection connection = DriverManager.getConnection(url, username, password);
String insertQuery = "INSERT INTO your_table (column1, column2, column3) VALUES (?, ?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setString(1, "value1");
preparedStatement.setString(2, "value2");
preparedStatement.setString(3, "value3");
int rowsAffected = preparedStatement.executeUpdate();
System.out.println(rowsAffected + " row(s) inserted!");
} catch (SQLException e) {
System.out.println("Insert failed! Error: " + e.getMessage());
}
}
}
```
3. 更新数据
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class UpdateData {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/zlr?serverTimezone=GMT&useSSL=false";
String username = "your_username";
String password = "your_password";
try {
Connection connection = DriverManager.getConnection(url, username, password);
String updateQuery = "UPDATE your_table SET column1 = ? WHERE condition_column = ?";
PreparedStatement preparedStatement = connection.prepareStatement(updateQuery);
preparedStatement.setString(1, "new_value");
preparedStatement.setString(2, "condition_value");
int rowsAffected = preparedStatement.executeUpdate();
System.out.println(rowsAffected + " row(s) updated!");
} catch (SQLException e) {
System.out.println("Update failed! Error: " + e.getMessage());
}
}
}
```
4. 删除数据
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DeleteData {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/zlr?serverTimezone=GMT&useSSL=false";
String username = "your_username";
String password = "your_password";
try {
Connection connection = DriverManager.getConnection(url, username, password);
String deleteQuery = "DELETE FROM your_table WHERE condition_column = ?";
PreparedStatement preparedStatement = connection.prepareStatement(deleteQuery);
preparedStatement.setString(1, "condition_value");
int rowsAffected = preparedStatement.executeUpdate();
System.out.println(rowsAffected + " row(s) deleted!");
} catch (SQLException e) {
System.out.println("Delete failed! Error: " + e.getMessage());
}
}
}
```