StuMainInterface.java 文件:
package com.cn.stu;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class StuMainInterface extends JFrame implements ActionListener {
JLabel jl1;
JButton jb1,jb2,jb3,jb4,jb5;
JTextField jtf;
JTable jt;
JScrollPane jsp;
JPanel jp1,jp3;
TableModel tm;
public static void main(String[] args) {
// TODO Auto-generated method stub
StuMainInterface demo=new StuMainInterface();
}
public StuMainInterface(){
jl1=new JLabel("请输入将要查询的名字");
jtf=new JTextField(10);
jb1=new JButton("查询");
jp1=new JPanel();
jp1.add(jl1);
jp1.add(jtf);
jp1.add(jb1);
jb1.addActionListener(this);
jb2=new JButton("添加");
jb3=new JButton("修改");
jb4=new JButton("删除");
jb5=new JButton("返回");
jp3=new JPanel();
jp3.add(jb2);
jp3.add(jb3);
jp3.add(jb4);
jp3.add(jb5);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
jb5.addActionListener(this);
tm=new TableModel();
jt=new JTable(tm);
jsp=new JScrollPane(jt);
//jt.setBackground(Color.pink);
jt.setForeground(Color.black);
jt.setGridColor(Color.red);
this.add(jp1, BorderLayout.NORTH);
this.add(jp3, BorderLayout.SOUTH);
this.add(jsp);
this.setTitle("mini学生管理系统");
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb1){
String name=this.jtf.getText().trim();
if(name.equals("")){
JOptionPane.showMessageDialog(this,"请输入要查询的名字,不可为空");
String sql="select * from stu";
tm=new TableModel(sql);
jt.setModel(tm);
return ;
}
String sql="select * from stu where stuName='"+name+"'";
tm=new TableModel(sql);
jt.setModel(tm);
}else if(e.getSource()==jb5){
String sql="select * from stu";
tm=new TableModel(sql);
jt.setModel(tm);
}else if(e.getSource()==jb2){
AddInterface addi=new AddInterface(this,"添加学生窗口",true);
tm=new TableModel();
jt.setModel(tm);
}else if(e.getSource()==jb3){
int rowNum=this.jt.getSelectedRow();
if(rowNum==-1){
JOptionPane.showMessageDialog(this, "请先选择一行");
return;
}
//System.out.println("触发:"+rowNum);
UpdateInterface upd=new UpdateInterface(this,"修改学生窗口",true,rowNum,tm);
tm=new TableModel();
jt.setModel(tm);
}else if(e.getSource()==jb4){
int rowNum=this.jt.getSelectedRow();
if(rowNum==-1){
JOptionPane.showMessageDialog(this, "请先选择一行");
return;
}
tm=new TableModel();
String k=tm.getValueAt(rowNum, 0).toString();
//System.out.println(k);
String []tt={k};
String sql="delete from stu where stuId=?";
tm.doSomething(sql, tt);
tm=new TableModel();
jt.setModel(tm);
}
}
}
AddInterface.java 文件
package com.cn.stu;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class AddInterface extends JDialog implements ActionListener {
JButton jb1,jb2;
JLabel jl1,jl2,jl3,jl4,jl5,jl6;
JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;
JPanel jp1,jp2,jp3;
public AddInterface(Frame owner,String title,boolean model){
super(owner,title,model);
jl1=new JLabel("学号");
jl2=new JLabel("姓名");
jl3=new JLabel("性别");
jl4=new JLabel("年龄");
jl5=new JLabel("籍贯");
jl6=new JLabel("系别");
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
jtf4=new JTextField(10);
jtf5=new JTextField(10);
jtf6=new JTextField(10);
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp1.setLayout(new GridLayout(6,1));
jp1.add(jl1);
jp1.add(jl2);
jp1.add(jl3);
jp1.add(jl4);
jp1.add(jl5);
jp1.add(jl6);
this.add(jp1, BorderLayout.WEST);
jp2.setLayout(new GridLayout(6,1));
jp2.add(jtf1);
jp2.add(jtf2);
jp2.add(jtf3);
jp2.add(jtf4);
jp2.add(jtf5);
jp2.add(jtf6);
this.add(jp2);
jb1=new JButton("确认添加");
jb2=new JButton("取消");
jb1.addActionListener(this);
jb2.addActionListener(this);
jp3.add(jb1);
jp3.add(jb2);
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){
if(!(jtf3.getText().equals("男"))&&(!(jtf3.getText().equals("女")))){
JOptionPane.showMessageDialog(this, "请正确输入性别,男或女");
return;
}
if(jtf1.getText().equals("")||jtf2.getText().equals("")){
JOptionPane.showMessageDialog(this, "请填入必要信息");
return;
}
String sql="insert into stu values(?,?,?,?,?,?)";
TableModel tm=new TableModel();
String [] param={jtf1.getText(),jtf2.getText(),jtf3.getText(),jtf4.getText(),jtf5.getText(),jtf6.getText()};
tm.doSomething(sql, param);
this.dispose();
}
if(e.getSource()==jb2){
this.dispose();
}
}
}
TableModel.java文件
package com.cn.stu;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
public class TableModel extends AbstractTableModel {
PreparedStatement ps=null;
Connection ct=null;
String Driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;databasename=master";
ResultSet rs=null;
Vector rowDate,columnNames;
public PreparedStatement ConDb(String sql)
{
try{
Class.forName(Driver);
ct=DriverManager.getConnection(url,"sa","1");
ps=ct.prepareStatement(sql);
}catch(Exception e){
e.printStackTrace();
}
return ps;
}
//增加、修改
public void doSomething(String sql,String []param){
ps=this.ConDb(sql);
try{
for(int i=0;i<param.length;i++){
ps.setString(i+1, param[i]);
}
ps.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(rs!=null) rs.close();
if(ps!=null) ps.close();
if(ct!=null) ct.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//查询
public void init(String sql){
if(sql.equals("")){
sql="select * from stu";
}
columnNames=new Vector();
columnNames.add("学号");
columnNames.add("名字");
columnNames.add("性别");
columnNames.add("年龄");
columnNames.add("籍贯");
columnNames.add("部门");
rowDate=new Vector();
try {
ps=this.ConDb(sql);
rs=ps.executeQuery();
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));
rowDate.add(hang);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(rs!=null) rs.close();
if(ps!=null) ps.close();
if(ct!=null) ct.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public TableModel(){
this.init("");
}
public TableModel(String sql){
this.init(sql);
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return this.rowDate.size();
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return this.columnNames.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return ((Vector)this.rowDate.get(rowIndex)).get(columnIndex);
}
@Override
public String getColumnName(int column) {
// TODO Auto-generated method stub
return (String)this.columnNames.get(column);
}
}
UpdateInterface.java文件
package com.cn.stu;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class UpdateInterface extends JDialog implements ActionListener{
JButton jb1,jb2;
JLabel jl1,jl2,jl3,jl4,jl5,jl6;
JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;
JPanel jp1,jp2,jp3;
public UpdateInterface(Frame owner,String title,boolean model,int rowNum,TableModel tm){
super(owner,title,model);
jl1=new JLabel("学号");
jl2=new JLabel("姓名");
jl3=new JLabel("性别");
jl4=new JLabel("年龄");
jl5=new JLabel("籍贯");
jl6=new JLabel("系别");
System.out.println(rowNum);
jtf1=new JTextField(10);
jtf2=new JTextField(10);
jtf3=new JTextField(10);
jtf4=new JTextField(10);
jtf5=new JTextField(10);
jtf6=new JTextField(10);
System.out.println("打印"+tm.getValueAt(rowNum, 0));
jtf1.setText((String)tm.getValueAt(rowNum,0));
jtf1.setEditable(false);
jtf2.setText((String)tm.getValueAt(rowNum,1));
jtf3.setText((String)tm.getValueAt(rowNum,2));
jtf4.setText(tm.getValueAt(rowNum,3).toString());
jtf5.setText((String)tm.getValueAt(rowNum,4));
jtf6.setText((String)tm.getValueAt(rowNum,5));
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jp1.setLayout(new GridLayout(6,1));
jp1.add(jl1);
jp1.add(jl2);
jp1.add(jl3);
jp1.add(jl4);
jp1.add(jl5);
jp1.add(jl6);
this.add(jp1, BorderLayout.WEST);
jp2.setLayout(new GridLayout(6,1));
jp2.add(jtf1);
jp2.add(jtf2);
jp2.add(jtf3);
jp2.add(jtf4);
jp2.add(jtf5);
jp2.add(jtf6);
this.add(jp2);
jb1=new JButton("确认修改");
jb2=new JButton("取消");
jb1.addActionListener(this);
jb2.addActionListener(this);
jp3.add(jb1);
jp3.add(jb2);
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){
String sql="update stu set stuName=?,stuSex=?,stuAge=?,stuJg=?,stuDept=? where stuId=?";
TableModel tm=new TableModel();
String [] param={jtf2.getText(),jtf3.getText(),jtf4.getText(),jtf5.getText(),jtf6.getText(),jtf1.getText()};
tm.doSomething(sql, param);
this.dispose();
}
if(e.getSource()==jb2){
this.dispose();
}
}
}
在sqlsever数据库中插入表,如下图所示,
把这四个java文件放在同一个包下,即可以运行了。。。。当然别玩了在数据库master下插入表数据。。。。。。。。。一个简单的学生管理系统就大功告成。