用Java实现查询、增加、删除、修改
时间: 2023-10-01 13:09:28 浏览: 55
这是一个比较广泛的问题,需要具体针对不同的场景和数据结构进行实现。下面是一些常见的例子:
1. 查询
使用Java语言实现查询操作的方法包括:使用JDBC连接数据库、使用ORM框架(如Hibernate、MyBatis等)操作数据库、使用JPA(Java Persistence API)查询数据库等。以下是一个使用JDBC查询MySQL数据库的例子:
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class QueryExample {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM student");
List<Student> students = new ArrayList<>();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
students.add(new Student(id, name, age, gender));
}
System.out.println(students);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
class Student {
private int id;
private String name;
private int age;
private String gender;
public Student(int id, String name, int age, String gender) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
}
```
2. 增加
使用Java语言实现增加数据的方法包括:使用JDBC连接数据库、使用ORM框架(如Hibernate、MyBatis等)操作数据库、使用JPA(Java Persistence API)操作数据库等。以下是一个使用JDBC向MySQL数据库中插入数据的例子:
```java
import java.sql.*;
public class InsertExample {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
stmt = conn.prepareStatement("INSERT INTO student (name, age, gender) VALUES (?, ?, ?)");
stmt.setString(1, "Tom");
stmt.setInt(2, 18);
stmt.setString(3, "male");
int rows = stmt.executeUpdate();
System.out.println(rows + " row(s) affected");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
3. 删除
使用Java语言实现删除数据的方法包括:使用JDBC连接数据库、使用ORM框架(如Hibernate、MyBatis等)操作数据库、使用JPA(Java Persistence API)操作数据库等。以下是一个使用JDBC从MySQL数据库中删除数据的例子:
```java
import java.sql.*;
public class DeleteExample {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
stmt = conn.prepareStatement("DELETE FROM student WHERE id = ?");
stmt.setInt(1, 1);
int rows = stmt.executeUpdate();
System.out.println(rows + " row(s) affected");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
4. 修改
使用Java语言实现修改数据的方法包括:使用JDBC连接数据库、使用ORM框架(如Hibernate、MyBatis等)操作数据库、使用JPA(Java Persistence API)操作数据库等。以下是一个使用JDBC更新MySQL数据库中的数据的例子:
```java
import java.sql.*;
public class UpdateExample {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
stmt = conn.prepareStatement("UPDATE student SET age = ? WHERE id = ?");
stmt.setInt(1, 20);
stmt.setInt(2, 2);
int rows = stmt.executeUpdate();
System.out.println(rows + " row(s) affected");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```