首页 > 代码库 > 作品(通过窗体实现数据库的增删改)

作品(通过窗体实现数据库的增删改)

定义对象:

package jdbc;

public class ShangPin {

private int id;
private String name;
private int buyInPrice;
private int unitPrice;
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 int getBuyInPrice() {
return buyInPrice;
}
public void setBuyInPrice(int buyInPrice) {
this.buyInPrice = buyInPrice;
}
public int getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(int unitPrice) {
this.unitPrice = unitPrice;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}

 

包装方法:

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;


public class ShangPinManager {

private final static String DRIVER = "com.mysql.jdbc.Driver";
private final static String URL = "jdbc:mysql://127.0.0.1:3306/e-market";
private final static String USER = "root";
private final static String PWD = "1992";

public int AddShangPin(ShangPin sp) throws ClassNotFoundException, SQLException{
int n = -1;
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USER, PWD);
java.sql.Statement st = conn.createStatement();
String sql = "insert into commodityinfo(CID,CName,InputPrice,OutputPrice,Amount)"
+"values(null,‘"+sp.getName()+"‘,‘"+sp.getBuyInPrice()+"‘,‘"
+sp.getUnitPrice()+"‘,‘"+sp.getStock()+"‘)";
n = st.executeUpdate(sql);
if(n>=1){
JOptionPane.showMessageDialog(null, "添加商品成功");
}else{
JOptionPane.showMessageDialog(null, "添加商品失败");
}
return n;
}

public int DeleteShangPin(ShangPin sp) throws ClassNotFoundException, SQLException{
int n = -1;
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USER, PWD);
java.sql.Statement st = conn.createStatement();
String sql = "delete from commodityinfo where CID="+sp.getId();
n = st.executeUpdate(sql);
if(n>=1){
JOptionPane.showMessageDialog(null, "删除商品成功");
}else{
JOptionPane.showMessageDialog(null, "删除商品失败");
}
return n;
}

public int ReplceShangPin(ShangPin sp) throws ClassNotFoundException, SQLException{
int n = -1;
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USER, PWD);
java.sql.Statement st = conn.createStatement();
String sql = "upDate commodityinfo set CName=‘"+sp.getName()+"‘, InputPrice="+sp.getBuyInPrice()+", "
+ "OutputPrice="+sp.getUnitPrice()+", Amount="+sp.getStock()+" where CID="+sp.getId();
n = st.executeUpdate(sql);
if(n>=1){
JOptionPane.showMessageDialog(null, "替换商品成功");
}else{
JOptionPane.showMessageDialog(null, "替换商品失败");
}
return n;
}
}

 

创建窗体并实例化对象:

package jdbc;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.UIManager;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.DropMode;
import javax.swing.SwingConstants;

public class Interface extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel jp;
private JLabel labelID,labelName,labelBuyIn,labelPrice,labelStock;
private JButton buttonAdd,buttonReplce,buttonDelete;
private JTextField txtID,txtName,txtBuyIn,txtPrice,txtStock;
private ShangPinManager spm;
private JLabel lblNewLabel;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;

public static void main(String[] args) {
new Interface();
}

public Interface() {
super("商品管理系统");
jp = new JPanel();
jp.setBounds(0, 0, 375, 281);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 385, 309);
labelID = new JLabel("商品ID");
labelID.setBounds(10, 10, 64, 24);
labelID.setForeground(new Color(255, 0, 0));
labelID.setFont(new Font("楷体", Font.BOLD, 20));
txtID = new JTextField(10);
txtID.setBounds(92, 10, 141, 21);

labelName =new JLabel("商品名称");
labelName.setBounds(10, 44, 84, 24);
labelName.setForeground(new Color(255, 0, 0));
labelName.setFont(new Font("楷体", Font.BOLD, 20));
txtName = new JTextField(10);
txtName.setBounds(104, 44, 129, 21);

labelBuyIn =new JLabel("买入价格");
labelBuyIn.setBounds(10, 112, 84, 24);
labelBuyIn.setFont(new Font("楷体", Font.BOLD, 20));
labelBuyIn.setForeground(new Color(255, 0, 0));
txtBuyIn = new JTextField(10);
txtBuyIn.setBounds(104, 112, 129, 21);

labelPrice =new JLabel("商品单价");
labelPrice.setBounds(10, 78, 84, 24);
labelPrice.setFont(new Font("楷体", Font.BOLD, 20));
labelPrice.setForeground(new Color(255, 0, 0));
txtPrice = new JTextField(10);
txtPrice.setBounds(104, 78, 129, 21);

labelStock =new JLabel("商品库存");
labelStock.setBounds(10, 146, 84, 24);
labelStock.setFont(new Font("楷体", Font.BOLD, 20));
labelStock.setForeground(new Color(255, 0, 0));
txtStock = new JTextField(10);
txtStock.setBounds(104, 146, 129, 21);

spm = new ShangPinManager();

buttonAdd = new JButton("新增");
buttonAdd.setBounds(10, 206, 119, 43);
buttonAdd.setFont(new Font("黑体", Font.BOLD, 30));
buttonAdd.setForeground(new Color(255, 69, 0));
buttonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// int id = Integer.valueOf(txtID.getText());
String name = txtName.getText();
int maiRu = Integer.valueOf(txtBuyIn.getText());
int danJia = Integer.valueOf(txtPrice.getText());
int kuCun = Integer.valueOf(txtStock.getText());

ShangPin sp = new ShangPin();
// sp.setId(id);
sp.setName(name);
sp.setBuyInPrice(maiRu);
sp.setUnitPrice(danJia);
sp.setStock(kuCun);
try {
int n = spm.AddShangPin(sp);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});

buttonDelete = new JButton("删除");
buttonDelete.setBounds(253, 206, 112, 43);
buttonDelete.setForeground(new Color(255, 255, 0));
buttonDelete.setFont(new Font("黑体", Font.BOLD, 30));
buttonDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.valueOf(txtID.getText());

ShangPin sp = new ShangPin();
sp.setId(id);
try {
int n = spm.DeleteShangPin(sp);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});

buttonReplce = new JButton("替换");
buttonReplce.setBounds(131, 206, 119, 43);
buttonReplce.setFont(new Font("黑体", Font.BOLD, 30));
buttonReplce.setForeground(new Color(255, 69, 0));
buttonReplce.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int id = Integer.valueOf(txtID.getText());
String name = txtName.getText();
int maiRu = Integer.valueOf(txtBuyIn.getText());
int danJia = Integer.valueOf(txtPrice.getText());
int kuCun = Integer.valueOf(txtStock.getText());

ShangPin sp = new ShangPin();
sp.setId(id);
sp.setName(name);
sp.setBuyInPrice(maiRu);
sp.setUnitPrice(danJia);
sp.setStock(kuCun);
try {
int n = spm.ReplceShangPin(sp);
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});

getContentPane().setLayout(null);
getContentPane().add(jp);
jp.setLayout(null);
jp.add(buttonAdd);
jp.add(buttonDelete);
jp.add(buttonReplce);
jp.add(labelPrice);
jp.add(txtPrice);
jp.add(labelName);
jp.add(txtName);
jp.add(labelID);
jp.add(txtID);
jp.add(labelBuyIn);
jp.add(txtStock);
jp.add(labelStock);
jp.add(txtBuyIn);

textField = new JTextField();
textField.setForeground(new Color(255, 20, 147));
textField.setFont(new Font("微软雅黑", Font.BOLD, 35));
textField.setText("\u5546");
textField.setBounds(301, 10, 41, 37);
jp.add(textField);
textField.setColumns(10);

textField_1 = new JTextField();
textField_1.setForeground(new Color(255, 20, 147));
textField_1.setFont(new Font("微软雅黑", Font.BOLD, 35));
textField_1.setText("\u54C1");
textField_1.setBounds(243, 44, 41, 37);
jp.add(textField_1);
textField_1.setColumns(10);

textField_2 = new JTextField();
textField_2.setForeground(new Color(255, 20, 147));
textField_2.setFont(new Font("微软雅黑", Font.BOLD, 35));
textField_2.setText("\u7BA1");
textField_2.setBounds(243, 99, 41, 37);
jp.add(textField_2);
textField_2.setColumns(10);

textField_3 = new JTextField();
textField_3.setForeground(new Color(255, 20, 147));
textField_3.setFont(new Font("微软雅黑", Font.BOLD, 35));
textField_3.setText("\u7406");
textField_3.setBounds(301, 126, 41, 43);
jp.add(textField_3);
textField_3.setColumns(10);


lblNewLabel = new JLabel("New label");
lblNewLabel.setIcon(new ImageIcon(Interface.class.getResource("/jdbc/2.jpg")));
lblNewLabel.setBounds(0, 0, 375, 271);
jp.add(lblNewLabel);
this.setVisible(true);
}
}

作品(通过窗体实现数据库的增删改)