62,625
社区成员
发帖
与我相关
我的任务
分享/**
* 完成一个迷你版本的学生管理系统
* 可以把Stu表的操作封装在该模型中
* 1:查询任务
* 2:添加一个学生
*/
package stuanswer73;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class StuManage extends JFrame implements ActionListener{
//定义控件
JPanel jp1, jp2;
JLabel jl1;
JButton jb1, jb2, jb3, jb4;
//JScrollPane jsp;
JTextField jtf=null;
StuModel sm=null;
JTable jt=null;
JScrollPane jsp=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
StuManage test=new StuManage();
}
//构造函数
public StuManage()
{
jp1=new JPanel();
jtf=new JTextField(10);
jb1=new JButton("查询");
jb1.addActionListener(this);
jl1=new JLabel("请输入名字");
//把各个控件加入到jp1
jp1.add(jl1);
jp1.add(jtf);
jp1.add(jb1);
jp2=new JPanel();
jb2=new JButton("添加");
jb2.addActionListener(this);
jb3=new JButton("修改");
jb3.addActionListener(this);
jb4=new JButton("删除");
jb4.addActionListener(this);
//把各个按钮加入到jp2
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
//创建一个数据模型对象
sm=new StuModel();
String []paras={"1"};
sm.queryStu("select *from stu1 where 1=?", paras);
// 初始化JTable
jt=new JTable(sm);
//初始化jsp JScrollPane
jsp=new JScrollPane(jt);
//把jsp放入到 jfame
this.add(jsp);
this.add(jp1,"North");
this.add(jp2,"South");
this.setSize(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//判断是那个按钮被按下 .jb1 和 actonperformed必须在同一个类里面
if(arg0.getSource()==jb1)
{
System.out.println("用户想查询");
//因为把表的数据封装在StuModel中了。我们就很容易完成查询
String name=this.jtf.getText().trim();
//写一个sql语句
String sql="select *from stu1 where stuName=?";
String paras[]={name};
//模糊查询
// String sql="select *from stu where stuName like '%"+name+"%'";
//构建新的数据模型类,并更新
sm=new StuModel();
sm.queryStu(sql, paras);
//更新Jtable
jt.setModel(sm);
}
//当用户点击查询
else if(arg0.getSource()==jb2)
{
StuAddDialog sa=new StuAddDialog(this,"添加学生",true);
//重新获得数据类型
//构建新的数据模型类,并更新
sm=new StuModel();
String []paras2={"1"};
sm.queryStu("select * from stu1 where 1=?", paras2);
//更新Jtable
jt.setModel(sm);
}
//点击的是修改
else if(arg0.getSource()==jb3)
{
//用户希望修改
int rowNum=this.jt.getSelectedRow();
if(rowNum==-1)
{
//提示
JOptionPane.showMessageDialog(this, "选择一行");
return;
}
//显示修改对话框
new StuUpdDialog(this,"修改学生",true, sm, rowNum);
//构建新的数据模型类,并更新
sm=new StuModel();
String []paras2={"1"};
sm.queryStu("select * from stu1 where 1=?", paras2);
//更新Jtable
jt.setModel(sm);
}
//点击的是删除
else if(arg0.getSource()==jb4)
{
//说明用户希望删除记录
//得到该学生的id
//如果该用户一行都没有选择就返回-1
int rowNum=this.jt.getSelectedRow();
if(rowNum==-1)
{
//提示
JOptionPane.showMessageDialog(this, "选择一行");
return;
}
//删除数据库中的一条记录
//得到学生的编号
String stuId=(String)sm.getValueAt(rowNum, 0); //返回编号
//创建一个sql语句
String sql="delete from stu1 where stuid=?";
String [] paras={stuId};
StuModel temp=new StuModel();
temp.updStu(sql, paras);
//构建新的数据模型类,并更新
sm=new StuModel();
String []paras2={"1"};
sm.queryStu("select * from stu1 where 1=?", paras2);
//更新Jtable
jt.setModel(sm);
}
}
}
这个是提到的类b
package stuanswer73;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.table.*;
public class StuModel extends AbstractTableModel{
//rowData用来存放行数据
//columnNames存放列名
Vector rowData, columnNames;
//添加,修改,删除都可以用这个方法
public boolean updStu(String sql, String []paras)
{
//创建一个 SqlHelper(如果程序的并发性不考虑可以把对象做出静态的)
SqlHelper sqlHelper=new SqlHelper();
return sqlHelper.updExecute(sql, paras);
}
//查询就用这个方法
//查询的本质就是初始化
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())
{
//rowData
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) {
// TODO: handle exception
e.printStackTrace();
}finally{
sqlHelper.close();
}
}
//得到共有多少列
public int getColumnCount() {
// TODO Auto-generated method stub
return this.columnNames.size();
// return 0;
}
//得到共有多少行
public int getRowCount() {
// TODO Auto-generated method stub
return this.rowData.size();
//return 0;
}
//得到某行某列的数据
public Object getValueAt(int arg0, int arg1) {
// TODO Auto-generated method stub
return ((Vector) this.rowData.get(arg0)).get(arg1); // arg0表示行 arg1 表示列
// return null;
}
//重写方法 getColumnName
@Override
public String getColumnName(int column) {
// TODO Auto-generated method stub
return (String)this.columnNames.get(column);
}
}
麻烦大家帮忙看看,谢谢