admin管理员组文章数量:1573361
实现简单的信息管理系统,我们三个人经过几天的努力终于完成了用java界面实现连接数据库,并附带增删改查的基本功能的信息管理系统。完成后到现在已经过去很长时间了,最近在整理以前写过的代码的时候发现的,在这里进行总结一下。
一、实现功能
- 用户向家具管理系统请求信息,系统通过与数据库的连接调用数据库中的信息来响应用户,用户可以在系统中进行增加、删除、修改、查询的功能,系统通过连接在数据库中进行相应的增加、删除、修改、查询并返回结果。
二、代码实现
- 代码1:增删改查功能实现和面板设置MainFrame类。
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class MainFrame extends JFrame implements ActionListener
{
JLabel numLa,nameLa;
JTextField numTxt,nameTxt;
JButton numFBt,nameFBt,addBt,delBt,upBt;
JTable table;
JScrollPane panel;
public MainFrame()
{
//JFrame DengLu = new JFrame("欢迎使用家具管理系统");
//设置关闭窗口时的默认操作
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口位置
this.setLocation(200, 200);
//设置窗口大小
this.setSize(400, 300);
//设置窗体为空
this.setLayout(null);
//插入密码按键
JLabel passLa = new JLabel("密 码");
passLa.setSize(80, 60);
passLa.setLocation(60, 70);
//插入用户名按键
JLabel userLa = new JLabel("用户名");
userLa.setSize(80, 60);
userLa.setLocation(60, 20);
//添加密码和用户名
this.add(userLa);
this.add(passLa);
//设置文本域
JTextField userText = new JTextField(30);
JTextField passText = new JTextField(30);
userText.setLocation(130, 30);
userText.setSize(150, 30);
passText.setLocation(130, 80);
passText.setSize(150, 30);
//添加文本域
this.add(userText);
this.add(passText);
//添加登陆和退出按钮
JButton sureBut = new JButton("确认");
JButton returnBut = new JButton("返回");
sureBut.setLocation(60, 170);
returnBut.setLocation(240, 170);
sureBut.setSize(60, 40);
returnBut.setSize(60, 40);
//添加按钮
this.add(sureBut);
this.add(returnBut);
//显示顶级容器
this.setVisible(true);
returnBut.addActionListener(e -> {
userText.setText("");
passText.setText("");
});
sureBut.addActionListener(e -> {
String user=userText.getText();
String pass=passText.getText();
if (user.equals("111") && pass.equals("111"))
{
this.setSize(800,600);
this.setTitle("家具管理系统");
this.getContentPane().removeAll();
this.setLayout(null);
numLa=new JLabel("名称");
numLa.setSize(60, 30);
numLa.setLocation(30,30);
this.add(numLa);
numTxt=new JTextField();
numTxt.setSize(150, 30);
numTxt.setLocation(90,30);
this.add(numTxt);
numFBt=new JButton("名称查找");
numFBt.setSize(90, 30);
numFBt.setLocation(280,30);
numFBt.addActionListener(this);
this.add(numFBt);
nameLa=new JLabel("风格");
nameLa.setSize(60, 30);
nameLa.setLocation(410,30);
this.add(nameLa);
nameTxt=new JTextField();
nameTxt.setSize(150, 30);
nameTxt.setLocation(470,30);
this.add(nameTxt);
nameFBt=new JButton("风格查找");
nameFBt.setSize(90, 30);
nameFBt.setLocation(660,30);
nameFBt.addActionListener(this);
this.add(nameFBt);
addBt=new JButton("增加");
addBt.setSize(60, 30);
addBt.setLocation(30,90);
addBt.addActionListener(this);
this.add(addBt);
delBt=new JButton("删除");
delBt.setSize(60, 30);
delBt.setLocation(120,90);
delBt.addActionListener(this);
this.add(delBt);
upBt=new JButton("修改");
upBt.setSize(60, 30);
upBt.setLocation(210,90);
upBt.addActionListener(this);
this.add(upBt);
StudentDAO dao=new StudentDAO();
ArrayList list=dao.findStuBySname("");
initTable(list);
this.setVisible(true);
}
});
}
public void initTable(ArrayList<Furniture> stus)//初始化表格的方法
{
if((stus!=null)||(stus.size()!=0))
{
if(panel!=null)
{
this.remove(panel);
}
String[] columnNames = { "编号", "名称", "价格", "数量", "日期" ,"风格"};
String[][] values = new String[stus.size()][6];
for (int i = 0; i < stus.size(); i++) {
Furniture stu = (Furniture) stus.get(i);
values[i][0] = String.valueOf(stu.getId());
values[i][1] = stu.getName();
values[i][2] = String.valueOf(stu.getPrice());
values[i][3] = String.valueOf(stu.getNum());
values[i][4] = stu.getDates();
values[i][5] = stu.getStyle();
}
table = new JTable(values, columnNames);
panel = new JScrollPane(table);
panel.setSize(750, 400);
panel.setLocation(20, 150);
this.add(panel);
}
}
public void actionPerformed(ActionEvent e)
{
JButton bt=(JButton)e.getSource();
if(bt.getText().equals("风格查找"))
{
StudentDAO dao=new StudentDAO();
ArrayList list=dao.findStuBySname(nameTxt.getText().trim());
initTable(list);
}
else
{
if(bt.getText().equals("名称查找"))
{
StudentDAO dao=new StudentDAO();
ArrayList list=new ArrayList();
Furniture stu=dao.findStuBySno(numTxt.getText().trim());
if(stu!=null)
{
list.add(stu);
}
initTable(list);
}
else
{
if(bt.getText().equals("删除"))
{
if(table.getSelectedRow()==-1)
{
JOptionPane.showMessageDialog(this, "请选中要删除的家具名称");
}
else
{
StudentDAO dao = new StudentDAO();
dao.delStudent(table.getValueAt(table.getSelectedRow(),1).toString());
ArrayList list = dao.findStuBySname("");
initTable(list);
}
}
else
{
if(bt.getText().equals("修改"))
{
if(table.getSelectedRow()==-1)
{
JOptionPane.showMessageDialog(this, "请选中要修改的家具名称");
}
else
{
int row = table.getSelectedRow();
String id = table.getValueAt(row, 0).toString();
String name = table.getValueAt(row, 1).toString();
int price = Integer.parseInt(table.getValueAt(row, 2)
.toString());
int num = Integer.parseInt(table.getValueAt(row, 3)
.toString());
String dates = table.getValueAt(row, 4).toString();
String style = table.getValueAt(row, 4).toString();
Furniture stu = new Furniture(name,price ,num,dates,style);
AddOrUpdateFrame newFrame = new AddOrUpdateFrame(
"修改", stu, this);
}
}
else
{
AddOrUpdateFrame newFrame=new AddOrUpdateFrame("增加",null,this);
}
}
}
}
}
}
- 代码2:用于获取数据库对象的Furniture类。
public class Furniture {
private int id;
private String name;
private int price;
private int num;
private String dates;
private String style;
public Furniture( int id,String name, int price, int num, String dates, String style) {
this.id = id;
this.name = name;
this.price = price;
this.num = num;
this.dates = dates;
this.style = style;
}
public Furniture( String name, int price, int num, String dates, String style) {
this.id = id;
this.name = name;
this.price = price;
this.num = num;
this.dates = dates;
this.style = style;
}
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 getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getDates() {
return dates;
}
public void setDates(String dates) {
this.dates = dates;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
}
- 代码3:弹出框面板设计AddOrUpdateFrame类。
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class AddOrUpdateFrame extends JFrame implements ActionListener{
JLabel numLa,nameLa,sexLa,ageLa,deptLa;
JTextField numTxt,nameTxt,ageTxt,numText,sexTxt;
JComboBox deptCom;
JButton bt;
MainFrame main;//定义主窗口
public AddOrUpdateFrame(String type,Furniture stu,MainFrame main)
{
this.setSize(300,400);
this.setLayout(null);
numLa=new JLabel("名称");
numLa.setSize(60, 30);
numLa.setLocation(30, 30);
this.add(numLa);
numTxt=new JTextField();
numTxt.setSize(120, 30);
numTxt.setLocation(100, 30);
this.add(numTxt);
nameLa=new JLabel("价格");
nameLa.setSize(60, 30);
nameLa.setLocation(30, 80);
this.add(nameLa);
nameTxt=new JTextField();
nameTxt.setSize(120, 30);
nameTxt.setLocation(100, 80);
this.add(nameTxt);
sexLa=new JLabel("数量");
sexLa.setSize(60, 30);
sexLa.setLocation(30, 130);
this.add(sexLa);
numText=new JTextField();
numText.setSize(120, 30);
numText.setLocation(100, 130);
this.add(numText);
ageLa=new JLabel("日期");
ageLa.setSize(60, 30);
ageLa.setLocation(30, 180);
this.add(ageLa);
ageTxt=new JTextField();
ageTxt.setSize(120, 30);
ageTxt.setLocation(100, 180);
this.add(ageTxt);
deptLa=new JLabel("风格");
deptLa.setSize(60, 30);
deptLa.setLocation(30, 230);
this.add(deptLa);
deptCom=new JComboBox();
deptCom.setSize(120, 30);
deptCom.setLocation(100, 230);
deptCom.addItem("地中海风格");
deptCom.addItem("古典风格");
deptCom.addItem("欧式风格");
deptCom.addItem("现代前卫风格");
this.add(deptCom);
if(type.equals("增加"))
{
this.setTitle("增加");
bt=new JButton("增加");
bt.setSize(60, 30);
bt.setLocation(90,280);
}
else
{
this.setTitle("修改");
bt=new JButton("修改");
bt.setSize(60, 30);
bt.setLocation(90,280);
if(stu!=null)
{
numTxt.setText(stu.getName());
numTxt.setEditable(false);
nameTxt.setText(String.valueOf(stu.getPrice()));
numText.setText(String.valueOf(stu.getNum()));
ageTxt.setText(stu.getDates());
deptCom.setSelectedItem(stu.getStyle());
}
}
this.add(bt);
bt.addActionListener(this);
this.setVisible(true);
this.main=main;
}
public void actionPerformed(ActionEvent e)
{
JButton bt=(JButton)e.getSource();
StudentDAO dao=new StudentDAO();
String name=numTxt.getText();
int price=Integer.valueOf(nameTxt.getText());
int num=Integer.valueOf(numText.getText());
String data=ageTxt.getText();
String style =deptCom.getSelectedItem().toString();
Furniture stu=new Furniture(name,price,num,data,style);
if(bt.getText().equals("修改"))
{
dao.updateStudent(stu);
}
else
{
dao.addStudent(stu);
}
ArrayList list=dao.findStuBySname("");
main.initTable(list);
this.dispose();
}
}
- 代码4:数据库操作类Dao类。
//对于表进行数据操作的类
import java.sql.*;
import java.util.*;
import static javax.swing.UIManager.getString;
public class StudentDAO {
private Connection con;
public StudentDAO()
{
try{
Class.forName("com.mysql.jdbc.Driver");
//加载驱动
String conStr="jdbc:mysql://localhost:3306/java12?characterEncoding=utf8&useSSL=false; DatabaseName=java12";
//配置连接字符串
String user="填写自己的用户名";
//配置用户名
String password="填写自己的密码";
//配置用户的访问密码
con=DriverManager.getConnection(conStr,user,password);
//创建数据库连接对象
}
catch (ClassNotFoundException e) {//捕捉处理驱动类未找到异常
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {//捕捉处理数据连接或者操作异常
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//精确查找
public Furniture findStuBySno(String sno)
{
Furniture stu=null;
try{
PreparedStatement ps=con.prepareStatement("select id, name, price, num, dates,style from Furnitures where name = ?");
ps.setString(1, sno);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
int id=rs.getInt(1);
String name=rs.getString(2);
int price=rs.getInt(3);
int num=rs.getInt(4);
String dates=rs.getString(5);
String style=rs.getString(6);
stu=new Furniture(id,name,price,num,dates,style);
}
ps.close();
//关闭SQL语句执行对象
//con.close();
//关闭数据库连接对象
}
catch (SQLException e) {//捕捉处理数据连接或者操作异常
// TODO Auto-generated catch block
e.printStackTrace();
}
return stu;
}
//模糊查找
public ArrayList<Furniture> findStuBySname(String sstyle)
{
ArrayList<Furniture> result=new ArrayList<Furniture>();
try{
Statement st=con.createStatement();
String strSQL="select * from Furnitures where style like '%"+sstyle+"%';";
ResultSet rs=st.executeQuery(strSQL);
while(rs.next())
{
int id=rs.getInt(1);
String name=rs.getString(2);
int price=rs.getInt(3);
int num=rs.getInt(4);
String dates=rs.getString(5);
String style=rs.getString(6);
Furniture stu=new Furniture(id ,name, price, num, dates, style);
result.add(stu);
}
st.close();
//关闭SQL语句执行对象
//con.close();
//关闭数据库连接对象
}
catch (SQLException e) {//捕捉处理数据连接或者操作异常
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public void addStudent(Furniture stu)
{
try{
PreparedStatement ps=con.prepareStatement("insert into Furnitures( name, price, num,dates, style) values (?,?,?,?,?);");
//创建SQL语句执行对象
ps.setString(1, stu.getName());
ps.setInt(2,(stu.getPrice()));
ps.setInt(3,(stu.getNum()));
ps.setString(4, (stu.getDates()));
ps.setString(5,stu.getStyle());
ps.execute();
ps.close();
//关闭SQL语句执行对象
//con.close();
//关闭数据库连接对象
}
catch (SQLException e) {//捕捉处理数据连接或者操作异常
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void delStudent(String sname)
{
try{
PreparedStatement ps=con.prepareStatement("delete from Furnitures where name=?");
//创建SQL语句执行对象
ps.setString(1, sname);
ps.execute();
ps.close();
//关闭SQL语句执行对象
//con.close();
//关闭数据库连接对象
}
catch (SQLException e) {//捕捉处理数据连接或者操作异常
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void updateStudent(Furniture stu)
{
try{
PreparedStatement ps=con.prepareStatement("update furnitures set price = ?,num =?, style =? where name = ?");
//创建SQL语句执行对象
ps.setInt(1,stu.getPrice());
ps.setInt(2, stu.getNum());
ps.setString(3,stu.getStyle());
ps.setString(4, stu.getName());
ps.execute();
ps.close();
//关闭SQL语句执行对象
//con.close();
//关闭数据库连接对象
}
catch (SQLException e) {//捕捉处理数据连接或者操作异常
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- 代码5:程序运行的入口Test类。
import java.util.*;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
MainFrame main=new MainFrame();
}
}
- 实现的页面如下:
版权声明:本文标题:家具信息管理系统 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1727746097a1127820.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论