java增删改查
时间: 2023-07-07 19:05:36 浏览: 43
Java中的增删改查通常是指对数据库中的数据进行操作,以下是一个简单的示例:
1. 建立数据库连接:
```java
import java.sql.*;
public class DatabaseConnection {
private static final String URL = "jdbc:mysql://localhost:3306/database_name?useSSL=false&serverTimezone=UTC";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
```
2. 插入数据:
```java
import java.sql.*;
public class InsertData {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DatabaseConnection.getConnection();
String sql = "INSERT INTO table_name (col1, col2) VALUES (?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "value1");
pstmt.setString(2, "value2");
int result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("Insert data successfully.");
} else {
System.out.println("Insert data failed.");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
3. 查询数据:
```java
import java.sql.*;
public class QueryData {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DatabaseConnection.getConnection();
String sql = "SELECT * FROM table_name WHERE col1=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "value1");
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(rs.getString("col1") + "\t" + rs.getString("col2"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
4. 更新数据:
```java
import java.sql.*;
public class UpdateData {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DatabaseConnection.getConnection();
String sql = "UPDATE table_name SET col2=? WHERE col1=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "new_value");
pstmt.setString(2, "value1");
int result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("Update data successfully.");
} else {
System.out.println("Update data failed.");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
5. 删除数据:
```java
import java.sql.*;
public class DeleteData {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DatabaseConnection.getConnection();
String sql = "DELETE FROM table_name WHERE col1=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "value1");
int result = pstmt.executeUpdate();
if (result > 0) {
System.out.println("Delete data successfully.");
} else {
System.out.println("Delete data failed.");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
注意:以上仅仅是一个简单的示例,实际应用中可能需要根据具体情况进行更改和优化。同时,为了防止 SQL 注入攻击,建议使用 PreparedStatement 来构建 SQL 语句。