JAVA GUI +JDBC 实现超市管理系统完整版

需求:第一个窗口为欢迎 界面 ,点击按钮 ,会进入管理系统界面


开始界面


进入管理界面

思路:1.用可视化工具 ,首先实现图形界面 ,然后实现每个组件的具体功能 ,

2. 每一次执行操作之后,对显示水果列表的组件进行刷新。

刷新功能


private int update_UI(JPanel panel, Statement stmt, JTextPane textPane, JTextPane textPane_1,

JTextPane textPane_2, JTextPane textPane_3) throws SQLException {

panel.updateUI();//首先将其全部组件进行刷新

panel.add(textPane);

panel.add(textPane_1);

panel.add(textPane_2);

panel.add(textPane_3);//添加上基础的四个组件

Statement stmt1=stmt;//获取操作数据库的权限

String sql="select * from goods_info";

ResultSet rs=stmt1.executeQuery(sql);//执行sql语句,这条语句会返回一个Set集合,里面包含要获取的信息

int pos=21;

while(rs.next()) {//对其进行迭代

String name=rs.getString("name");

int price=rs.getInt("price");

int id=rs.getInt("id");

String unit=rs.getString("unit");//每一次获取到完整的四个数据

System.out.println(id+name+price+"/"+"unit");

JTextPane t1=new JTextPane();//创建临时按钮,设置文本,设置不可编辑

t1.setText(id+"");//填充数值

t1.setEditable(false);//设置不可被改动

t1.setFont(new Font("宋体", Font.BOLD, 12));

t1.setBorder(BorderFactory.createEtchedBorder());//设置边框

JTextPane t2=new JTextPane();

t2.setText(name);

t2.setEditable(false);

t2.setFont(new Font("宋体", Font.BOLD, 12));

t2.setBorder(BorderFactory.createEtchedBorder());

JTextPane t3=new JTextPane();

t3.setText(price+"");

t3.setEditable(false);

t3.setFont(new Font("宋体", Font.BOLD, 12));

t3.setBorder(BorderFactory.createEtchedBorder());

JTextPane t4=new JTextPane();

t4.setText(unit+"");

t4.setEditable(false);

t4.setFont(new Font("宋体", Font.BOLD, 12));

t4.setBorder(BorderFactory.createEtchedBorder());

t1.setBounds(0,pos, 100, 20);//X Y  WIDTH HEIGH

panel.add(t1);

t2.setBounds(100,pos, 100, 20);

panel.add(t2);

t3.setBounds(200,pos, 100, 20);

panel.add(t3);

t4.setBounds(300,pos, 100, 20);

panel.add(t4);

pos+=20;

}

return pos-20;

}

删除功能:

添加功能:

btnNewButton.addMouseListener(new MouseAdapter() {//提交按钮事件,比较特殊,因为不需要刷新,只要在末尾添加即可。。大概吧

@Override

public void mouseClicked(MouseEvent e) {

/*

* 当点击提交按钮时, 先判断前四个按钮 是否都有文本,

* 如果含有文本,那么就创建 四个不可改变的textPane

* 给它们对应赋值以后,放到主显示框上

*/

//获取数据

String id=textPane1.getText();

String name=textPane2.getText();

String price=textPane3.getText();

String danwei=textPane4.getText();

//判断是否为空值

if(!id.isEmpty()&&!name.isEmpty()&&!price.isEmpty()&&!danwei.isEmpty()) {//必须用是否为空判断 ==?

y_pos+=20;

System.out.println(id+"---"+name+"---"+price+"---"+danwei);

//输入正确,处理数据1、清空 输入框的数据 2、将数据存储到mysql 中3、将数据上传

textPane1.setText(null);

    textPane2.setText(null);

textPane3.setText(null);

textPane4.setText(null);

JTextPane t1=new JTextPane();//创建临时按钮,设置文本,设置不可编辑

t1.setText(id);//填充数值

t1.setEditable(false);//设置不可被改动

t1.setFont(new Font("宋体", Font.BOLD, 12));

t1.setBorder(BorderFactory.createEtchedBorder());//设置边框

JTextPane t2=new JTextPane();

t2.setText(name);

t2.setEditable(false);

t2.setFont(new Font("宋体", Font.BOLD, 12));

t2.setBorder(BorderFactory.createEtchedBorder());

JTextPane t3=new JTextPane();

t3.setText(price);

t3.setEditable(false);

t3.setFont(new Font("宋体", Font.BOLD, 12));

t3.setBorder(BorderFactory.createEtchedBorder());

JTextPane t4=new JTextPane();

t4.setText(danwei);

t4.setEditable(false);

t4.setFont(new Font("宋体", Font.BOLD, 12));

t4.setBorder(BorderFactory.createEtchedBorder());

t1.setBounds(0,y_pos, 100, 20);//X Y  WIDTH HEIGH

t2.setBounds(100,y_pos, 100, 20);

t3.setBounds(200,y_pos, 100, 20);

t4.setBounds(300,y_pos, 100, 20);

//将数据存入MySQL中

String sql="insert into goods_info value("+id+",\""+name+"\","+price+",\""+danwei+"\");";

try {

stmt.execute(sql);

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

try {

y_pos=update_UI(panel,stmt,textPane,textPane_1,textPane_2,textPane_3);

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}

});

改动功能:

button.addMouseListener(new MouseAdapter() {//改动按钮事件

@Override

public void mouseClicked(MouseEvent e) {

/*

* 当点击改动按钮的时候,获取到改动的值,id值是必须要输入的!

* 。。。好吧如果只改动一个值那么就要判断好多次,,,还是全都输入才能改动吧(其实就是懒)

*

*/

//获取数据

String id=textPane5.getText();

String name=textPane6.getText();

String price=textPane7.getText();

String danwei=textPane8.getText();

//判断是否为空值

if(!id.isEmpty()&&!name.isEmpty()&&!price.isEmpty()&&!danwei.isEmpty()) {//必须用是否为空判断 ==?

textPane5.setText(null);

    textPane6.setText(null);

textPane7.setText(null);

textPane8.setText(null);//首先清除所有的值

//然后执行sql语句,将数据值进行更新

String sql="update goods_info set name=\""+name+"\",price="+price+",unit=\""+danwei+"\" where id="+id;

try {

stmt.execute(sql);

} catch (SQLException e2) {

// TODO Auto-generated catch block

e2.printStackTrace();

}

//然后刷新一下就好了

try {

y_pos=update_UI(panel,stmt,textPane,textPane_1,textPane_2,textPane_3);

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}

}


难点在于 

1.结合了数据库 ,需要不断尝试将sql语句转换成有效的字符串,

2.组件之间的互动 ,要明确功能  和需要的组件 。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容