用Java和sql server做一个商品管理系统的程序
时间: 2024-06-08 20:05:18 浏览: 90
好的,下面是一个简单的商品管理系统的Java程序示例,使用SQL Server作为数据库:
1. 创建数据库和表
首先,在SQL Server中创建一个名为"ProductManagement"的数据库,并创建一个名为"products"的表,包含以下字段:
- id: 商品ID,自增长整数类型
- name: 商品名称,字符串类型
- price: 商品价格,浮点数类型
- quantity: 商品数量,整数类型
2. 连接数据库
在Java程序中,使用JDBC连接SQL Server数据库。需要下载SQL Server JDBC驱动程序,将其添加到Java项目的类路径中。然后,使用以下代码连接数据库:
```java
import java.sql.*;
public class ProductManagementSystem {
public static void main(String[] args) {
String url = "jdbc:sqlserver://localhost:1433;databaseName=ProductManagement";
String user = "your_username";
String password = "your_password";
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
System.out.println("Connected to database successfully!");
} catch (SQLException e) {
System.out.println("Failed to connect to database.");
e.printStackTrace();
}
}
}
```
请将`your_username`和`your_password`替换为您的SQL Server用户名和密码。
3. 添加商品
以下代码演示如何向数据库添加新商品:
```java
public static void addProduct(Connection conn, String name, double price, int quantity) throws SQLException {
String sql = "INSERT INTO products (name, price, quantity) VALUES (?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, name);
statement.setDouble(2, price);
statement.setInt(3, quantity);
int rowsInserted = statement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("Product added successfully!");
}
}
```
4. 更新商品
以下代码演示如何更新数据库中的商品信息:
```java
public static void updateProduct(Connection conn, int id, String name, double price, int quantity) throws SQLException {
String sql = "UPDATE products SET name = ?, price = ?, quantity = ? WHERE id = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, name);
statement.setDouble(2, price);
statement.setInt(3, quantity);
statement.setInt(4, id);
int rowsUpdated = statement.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("Product updated successfully!");
}
}
```
5. 删除商品
以下代码演示如何从数据库中删除商品:
```java
public static void deleteProduct(Connection conn, int id) throws SQLException {
String sql = "DELETE FROM products WHERE id = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setInt(1, id);
int rowsDeleted = statement.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("Product deleted successfully!");
}
}
```
6. 查询商品
以下代码演示如何从数据库中查询商品:
```java
public static void searchProduct(Connection conn, String name) throws SQLException {
String sql = "SELECT * FROM products WHERE name LIKE ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, "%" + name + "%");
ResultSet result = statement.executeQuery();
while (result.next()) {
int id = result.getInt("id");
String productName = result.getString("name");
double price = result.getDouble("price");
int quantity = result.getInt("quantity");
System.out.println("ID: " + id + ", Name: " + productName + ", Price: " + price + ", Quantity: " + quantity);
}
}
```
以上就是简单的商品管理系统Java程序示例,希望对您有帮助。
阅读全文