一个自制精美的基础网页,主要用于商品售卖、产品展示等
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import connection.my_connection;
public class caigou extends JFrame implements ActionListener
{
private static final long serialVersionUID = 7975505039196904660L;
Connection con;
Statement st;
private JTable ta;
private JScrollPane sPane;
private JButton btn;
private JButton btn1;
private JButton btn2;
private JButton btn3;
private String []c={"物料编号","物料名称","物料数量","金额","单位","采购时间","采购员姓名编号","供应商"};
private String [][]v={};
private DefaultTableModel tablemodel;
public JTable getTa()
{
return ta;
}
public void setTa(JTable ta)
{
this.ta = ta;
}
private JTextField e1;
private JTextField e2;
private JTextField e3;
private JTextField e4;
private JTextField e5;
private JTextField e6;
private JTextField e7;
private JTextField e8;
private JTextField e9;
private JLabel E1;
private JLabel E2;
private JLabel E3;
private JLabel E4;
private JLabel E5;
private JLabel E6;
private JLabel E7;
private JLabel E8;
private JLabel E9;
private JPanel p0;
private JPanel p1;
private JPanel p2;
private JPanel p3;
private JPanel p4;
private JPanel p5;
private JPanel p6;
JToolBar toolBar;
Box top;
Box top1;
Box top2;
Object data[][]=new Object[0][4];
// String title[]=new String[4];
private void init()
{
tablemodel=new DefaultTableModel(v,c);
ta=new JTable(tablemodel);
ta.setModel(tablemodel);
toolBar = new JToolBar();
top1 = Box.createVerticalBox();
top2 = Box.createVerticalBox();
// top1 =Box.createHorizontalBox();
sPane=new JScrollPane();
E1=new JLabel("物料编号");
E2=new JLabel("物料编号");
E3=new JLabel("物料名称");
E4=new JLabel("物料数量");
E5=new JLabel("金额");
E6=new JLabel("单位");
E7=new JLabel("采购时间");
E8=new JLabel("采购员姓名编号");
E9=new JLabel("供应商");
btn1=new JButton("删除");
btn2=new JButton("修改");
btn3=new JButton("添加");
btn=new JButton("采购信息");
btn.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
p0=new JPanel();
p1=new JPanel();
p2=new JPanel();
p3=new JPanel();
p4=new JPanel();
setSize(1000, 600);
setLocation(200, 100);
setVisible(true);
setResizable(false);
e1=new JTextField(12);
e2=new JTextField(12);
e3=new JTextField(12);
e4=new JTextField(12);
e5=new JTextField(12);
e6=new JTextField(12);
e7=new JTextField(12);
e8=new JTextField(12);
e9=new JTextField(12);
p4.setBorder(new TitledBorder(new EtchedBorder(), "查询所有采购信息"));
p4.add(btn);
p1.add(btn1);
p1.add(E1);
p1.add(e1);
p0.add(btn2);
p0.add(btn3);
p0.add(E2);
p0.add(e2);
p0.add(E3);
p0.add(e3);
p0.add(E4);
p0.add(e4);
p0.add(E5);
p0.add(e5);
p5.add(E6);
p5.add(e6);
p5.add(E7);
p6.add(e7);
p6.add(E8);
p6.add(e8);
p6.add(E9);
p6.add(e9);
top1.setBorder(new TitledBorder(new EtchedBorder(), "管理采购信息"));
top1.add(p2);
top1.add(p1);
top2.add(p2);
top2.add(p0);
top=Box.createVerticalBox();
top.add(Box.createHorizontalStrut(20));
top.add(p4);
top.add(top1);
top.add(top2);
p3.add(new JLabel("采购信息表 "));
top.add(p3);
top.add(sPane);
top.add(Box.createGlue());
add(toolBar, BorderLayout.NORTH);
add(top, BorderLayout.CENTER);
add(new JLabel(" "), BorderLayout.SOUTH);
add(new JLabel(" "), BorderLayout.EAST);
add(new JLabel(" "), BorderLayout.WEST);
}
public caigou() throws SQLException
{
this.setTitle("采购管理");
con=my_connection.getConnection();
st=con.createStatement();
init();
ta.setModel(new DefaultTableModel(data,c));
sPane.setViewportView(ta);
ta.setEnabled(false);
setVisible(true);
setResizable(false);
setAlwaysOnTop(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1){
try {
con=my_connection.getConnection();
java.sql.Statement
cmd=con.createStatement();
int rs=cmd.executeUpdate("delete from 采购信息 where 物料编号 ='" e1.getText() "'");
if(rs!=0)
{
JOptionPane.showMessageDialog(null,"删除成功!");
e1.setText("");
/* e3.setText("");
e4.setText("");
e5.setText(""); */
}
else
{
JOptionPane.showMessageDialog(null,"删除失败,请重新输入!");
}
}
catch (Exception ex) {
JOptionPane.showMessageDialog(this,"信息查询失败!");
ex.printStackTrace();
}
}
else if(e.getSource()==btn)
{
try {
ResultSet rs=st.executeQuery("select * from 采购信息");
tablemodel.setRowCount(0);//清零
// tablemodel.getRowCount();//获得行数
while(rs.next())
{
String data[]={rs.getString(1),rs.getString(2),"" rs.getInt(3) "","" rs.getInt(4) "",rs.getString(5),"" rs.getDate(6) "",rs.getString(7),rs.getString(8)};
tablemodel.addRow(data);//ta.setModel(tablemodel);
}
ta.setModel(tablemodel);
}
catch (SQLException e1) {
JOptionPane.showMessageDialog(this,"信息查询失败!");
e1.printStackTrace();
}
}
else if(e.getSource()==btn2){
try{
int rs;
con=my_connection.getConnection();
java.sql.Statement cmd=con.createStatement();
rs=cmd.executeUpdate("update 采购信息set 物料名称= '" e3.getText() "',物料数量= '" e4.getText() "',金额= '" e5.getText() "',单位= '" e6.getText() "',采购时间= '" e7.getText() "',采购员编号= '" e8.getText() "',供应商= '" e9.getText() "' where 物料编号='" e2.getText() "'");
if(rs!=0){
JOptionPane.showMessageDialog(null,"修改成功!");
}
}
catch(Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null,"请重新输入!");
}
}
else if(e.getSource()==btn3){
try{
//int rs;
con=my_connection.getConnection();
java.sql.Statement cmd=con.createStatement();
String sqlinsert="INSERT into 采购信息 values('" e2.getText() "','" e3.getText() "','" e4.getText() "','" e5.getText() "','" e6.getText() "','" e7.getText() "','" e8.getText() "','" e9.getText() "')";
cmd=con.createStatement();
cmd.executeUpdate(sqlinsert);
JOptionPane.showMessageDialog(null,"添加成功!");
}
catch(Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null,"请重新输入!");
}
}
}
public static void main(String[] args) throws SQLException
{
caigou u=new caigou();
u.setAlwaysOnTop(true);
}
}
评论