我需要运用eclipse和mysql 做一个网页系统,系统具有手机信息的记录,删除和查询。记录销售信息,统计库存的功能,请帮我编出代码
时间: 2024-03-14 15:48:18 浏览: 56
手机销售信息管理系统(管理员有添加、删除查询用户信息,对销售员、省份、手机信息的增、删、改、查的功能;查询用户有按指定的不同方式查询销售员信息、手机销售信息、对各种手机进行分类汇总(按销售量降序排列)和查询省份信息)
好的,我可以为您提供一个简单的示例代码,帮助您了解如何使用 Eclipse 和 MySQL 开发一个具有手机信息记录、删除和查询,销售信息记录和库存统计功能的网页系统。以下是示例代码:
**1. 创建一个数据库连接**
```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/mobile_store";
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;
}
}
```
**2. 创建一个 JavaBean 类,用于封装手机信息**
```java
public class Mobile {
private int id;
private String name;
private String brand;
private double price;
private int stock;
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 getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}
```
**3. 创建一个 DAO 类,用于访问数据库**
```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 MobileDAO {
private Connection conn;
public MobileDAO() {
conn = DBUtil.getConnection();
}
public void addMobile(Mobile mobile) {
try {
String sql = "insert into mobile (name, brand, price, stock) values (?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, mobile.getName());
ps.setString(2, mobile.getBrand());
ps.setDouble(3, mobile.getPrice());
ps.setInt(4, mobile.getStock());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteMobile(int id) {
try {
String sql = "delete from mobile where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateMobile(Mobile mobile) {
try {
String sql = "update mobile set name=?, brand=?, price=?, stock=? where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, mobile.getName());
ps.setString(2, mobile.getBrand());
ps.setDouble(3, mobile.getPrice());
ps.setInt(4, mobile.getStock());
ps.setInt(5, mobile.getId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Mobile getMobileById(int id) {
Mobile mobile = null;
try {
String sql = "select * from mobile where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
mobile = new Mobile();
mobile.setId(rs.getInt("id"));
mobile.setName(rs.getString("name"));
mobile.setBrand(rs.getString("brand"));
mobile.setPrice(rs.getDouble("price"));
mobile.setStock(rs.getInt("stock"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return mobile;
}
public List<Mobile> getAllMobiles() {
List<Mobile> mobiles = new ArrayList<Mobile>();
try {
String sql = "select * from mobile";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Mobile mobile = new Mobile();
mobile.setId(rs.getInt("id"));
mobile.setName(rs.getString("name"));
mobile.setBrand(rs.getString("brand"));
mobile.setPrice(rs.getDouble("price"));
mobile.setStock(rs.getInt("stock"));
mobiles.add(mobile);
}
} catch (SQLException e) {
e.printStackTrace();
}
return mobiles;
}
public int getMobileCount() {
int count = 0;
try {
String sql = "select count(*) from mobile";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
count = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
public int getMobileStockSum() {
int sum = 0;
try {
String sql = "select sum(stock) from mobile";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
sum = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return sum;
}
}
```
**4. 编写业务逻辑层代码**
```java
public class MobileService {
private MobileDAO mobileDAO;
public MobileService() {
mobileDAO = new MobileDAO();
}
public void addMobile(Mobile mobile) {
mobileDAO.addMobile(mobile);
}
public void deleteMobile(int id) {
mobileDAO.deleteMobile(id);
}
public void updateMobile(Mobile mobile) {
mobileDAO.updateMobile(mobile);
}
public Mobile getMobileById(int id) {
return mobileDAO.getMobileById(id);
}
public List<Mobile> getAllMobiles() {
return mobileDAO.getAllMobiles();
}
public int getMobileCount() {
return mobileDAO.getMobileCount();
}
public int getMobileStockSum() {
return mobileDAO.getMobileStockSum();
}
}
```
**5. 编写网页代码**
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mobile Store</title>
</head>
<body>
<h1>Mobile Store</h1>
<h2>Add Mobile</h2>
<form method="post" action="addMobile">
<label>Name:</label><input type="text" name="name" required><br>
<label>Brand:</label><input type="text" name="brand" required><br>
<label>Price:</label><input type="number" name="price" step="0.01" required><br>
<label>Stock:</label><input type="number" name="stock" required><br>
<input type="submit" value="Add">
</form>
<hr>
<h2>Mobile List</h2>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Brand</th>
<th>Price</th>
<th>Stock</th>
<th>Action</th>
</tr>
<c:forEach var="mobile" items="${mobileList}">
<tr>
<td>${mobile.id}</td>
<td>${mobile.name}</td>
<td>${mobile.brand}</td>
<td>${mobile.price}</td>
<td>${mobile.stock}</td>
<td>
<a href="editMobile?id=${mobile.id}">Edit</a>
<a href="deleteMobile?id=${mobile.id}">Delete</a>
</td>
</tr>
</c:forEach>
</table>
<hr>
<h2>Mobile Count</h2>
<p>${mobileCount}</p>
<hr>
<h2>Mobile Stock Sum</h2>
<p>${mobileStockSum}</p>
</body>
</html>
```
**6. 配置 web.xml 文件**
```xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>AddMobileServlet</servlet-name>
<servlet-class>com.example.AddMobileServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>DeleteMobileServlet</servlet-name>
<servlet-class>com.example.DeleteMobileServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>EditMobileServlet</servlet-name>
<servlet-class>com.example.EditMobileServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>MobileListServlet</servlet-name>
<servlet-class>com.example.MobileListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddMobileServlet</servlet-name>
<url-pattern>/addMobile</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DeleteMobileServlet</servlet-name>
<url-pattern>/deleteMobile</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>EditMobileServlet</servlet-name>
<url-pattern>/editMobile</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MobileListServlet</servlet-name>
<url-pattern>/mobileList</url-pattern>
</servlet-mapping>
</web-app>
```
以上是一个简单的示例代码,您可以根据需要进行修改和完善。需要注意的是,本示例代码仅供参考,实际项目中需要考虑更多的问题,比如数据校验、安全性、性能等。如果您有任何问题或者需要进一步的帮助,可以随时联系我。
阅读全文