通过Java语言生成MySQL数据库的增删改查操作,主要分为以下五个类:DBUtil类、Mess类、MessDAO类、ShowAllData类以及Main类。的设计思路
时间: 2023-08-11 11:07:36 浏览: 92
Java语言实现对MySql数据库中数据的增删改查操作的代码
1. DBUtil类:用于数据库连接和关闭。
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
private static final String USER = "root";
private static final String PASSWORD = "123456";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
2. Mess类:用于定义实体类,对应数据库中的表。
```java
public class Mess {
private int id;
private String name;
private String message;
public Mess() {}
public Mess(String name, String message) {
this.name = name;
this.message = message;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
```
3. MessDAO类:用于对数据库中的表进行增删改查操作。
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class MessDAO {
private Connection conn;
public MessDAO(Connection conn) {
this.conn = conn;
}
public boolean addMess(Mess mess) {
String sql = "insert into mess(name, message) values(?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, mess.getName());
pstmt.setString(2, mess.getMessage());
pstmt.execute();
pstmt.close();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean deleteMess(int id) {
String sql = "delete from mess where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean updateMess(Mess mess) {
String sql = "update mess set name = ?, message = ? where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, mess.getName());
pstmt.setString(2, mess.getMessage());
pstmt.setInt(3, mess.getId());
pstmt.executeUpdate();
pstmt.close();
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public List<Mess> getAllMess() {
List<Mess> list = new ArrayList<>();
String sql = "select * from mess";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Mess mess = new Mess();
mess.setId(rs.getInt("id"));
mess.setName(rs.getString("name"));
mess.setMessage(rs.getString("message"));
list.add(mess);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
```
4. ShowAllData类:用于显示数据库中mess表中的所有数据。
```java
import java.sql.Connection;
import java.util.List;
public class ShowAllData {
public static void main(String[] args) {
Connection conn = DBUtil.getConnection();
MessDAO messDAO = new MessDAO(conn);
List<Mess> list = messDAO.getAllMess();
for (Mess mess : list) {
System.out.println("ID:" + mess.getId() + ",姓名:" + mess.getName() + ",留言:" + mess.getMessage());
}
DBUtil.closeConnection(conn);
}
}
```
5. Main类:用于测试增删改查操作。
```java
import java.sql.Connection;
public class Main {
public static void main(String[] args) {
Connection conn = DBUtil.getConnection();
MessDAO messDAO = new MessDAO(conn);
// 添加
Mess mess1 = new Mess("张三", "你好!");
messDAO.addMess(mess1);
Mess mess2 = new Mess("李四", "你好啊!");
messDAO.addMess(mess2);
// 修改
Mess mess3 = new Mess();
mess3.setId(1);
mess3.setName("张三");
mess3.setMessage("你好吗?");
messDAO.updateMess(mess3);
// 删除
messDAO.deleteMess(2);
DBUtil.closeConnection(conn);
}
}
```
阅读全文