供应商基础信息管理,基于java和mysql数据库,idea实现,详细代码及步骤
时间: 2024-03-10 17:46:46 浏览: 44
基于java+servlet+mysql员工信息管理系统
好的,我可以为您提供一个简单的供应商基础信息管理系统的Java代码实现,使用MySQL数据库,基于IDEA开发工具。以下是实现步骤:
1. 创建MySQL数据库
首先,我们需要在MySQL数据库中创建一个新的数据库。您可以使用以下命令:
```
CREATE DATABASE supplier;
```
2. 创建供应商信息表
在新创建的数据库中,我们需要创建一个供应商信息表。您可以使用以下命令:
```
CREATE TABLE supplier_info (
supplier_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
supplier_name VARCHAR(50) NOT NULL,
supplier_address VARCHAR(100) NOT NULL,
supplier_phone VARCHAR(20) NOT NULL
);
```
3. 配置IDEA开发环境
在IDEA中,我们需要添加MySQL连接器和JDBC驱动程序。您可以按照以下步骤操作:
- 在“File”菜单中选择“Project Structure”。
- 在“Modules”选项卡下,选择“Dependencies”标签。
- 单击“+”按钮,选择“JARs or directories”。
- 选择MySQL连接器和JDBC驱动程序的JAR文件。
4. 创建Java项目
在IDEA中,创建一个新的Java项目,并添加以下类:
Supplier.java
```java
public class Supplier {
private int id;
private String name;
private String address;
private String phone;
public Supplier(int id, String name, String address, String phone) {
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
```
SupplierDAO.java
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class SupplierDAO {
private Connection connection;
public SupplierDAO(Connection connection) {
this.connection = connection;
}
public void addSupplier(Supplier supplier) throws SQLException {
PreparedStatement statement = connection.prepareStatement("INSERT INTO supplier_info (supplier_name, supplier_address, supplier_phone) VALUES (?, ?, ?)");
statement.setString(1, supplier.getName());
statement.setString(2, supplier.getAddress());
statement.setString(3, supplier.getPhone());
statement.executeUpdate();
}
public void deleteSupplier(int id) throws SQLException {
PreparedStatement statement = connection.prepareStatement("DELETE FROM supplier_info WHERE supplier_id = ?");
statement.setInt(1, id);
statement.executeUpdate();
}
public void updateSupplier(Supplier supplier) throws SQLException {
PreparedStatement statement = connection.prepareStatement("UPDATE supplier_info SET supplier_name = ?, supplier_address = ?, supplier_phone = ? WHERE supplier_id = ?");
statement.setString(1, supplier.getName());
statement.setString(2, supplier.getAddress());
statement.setString(3, supplier.getPhone());
statement.setInt(4, supplier.getId());
statement.executeUpdate();
}
public Supplier getSupplier(int id) throws SQLException {
PreparedStatement statement = connection.prepareStatement("SELECT * FROM supplier_info WHERE supplier_id = ?");
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return new Supplier(resultSet.getInt("supplier_id"), resultSet.getString("supplier_name"), resultSet.getString("supplier_address"), resultSet.getString("supplier_phone"));
} else {
return null;
}
}
public List<Supplier> getAllSuppliers() throws SQLException {
List<Supplier> suppliers = new ArrayList<>();
PreparedStatement statement = connection.prepareStatement("SELECT * FROM supplier_info");
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
suppliers.add(new Supplier(resultSet.getInt("supplier_id"), resultSet.getString("supplier_name"), resultSet.getString("supplier_address"), resultSet.getString("supplier_phone")));
}
return suppliers;
}
}
```
Main.java
```java
import java.sql.*;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/supplier", "root", "password");
SupplierDAO supplierDAO = new SupplierDAO(connection);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Add supplier");
System.out.println("2. Delete supplier");
System.out.println("3. Update supplier");
System.out.println("4. Get supplier");
System.out.println("5. Get all suppliers");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter supplier name: ");
String name = scanner.next();
System.out.print("Enter supplier address: ");
String address = scanner.next();
System.out.print("Enter supplier phone: ");
String phone = scanner.next();
Supplier supplier = new Supplier(0, name, address, phone);
supplierDAO.addSupplier(supplier);
System.out.println("Supplier added successfully!");
break;
case 2:
System.out.print("Enter supplier id: ");
int id = scanner.nextInt();
supplierDAO.deleteSupplier(id);
System.out.println("Supplier deleted successfully!");
break;
case 3:
System.out.print("Enter supplier id: ");
int updateId = scanner.nextInt();
Supplier updateSupplier = supplierDAO.getSupplier(updateId);
if (updateSupplier == null) {
System.out.println("Supplier not found!");
} else {
System.out.print("Enter supplier name: ");
String updateName = scanner.next();
System.out.print("Enter supplier address: ");
String updateAddress = scanner.next();
System.out.print("Enter supplier phone: ");
String updatePhone = scanner.next();
updateSupplier.setName(updateName);
updateSupplier.setAddress(updateAddress);
updateSupplier.setPhone(updatePhone);
supplierDAO.updateSupplier(updateSupplier);
System.out.println("Supplier updated successfully!");
}
break;
case 4:
System.out.print("Enter supplier id: ");
int supplierId = scanner.nextInt();
Supplier getSupplier = supplierDAO.getSupplier(supplierId);
if (getSupplier == null) {
System.out.println("Supplier not found!");
} else {
System.out.println("Supplier id: " + getSupplier.getId());
System.out.println("Supplier name: " + getSupplier.getName());
System.out.println("Supplier address: " + getSupplier.getAddress());
System.out.println("Supplier phone: " + getSupplier.getPhone());
}
break;
case 5:
List<Supplier> allSuppliers = supplierDAO.getAllSuppliers();
for (Supplier s : allSuppliers) {
System.out.println("Supplier id: " + s.getId());
System.out.println("Supplier name: " + s.getName());
System.out.println("Supplier address: " + s.getAddress());
System.out.println("Supplier phone: " + s.getPhone());
System.out.println("--------------------");
}
break;
case 6:
System.exit(0);
default:
System.out.println("Invalid choice!");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
5. 运行程序
运行Main类,您将看到一个简单的命令行界面,可用于添加、删除、更新、获取供应商信息,以及获取所有供应商信息。
希望这个简单的供应商基础信息管理系统能够帮助到您!
阅读全文