使用MySQL数据库,建立一个模拟商店的数据库,数据库名为shop 在数据库中创建商品表goods,表结构如下: number(文本),name(文本),madeTime(日期),price(数字,双精度) 其中number为主键,表示商品编号。 往数据表随机写入30-50条数据。 编写程序,先查询出goods表中的数据,然后随机获取其中的10条记录,并计算出这10条记录price字段值的平均值,即平均价格,在控制台打印出10条记录和平均价格
时间: 2024-04-09 09:29:14 浏览: 58
以下是使用Java编写的示例代码来创建数据库和表,插入随机数据,并执行查询和计算平均价格的操作:
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class ShopDatabase {
private static final String URL = "jdbc:mysql://localhost:3306/shop";
private static final String USERNAME = "your_username";
private static final String PASSWORD = "your_password";
public static void main(String[] args) {
try {
// 连接数据库
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// 创建商品表
createGoodsTable(connection);
// 插入随机数据
insertRandomData(connection);
// 查询商品表中的数据
List<Goods> goodsList = queryGoodsTable(connection);
// 随机获取10条记录并计算平均价格
List<Goods> randomGoodsList = getRandomGoods(goodsList, 10);
double averagePrice = calculateAveragePrice(randomGoodsList);
// 打印10条记录和平均价格
System.out.println("随机获取的10条记录:");
for (Goods goods : randomGoodsList) {
System.out.println(goods);
}
System.out.println("平均价格:" + averagePrice);
// 关闭数据库连接
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void createGoodsTable(Connection connection) throws SQLException {
String createTableQuery = "CREATE TABLE IF NOT EXISTS goods (" +
"number VARCHAR(255) PRIMARY KEY," +
"name VARCHAR(255)," +
"madeTime DATE," +
"price DOUBLE)";
Statement statement = connection.createStatement();
statement.execute(createTableQuery);
statement.close();
}
private static void insertRandomData(Connection connection) throws SQLException {
String insertQuery = "INSERT INTO goods (number, name, madeTime, price) VALUES (?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(insertQuery);
Random random = new Random();
int numOfData = random.nextInt(21) + 30; // 生成30-50之间的随机数
for (int i = 1; i <= numOfData; i++) {
statement.setString(1, "G" + i);
statement.setString(2, "商品" + i);
statement.setDate(3, new Date(System.currentTimeMillis()));
statement.setDouble(4, random.nextDouble() * 100);
statement.executeUpdate();
}
statement.close();
}
private static List<Goods> queryGoodsTable(Connection connection) throws SQLException {
String query = "SELECT * FROM goods";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
List<Goods> goodsList = new ArrayList<>();
while (resultSet.next()) {
String number = resultSet.getString("number");
String name = resultSet.getString("name");
Date madeTime = resultSet.getDate("madeTime");
double price = resultSet.getDouble("price");
Goods goods = new Goods(number, name, madeTime, price);
goodsList.add(goods);
}
resultSet.close();
statement.close();
return goodsList;
}
private static List<Goods> getRandomGoods(List<Goods> goodsList, int num) {
List<Goods> randomGoodsList = new ArrayList<>();
Random random = new Random();
int size = goodsList.size();
if (size <= num) {
return goodsList;
}
for (int i = 0; i < num; i++) {
int randomIndex = random.nextInt(size);
randomGoodsList.add(goodsList.get(randomIndex));
}
return randomGoodsList;
}
private static double calculateAveragePrice(List<Goods> goodsList) {
double sum = 0;
for (Goods goods : goodsList) {
sum += goods.getPrice();
}
return sum / goodsList.size();
}
}
class Goods {
private String number;
private String name;
private Date madeTime;
private double price;
public Goods(String number, String name, Date madeTime, double price) {
this.number = number;
this.name = name;
this.madeTime = madeTime;
this.price = price;
}
public String getNumber() {
return number;
}
public String getName() {
return name;
}
public Date getMadeTime() {
return madeTime;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return "商品编号:" + number + ", 商品名称:" + name + ", 生产日期:" + madeTime + ", 价格:" + price;
}
}
```
请将`your_username`和`your_password`替换为您的MySQL用户名和密码。该代码首先创建一个名为`shop`的数据库,然后在该数据库中创建名为`goods`的商品表,并插入30-50条随机数据。接下来,它查询商品表中的数据,并随机选择10条记录。最后,它计算这10条记录的平均价格,并将10条记录和平均价格打印到控制台上。
请确保您已经正确安装了MySQL数据库,并将Java代码中的数据库连接参数替换为您自己的数据库连接参数。
阅读全文