java定义的新闻的一个类,构建一个数据库,然后在java里增删查改。
时间: 2024-02-21 11:58:52 浏览: 165
好的,我可以帮你解答这个问题。
首先,我们需要定义一个新闻类,可以包含新闻的标题、内容、发布时间等属性。示例代码如下:
```java
public class News {
private int id; // 新闻编号
private String title; // 标题
private String content; // 内容
private Date publishTime; // 发布时间
// 构造函数
public News(int id, String title, String content, Date publishTime) {
this.id = id;
this.title = title;
this.content = content;
this.publishTime = publishTime;
}
// Getter 和 Setter 方法
// ...
}
```
接着,我们需要构建一个数据库来存储新闻信息。可以使用关系型数据库如MySQL或SQLite,也可以使用非关系型数据库如MongoDB。
以MySQL为例,我们可以创建一个名为news的数据库,其中包含一个名为news_table的数据表,用于存储新闻信息。数据表结构如下:
```sql
CREATE TABLE news_table (
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(50),
content TEXT,
publish_time DATETIME
);
```
然后,我们可以使用Java的JDBC API来连接数据库并执行增删查改操作。示例代码如下:
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class NewsDatabase {
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost/news";
// 数据库用户名和密码
private static final String USER = "root";
private static final String PASS = "root";
// 获取数据库连接
private static Connection getConnection() {
Connection conn = null;
try {
// 注册 JDBC 驱动器
Class.forName(JDBC_DRIVER);
// 打开连接
conn = DriverManager.getConnection(DB_URL, USER, PASS);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
// 新增一条新闻
public static void insertNews(News news) {
try (Connection conn = getConnection()) {
String sql = "INSERT INTO news_table(title, content, publish_time) VALUES (?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, news.getTitle());
pstmt.setString(2, news.getContent());
pstmt.setTimestamp(3, new Timestamp(news.getPublishTime().getTime()));
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 根据新闻编号删除一条新闻
public static void deleteNews(int id) {
try (Connection conn = getConnection()) {
String sql = "DELETE FROM news_table WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 根据新闻编号更新一条新闻
public static void updateNews(News news) {
try (Connection conn = getConnection()) {
String sql = "UPDATE news_table SET title = ?, content = ?, publish_time = ? WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, news.getTitle());
pstmt.setString(2, news.getContent());
pstmt.setTimestamp(3, new Timestamp(news.getPublishTime().getTime()));
pstmt.setInt(4, news.getId());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 根据新闻编号查询一条新闻
public static News getNewsById(int id) {
News news = null;
try (Connection conn = getConnection()) {
String sql = "SELECT * FROM news_table WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
String title = rs.getString("title");
String content = rs.getString("content");
Date publishTime = new Date(rs.getTimestamp("publish_time").getTime());
news = new News(id, title, content, publishTime);
}
} catch (SQLException e) {
e.printStackTrace();
}
return news;
}
// 查询所有新闻
public static List<News> getAllNews() {
List<News> newsList = new ArrayList<>();
try (Connection conn = getConnection()) {
String sql = "SELECT * FROM news_table";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String content = rs.getString("content");
Date publishTime = new Date(rs.getTimestamp("publish_time").getTime());
News news = new News(id, title, content, publishTime);
newsList.add(news);
}
} catch (SQLException e) {
e.printStackTrace();
}
return newsList;
}
}
```
以上代码中,getConnection()方法用于创建数据库连接,insertNews()方法用于新增一条新闻,deleteNews()方法用于根据新闻编号删除一条新闻,updateNews()方法用于根据新闻编号更新一条新闻,getNewsById()方法用于根据新闻编号查询一条新闻,getAllNews()方法用于查询所有新闻。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)