java gui 结构_Java第一个GUI项目-学生信息查询-MVC结构

M部分:

package model;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.Vector;

import javax.swing.table.*;

import jdbcUtil.SqlHelper;

//把对学生表的各种操作封装到一个模型中

@SuppressWarnings("serial")

public class Model extends AbstractTableModel{

//rowData用来存放行数据

//columnNames用来存放列名

@SuppressWarnings("rawtypes")

Vector rowData,columnNames;

//定义操作数据库需要的东西

PreparedStatement ps=null;

Connection ct=null;

ResultSet rs=null;

String url="jdbc:mysql://localhost:3306/test?useSSL=true&characterEncoding=utf-8";

String user="root";

String passwd="";

String driver="com.mysql.jdbc.Driver";

//添加学生(增,删,改)

public boolean updStu(String sql,String []paras)

{

//创建sqlHelper(如果程序并发性不考虑,可以把sqlHelper做成static)

SqlHelper sqlHelper = new SqlHelper();

return sqlHelper.updExecute(sql, paras);

}

//查询的本质就是初始化

@SuppressWarnings({ "rawtypes", "unchecked" })

public void queryStu(String sql,String []paras)

{

SqlHelper sqlHelper =null;

//中间

columnNames=new Vector();

//设置列名

columnNames.add("学号");

columnNames.add("名字");

columnNames.add("性别");

columnNames.add("年龄");

columnNames.add("籍贯");

columnNames.add("系别");

rowData=new Vector();

try{

sqlHelper = new SqlHelper();

ResultSet rs=sqlHelper.queryExecute(sql, paras);

while(rs.next())

{

Vector hang=new Vector();

hang.add(rs.getString(1));

hang.add(rs.getString(2));

hang.add(rs.getString(3));

hang.add(rs.getInt(4));

hang.add(rs.getString(5));

hang.add(rs.getString(6));

//加入到rowData

rowData.add(hang);

}

}catch(Exception e){

e.printStackTrace();

}finally{

sqlHelper.close();

}

}

//得到共有多少行

public int getRowCount() {

return this.rowData.size();

}

//得到共有多少列

public int getColumnCount() {

return this.columnNames.size();

}

//得到某行某列的数据

@SuppressWarnings("rawtypes")

public Object getValueAt(int row, int column) {

return ((Vector)this.rowData.get(row)).get(column);

}

@Override

public String getColumnName(int column) {

return (String)this.columnNames.get(column);

}

}

V部分:

主窗口:

38380dbc3599e4721cfd52468201c95a.png

package model;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.Vector;

import javax.swing.table.*;

import jdbcUtil.SqlHelper;

//把对学生表的各种操作封装到一个模型中

@SuppressWarnings("serial")

public class Model extends AbstractTableModel{

//rowData用来存放行数据

//columnNames用来存放列名

@SuppressWarnings("rawtypes")

Vector rowData,columnNames;

//定义操作数据库需要的东西

PreparedStatement ps=null;

Connection ct=null;

ResultSet rs=null;

String url="jdbc:mysql://localhost:3306/test?useSSL=true&characterEncoding=utf-8";

String user="root";

String passwd="";

String driver="com.mysql.jdbc.Driver";

//添加学生(增,删,改)

public boolean updStu(String sql,String []paras)

{

//创建sqlHelper(如果程序并发性不考虑,可以把sqlHelper做成static)

SqlHelper sqlHelper = new SqlHelper();

return sqlHelper.updExecute(sql, paras);

}

//查询的本质就是初始化

@SuppressWarnings({ "rawtypes", "unchecked" })

public void queryStu(String sql,String []paras)

{

SqlHelper sqlHelper =null;

//中间

columnNames=new Vector();

//设置列名

columnNames.add("学号");

columnNames.add("名字");

columnNames.add("性别");

columnNames.add("年龄");

columnNames.add("籍贯");

columnNames.add("系别");

rowData=new Vector();

try{

sqlHelper = new SqlHelper();

ResultSet rs=sqlHelper.queryExecute(sql, paras);

while(rs.next())

{

Vector hang=new Vector();

hang.add(rs.getString(1));

hang.add(rs.getString(2));

hang.add(rs.getString(3));

hang.add(rs.getInt(4));

hang.add(rs.getString(5));

hang.add(rs.getString(6));

//加入到rowData

rowData.add(hang);

}

}catch(Exception e){

e.printStackTrace();

}finally{

sqlHelper.close();

}

}

//得到共有多少行

public int getRowCount() {

return this.rowData.size();

}

//得到共有多少列

public int getColumnCount() {

return this.columnNames.size();

}

//得到某行某列的数据

@SuppressWarnings("rawtypes")

public Object getValueAt(int row, int column) {

return ((Vector)this.rowData.get(row)).get(column);

}

@Override

public String getColumnName(int column) {

return (String)this.columnNames.get(column);

}

}

对话框:

35fd830361fb52328c071301d70091a4.png

//当我们点击一个”添加“按钮时,弹出一个添加学生对话框

package dialog;

import javax.swing.*;

import model.Model;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

@SuppressWarnings("serial")

public class StuAddDialog extends JDialog implements ActionListener{

//定义我需要的swing组件

JLabel jl1,jl2,jl3,jl4,jl5,jl6;

JButton jb1,jb2;

JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;

JPanel jp1,jp2,jp3;

//owner它的父窗口

//title 窗口名

//modal 指定是模态窗口,还是非模态窗口

public StuAddDialog(Frame owner,String title ,boolean modal)

{

super(owner,title,modal);//调用父类构造方法,达到模式对话框效果

jp1=new JPanel();

jp2=new JPanel();

jp3=new JPanel();

jl1=new JLabel("学号");

jl2=new JLabel("姓名");

jl3=new JLabel("性别");

jl4=new JLabel("年龄");

jl5=new JLabel("籍贯");

jl6=new JLabel("系别");

jtf1=new JTextField();

jtf2=new JTextField();

jtf3=new JTextField();

jtf4=new JTextField();

jtf5=new JTextField();

jtf6=new JTextField();

jb1=new JButton("添加");

//注册监听

jb1.addActionListener(this);

jb2=new JButton("取消");

jb2.addActionListener(this);

//设置布局

jp1.setLayout(new GridLayout(6,1));

jp2.setLayout(new GridLayout(6,1));

//添加组件

jp1.add(jl1);

jp1.add(jl2);

jp1.add(jl3);

jp1.add(jl4);

jp1.add(jl5);

jp1.add(jl6);

jp2.add(jtf1);

jp2.add(jtf2);

jp2.add(jtf3);

jp2.add(jtf4);

jp2.add(jtf5);

jp2.add(jtf6);

jp3.add(jb1);

jp3.add(jb2);

this.add(jp1,BorderLayout.WEST);

this.add(jp2,BorderLayout.CENTER);

this.add(jp3,BorderLayout.SOUTH);

//展现

this.setSize(300,250);

this.setVisible(true);

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getSource()==jb1)

{

//希望添加

Model temp=new Model();

String sql="insert into stu values(?,?,?,?,?,?)";

String []paras={jtf1.getText(),jtf2.getText(),jtf3.getText(),jtf4.getText(),jtf5.getText(),jtf6.getText()};

if(!temp.updStu(sql, paras))

{

//提示

JOptionPane.showMessageDialog(this, "添加失败");

}

//关闭对话框

this.dispose();

}

else if(e.getSource()==jb2) {

this.dispose();

}

}

}

//负责对学生的信息进行更新的类

package dialog;

import javax.swing.*;

import model.Model;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

@SuppressWarnings("serial")

public class StuupdDialog extends JDialog implements ActionListener{

//定义我需要的swing组件

JLabel jl1,jl2,jl3,jl4,jl5,jl6;

JButton jb1,jb2;

JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;

JPanel jp1,jp2,jp3;

//owner它的父窗口

//title 窗口名

//modal 指定是模态窗口,还是非模态窗口

public StuupdDialog(Frame owner,String title ,boolean modal,Model sm,int rowNums)

{

super(owner,title,modal);//调用父类构造方法,达到模式对话框效果

jp1=new JPanel();

jp2=new JPanel();

jp3=new JPanel();

jl1=new JLabel("学号");

jl2=new JLabel("姓名");

jl3=new JLabel("性别");

jl4=new JLabel("年龄");

jl5=new JLabel("籍贯");

jl6=new JLabel("系别");

jtf1=new JTextField();

//初始化数据

jtf1.setText((String)sm.getValueAt(rowNums, 0));

//让jtf1不能修改

jtf1.setEditable(false);

jtf2=new JTextField();

jtf2.setText((String)sm.getValueAt(rowNums, 1));

jtf3=new JTextField();

jtf3.setText((String)sm.getValueAt(rowNums, 2));

jtf4=new JTextField();

jtf4.setText((String)sm.getValueAt(rowNums, 3).toString());

jtf5=new JTextField();

jtf5.setText((String)sm.getValueAt(rowNums, 4));

jtf6=new JTextField();

jtf6.setText((String)sm.getValueAt(rowNums, 5));

jb1=new JButton("修改");

//注册监听

jb1.addActionListener(this);

jb2=new JButton("取消");

jb2.addActionListener(this);

//设置布局

jp1.setLayout(new GridLayout(6,1));

jp2.setLayout(new GridLayout(6,1));

//添加组件

jp1.add(jl1);

jp1.add(jl2);

jp1.add(jl3);

jp1.add(jl4);

jp1.add(jl5);

jp1.add(jl6);

jp2.add(jtf1);

jp2.add(jtf2);

jp2.add(jtf3);

jp2.add(jtf4);

jp2.add(jtf5);

jp2.add(jtf6);

jp3.add(jb1);

jp3.add(jb2);

this.add(jp1,BorderLayout.WEST);

this.add(jp2,BorderLayout.CENTER);

this.add(jp3,BorderLayout.SOUTH);

//展现

this.setSize(300,250);

this.setVisible(true);

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(e.getSource()==jb1)

{

//对用户点击添加按钮后的响应动作

//做一个sql

//预编译语句对象

String str="update stu set stuName=?,stuSex=?," +

"stuAge=?,stuJg=?,stuDept=? where stuId=?";

String []paras={jtf2.getText(),jtf3.getText(),jtf4.getText(),jtf5.getText(),jtf6.getText(),jtf1.getText()};

Model temp=new Model();

temp.updStu(str, paras);

this.dispose();

}

else if(e.getSource()==jb2) {

this.dispose();

}

}

}

C部分:

package jdbcUtil;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

//用于对数据库进行操作的类

public class SqlHelper {

//定义操作数据库需要的东西

PreparedStatement ps=null;

Connection ct=null;

ResultSet rs=null;

String url="jdbc:mysql://localhost:3306/test?useSSL=true&characterEncoding=utf-8";

String user="root";

String passwd="";

String driver="com.mysql.jdbc.Driver";

//关闭数据库资源

public void close()

{

//关闭

try{

if(rs!=null) rs.close();

if(ps!=null) ps.close();

if(ct!=null) ct.close();

}catch(Exception e){

e.printStackTrace();

}

}

//写了一个不需要注入的方法

public ResultSet queryExcecute(String sql)

{

try{

//1.加载驱动

Class.forName(driver);

//2.得到连接

ct=DriverManager.getConnection(url,user,passwd);

//3.创建ps

ps=ct.prepareStatement(sql);

rs=ps.executeQuery();

}catch(Exception e){

e.printStackTrace();

}finally{

//关闭资源

}

return rs;

}

//查询数据库的操作

public ResultSet queryExecute(String sql,String []paras)

{

try{

//1.加载驱动

Class.forName(driver);

//2.得到连接

ct=DriverManager.getConnection(url,user,passwd);

//3.创建ps

ps=ct.prepareStatement(sql);

//给ps的?赋值

for(int i=0;i

{

ps.setString(i+1, paras[i]);

}

rs=ps.executeQuery();

}catch(Exception e){

e.printStackTrace();

}finally{

//关闭资源

}

return rs;

}

//把增删改合在一起

public boolean updExecute(String sql,String []paras)

{

boolean b=true;

try{

//1.加载驱动

Class.forName(driver);

//2.得到连接

ct=DriverManager.getConnection(url,user,passwd);

//3.创建ps

ps=ct.prepareStatement(sql);

//给ps的?赋值

for(int i=0;i

{

ps.setString(i+1, paras[i]);

}

//4.执行操作

if(ps.executeUpdate()!=1)

{

b=false;

}

}catch(Exception e){

b=false;

e.printStackTrace();

}finally{

this.close();

}

return b;

}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值