代码长度超过系统设置了。。
帮同学做的课设~
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
import javax.swing.*;
import java.io.IOException;
import java.sql.*;
class Add
{
private JFrame jf=new JFrame("添加");
private JFrame jj=new JFrame("请添加");
private JLabel id=new JLabel("学号");
private JLabel name=new JLabel("姓名");
private JLabel sex=new JLabel("性别:");
private JLabel birth=new JLabel("出生日期");
private JLabel a=new JLabel("年");
private JLabel b=new JLabel("月");
private JLabel c=new JLabel("日");
private JLabel addr=new JLabel("户口所在地");
private JLabel card=new JLabel("身份证号");
private JLabel dept=new JLabel("学院");
private JLabel pro=new JLabel("专业");
private JLabel clas=new JLabel("班级");
private JRadioButton boy=new JRadioButton("男",false);
private JRadioButton girl=new JRadioButton("女",false);
private ButtonGroup group=new ButtonGroup();
private JTextField tf1=new JTextField(8);
private JTextField tf2=new JTextField(8);
private JTextField tf41=new JTextField(8);
private JComboBox<Integer> jc1=new JComboBox<>();
private JComboBox<Integer> jc2=new JComboBox<>();
private JComboBox<Integer> jc3=new JComboBox<>();
private JTextField tf5=new JTextField(8);
private JTextField tf6=new JTextField(8);
private JTextField tf7=new JTextField(8);
private JTextField tf8=new JTextField(8);
private JTextField tf9=new JTextField(8);
private JButton ok=new JButton("提交");
private JButton clear=new JButton("重置");
private Box y=Box.createVerticalBox();
private Box x1=Box.createHorizontalBox();
private Box x2=Box.createHorizontalBox();
private Box x3=Box.createHorizontalBox();
private Box x4=Box.createHorizontalBox();
private Box x5=Box.createHorizontalBox();
private Box x6=Box.createHorizontalBox();
private Box x7=Box.createHorizontalBox();
private Box x8=Box.createHorizontalBox();
private Box x9=Box.createHorizontalBox();
private Box x10=Box.createHorizontalBox();
private JScrollPane jsp;
private ResultSet rs;
private Connection conn;
private Statement stmt;
void init()
{
for(int i=1949;i<=2020;i++)
{
jc3.addItem(i);
}
for(int i=1;i<=12;i++)
{
jc1.addItem(i);
}
for(int i=1;i<=31;i++)
{
jc2.addItem(i);
}
id.setForeground(Color.white);
name.setForeground(Color.white);
sex.setForeground(Color.white);
addr.setForeground(Color.white);
card.setForeground(Color.white);
dept.setForeground(Color.white);
pro.setForeground(Color.white);
clas.setForeground(Color.white);
birth.setForeground(Color.white);
a.setForeground(Color.white);
b.setForeground(Color.white);
c.setForeground(Color.white);
group.add(boy);
group.add(girl);
x1.add(id);
x1.add(tf1);
x1.add(Box.createHorizontalStrut(188));
x2.add(name);
x2.add(tf2);
x2.add(Box.createHorizontalStrut(188));
x3.add(sex);
x3.add(Box.createHorizontalStrut(28));
x3.add(boy);
x3.add(girl);
x3.add(Box.createHorizontalStrut(286));
x4.add(birth);
x4.add(jc3);
x4.add(a);
x4.add(jc1);
x4.add(b);
x4.add(jc2);
x4.add(c);
x4.add(Box.createHorizontalStrut(88));
x5.add(addr);
x5.add(tf5);
x5.add(Box.createHorizontalStrut(28));
x6.add(card);
x6.add(tf6);
x6.add(Box.createHorizontalStrut(28));
x7.add(dept);
x7.add(tf7);
x7.add(Box.createHorizontalStrut(28));
x8.add(pro);
x8.add(tf8);
x8.add(Box.createHorizontalStrut(28));
x9.add(clas);
x9.add(tf9);
x9.add(Box.createHorizontalStrut(28));
x10.add(clear);
x10.add(ok);
y.add(Box.createVerticalStrut(6));
y.add(x1);
y.add(Box.createVerticalStrut(12));
y.add(x2);
y.add(Box.createVerticalStrut(12));
y.add(x3);
y.add(Box.createVerticalStrut(12));
y.add(x4);
y.add(Box.createVerticalStrut(12));
y.add(x5);
y.add(Box.createVerticalStrut(12));
y.add(x6);
y.add(Box.createVerticalStrut(12));
y.add(x7);
y.add(Box.createVerticalStrut(12));
y.add(x8);
y.add(Box.createVerticalStrut(12));
y.add(x9);
y.add(Box.createVerticalStrut(6));
y.add(x10);
jf.add(y);
jf.pack();
jf.setBounds(616,266,340,399);
jf.setVisible(true);
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tf1.setText(null);
tf2.setText(null);
tf41.setText(null);
jc1.setSelectedItem(1);
jc2.setSelectedItem(1);
tf5.setText(null);
tf6.setText(null);
tf7.setText(null);
tf8.setText(null);
tf9.setText(null);
}
});
ok.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String query1=tf1.getText();
String query2=tf2.getText();
String query3=boy.isSelected()?"男":"女";
String query4=jc3.getSelectedItem()+"-"+jc1.getSelectedItem()+"-"+jc2.getSelectedItem();
String query5=tf5.getText();
String query6=tf6.getText();
String query7=tf7.getText();
String query8=tf8.getText();
String query9=tf9.getText();
try
{
if(jsp!=null)
jj.remove(jsp);
if(rs!=null)
rs.close();
String query="insert into info values"+
"(null,'"+query1+"','"+query2+"','"+query3+"','"+query4+"','"+query5+"',"+
"'"+query6+"','"+query7+"','"+query8+"','"+query9+"')";
try
{
conn=JDBC.getConnection();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
// stmt.executeUpdate(query);
String query0="select * from info where 学号='"+query1+"'";
rs=stmt.executeQuery(query0);
if(rs.next())
JOptionPane.showMessageDialog(jj, "添加失败!");
else
{
JOptionPane.showMessageDialog(jf, "添加成功");
jf.setVisible(false);
}
stmt.executeUpdate(query);
}
catch (SQLException e1)
{
JOptionPane.showMessageDialog(jj, "资料不能为空!");
e1.printStackTrace();
}
}
});
}
void initFrame()
{
JPanel GImage = new JPanel()
{
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g)
{
ImageIcon icon = new ImageIcon("D:\\pic.jpg");
Image img = icon.getImage();
g.drawImage(img, 0, 0, icon.getIconWidth(),
icon.getIconHeight(), icon.getImageObserver());
jf.setSize(340, 409);
}
};
jf.add(GImage);
jf.pack();
jf.setVisible(true);
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.TextField;
import java.awt.event.*;
import javax.swing.*;
import java.io.IOException;
import java.sql.*;
class Delete
{
private JFrame jf=new JFrame("删除");
private JFrame jj=new JFrame("请删除");
private JLabel id=new JLabel("学号");
private JLabel name=new JLabel("姓名");
private JButton ok1=new JButton("确定");
private JButton ok2=new JButton("确定");
private JButton clear=new JButton("重置");
private TextField tf1=new TextField(10);
private TextField tf2=new TextField(10);
private Box y=Box.createVerticalBox();
private Box x1=Box.createHorizontalBox();
private Box x2=Box.createHorizontalBox();
private Box x3=Box.createHorizontalBox();
private JScrollPane jsp;
private ReadOnlyTableModel romodel;
private ResultSet rs;
private Connection conn;
private Statement stmt;
void init()
{
id.setForeground(Color.WHITE);
name.setForeground(Color.WHITE);
x1.add(id);
x1.add(tf1);
x1.add(Box.createHorizontalStrut(6));
x1.add(ok1);
x2.add(name);
x2.add(tf2);
x2.add(Box.createHorizontalStrut(6));
x2.add(ok2);
x3.add(clear);
y.add(Box.createVerticalStrut(36));
y.add(x1);
y.add(Box.createVerticalStrut(16));
y.add(x2);
y.add(Box.createVerticalStrut(16));
y.add(x3);
y.add(Box.createVerticalStrut(16));
jf.add(y);
jf.pack();
jf.setBounds(616, 288, 240, 200);
jf.setVisible(true);
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tf1.setText(null);
tf2.setText(null);
}
});
ok1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String query1=tf1.getText();
try
{
if(jsp!=null)
jj.remove(jsp);
if(rs!=null)
rs.close();
String query="select * from info where 学号 ='"+query1+"'";
try
{
conn=JDBC.getConnection();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(query);
if(!rs.next())
JOptionPane.showMessageDialog(jj, "查无此人!");
else
{ romodel = new ReadOnlyTableModel(rs);
SuperTable table = new SuperTable(romodel);
Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"};
table.changeSize(table,columnTitle);
jsp = new JScrollPane(table);
jj.add(jsp);
jj.validate();
jj.pack();
jj.setSize(1366, 580);
jj.setVisible(true);
jf.setVisible(false);
int as=JOptionPane.showConfirmDialog(jj, "确定删除?", "删除", 0);
if(as==0)
{
jj.setVisible(false);
try
{
conn=JDBC.getConnection();
}
catch (ClassNotFoundException | SQLException
| IOException e1)
{
e1.printStackTrace();
}
String query3=tf1.getText();
String query4="delete from info where 学号='"+query3+"'";
try
{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(query4);
}
catch (SQLException e1)
{
e1.printStackTrace();
}
JOptionPane.showMessageDialog(jj,"删除成功");
}
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
});
ok2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String query2=tf2.getText();
try
{
if(jsp!=null)
jj.remove(jsp);
if(rs!=null)
rs.close();
String query="select * from info where 姓名='"+query2+"'";
try
{
conn=JDBC.getConnection();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(query);
if(!rs.next())
JOptionPane.showMessageDialog(jj, "查无此人!");
else
{
romodel = new ReadOnlyTableModel(rs);
SuperTable table = new SuperTable(romodel);
Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"};
table.changeSize(table,columnTitle);
/* TableColumn num = table.getColumn(columnTitle[0]);
num.setMaxWidth(30);
TableColumn id = table.getColumn(columnTitle[1]);
id. setMaxWidth(188);
TableColumn name= table.getColumn(columnTitle[2]);
name.setMaxWidth(66);
TableColumn sex = table.getColumn(columnTitle[3]);
sex.setMaxWidth(50);
TableColumn bith= table.getColumn(columnTitle[4]);
bith.setMaxWidth(80);
TableColumn addr= table.getColumn(columnTitle[5]);
addr.setPreferredWidth(120);
TableColumn dept= table.getColumn(columnTitle[7]);
dept.setPreferredWidth(48);
TableColumn clas= table.getColumn(columnTitle[9]);
clas.setMaxWidth(60);*/
jsp = new JScrollPane(table);
jj.add(jsp);
jj.validate();
jj.pack();
jj.setSize(1366, 168);
jj.setVisible(true);
jf.setVisible(false);
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}
int as=JOptionPane.showConfirmDialog(jf, "确认删除?");
if(as==0)
{
jj.setVisible(false);
try
{
conn=JDBC.getConnection();
}
catch (ClassNotFoundException | SQLException
| IOException e1)
{
e1.printStackTrace();
}
String query3=tf2.getText();
String query="delete from info where 姓名='"+query3+"'";
try
{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(query);
}
catch (SQLException e1)
{
e1.printStackTrace();
}
JOptionPane.showMessageDialog(jj,"删除成功");
}
}
});
}
void initFrame()
{
JPanel GImage = new JPanel()
{
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g)
{
ImageIcon icon = new ImageIcon("D:\\frame.jpg");
Image img = icon.getImage();
g.drawImage(img, 0, 0, icon.getIconWidth(),
icon.getIconHeight(), icon.getImageObserver());
jf.setSize(240, 240);
}
};
jf.add(GImage);
jf.pack();
jf.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.IOException;
import java.sql.*;
class Update
{
private JFrame jf=new JFrame("修改");
private JFrame jj=new JFrame("请修改");
private JLabel id=new JLabel("学号");
private JLabel name=new JLabel("姓名");
private JTextField tf1=new JTextField(18);
private JTextField tf2=new JTextField(18);
private JButton ok1=new JButton("确定");
private JButton ok2=new JButton("确定");
private JButton clear=new JButton("重置");
private Box y=Box.createVerticalBox();
private Box x1=Box.createHorizontalBox();
private Box x2=Box.createHorizontalBox();
private Box x3=Box.createHorizontalBox();
private JScrollPane jsp;
private ResultSetTableModel model;
private ResultSet rs;
private Connection conn;
private Statement stmt;
void init()
{
id.setForeground(Color.WHITE);
name.setForeground(Color.WHITE);
x1.add(id);
x1.add(tf1);
x1.add(Box.createHorizontalStrut(6));
x1.add(ok1);
x2.add(name);
x2.add(tf2);
x2.add(Box.createHorizontalStrut(6));
x2.add(ok2);
x3.add(clear);
y.add(Box.createVerticalStrut(36));
y.add(x1);
y.add(Box.createVerticalStrut(16));
y.add(x2);
y.add(Box.createVerticalStrut(16));
y.add(x3);
y.add(Box.createVerticalStrut(16));
jf.add(y);
jf.pack();
jf.setBounds(616, 288, 240, 200);
jf.setVisible(true);
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tf1.setText(null);
tf2.setText(null);
}
});
ok1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String query1=tf1.getText();
try
{
if(jsp!=null)
jj.remove(jsp);
if(rs!=null)
rs.close();
String query="select * from info where 学号 like '"+query1+"%'";
try
{
conn=JDBC.getConnection();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(query);
if(!rs.next())
JOptionPane.showMessageDialog(jj, "查无此人!");
else
{ model = new ResultSetTableModel(rs);
SuperTable table = new SuperTable(model);
Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"};
table.changeSize(table, columnTitle);
jsp = new JScrollPane(table);
jj.add(jsp);
jj.validate();
jj.pack();
jj.setSize(1366, 580);
jj.setVisible(true);
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
});
ok2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String query2=tf2.getText();
try
{
if(jsp!=null)
jj.remove(jsp);
if(rs!=null)
rs.close();
String query="select * from info where 姓名='"+query2+"'";
try
{
conn=JDBC.getConnection();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(query);
if(!rs.next())
JOptionPane.showMessageDialog(jj, "查无此人!");
else
{
model = new ResultSetTableModel(rs);
SuperTable table = new SuperTable(model);
Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"};
table.changeSize(table, columnTitle);
jsp = new JScrollPane(table);
jj.add(jsp);
jj.validate();
jj.pack();
jj.setSize(1366, 168);
jj.setVisible(true);
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
});
}
void initFrame()
{
JPanel GImage = new JPanel()
{
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g)
{
ImageIcon icon = new ImageIcon("D:\\frame.jpg");
Image img = icon.getImage();
g.drawImage(img, 0, 0, icon.getIconWidth(),
icon.getIconHeight(), icon.getImageObserver());
jf.setSize(240, 240);
}
};
jf.add(GImage);
jf.pack();
jf.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.IOException;
import java.sql.*;
class Query
{
private JFrame jf=new JFrame("查询");
private JFrame jj=new JFrame("查询结果");
private JLabel id=new JLabel("学号");
private JLabel name=new JLabel("姓名");
private JLabel addr=new JLabel("户籍");
private JButton ok1=new JButton("确定");
private JButton ok2=new JButton("确定");
private JButton ok3=new JButton("确定");
private JButton clear=new JButton("重置");
private TextField tf1=new TextField(10);
private TextField tf2=new TextField(10);
private TextField tf3=new TextField(10);
private Box y=Box.createVerticalBox();
private Box x1=Box.createHorizontalBox();
private Box x2=Box.createHorizontalBox();
private Box x3=Box.createHorizontalBox();
private Box x4=Box.createHorizontalBox();
private JScrollPane jsp;
private ReadOnlyTableModel romodel;
private ResultSet rs;
private Connection conn;
private Statement stmt;
void init()
{
id. setForeground(Color.WHITE);
name.setForeground(Color.WHITE);
addr.setForeground(Color.white);
x1.add(id);
x1.add(tf1);
x1.add(Box.createHorizontalStrut(10));
x1.add(ok1);
x2.add(name);
x2.add(tf2);
x2.add(Box.createHorizontalStrut(10));
x2.add(ok2);
x3.add(addr);
x3.add(tf3);
x3.add(Box.createHorizontalStrut(10));
x3.add(ok3);
x4.add(clear);
y.add(Box.createVerticalStrut(16));
y.add(x1);
y.add(Box.createVerticalStrut(16));
y.add(x2);
y.add(Box.createVerticalStrut(16));
y.add(x3);
y.add(Box.createVerticalStrut(16));
y.add(x4);
jf.add(y);
jf.pack();
jf.setBounds(616, 288, 240, 216);
jf.setVisible(true);
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tf1.setText(null);
tf2.setText(null);
tf3.setText(null);
}
});
ok1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String query1=tf1.getText();
try
{
if(jsp!=null)
jj.remove(jsp);
if(rs!=null)
rs.close();
String query="select * from info where 学号 like '"+query1+"%'";
try
{
conn=JDBC.getConnection();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(query);
if(!rs.next())
JOptionPane.showMessageDialog(jj, "查无此人!");
else
{ romodel = new ReadOnlyTableModel(rs);
SuperTable table = new SuperTable(romodel);
Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"};
table.changeSize(table,columnTitle);
jsp = new JScrollPane(table);
jj.add(jsp);
jj.validate();
jj.pack();
jj.setSize(1366, 580);
jj.setVisible(true);
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
});
ok2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String query2=tf2.getText();
try
{
if(jsp!=null)
jj.remove(jsp);
if(rs!=null)
rs.close();
String query="select * from info where 姓名='"+query2+"'";
try
{
conn=JDBC.getConnection();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(query);
if(!rs.next())
JOptionPane.showMessageDialog(jj, "查无此人!");
else
{
romodel = new ReadOnlyTableModel(rs);
SuperTable table = new SuperTable(romodel);
Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"};
table.changeSize(table,columnTitle);
jsp = new JScrollPane(table);
jj.add(jsp);
jj.validate();
jj.pack();
jj.setSize(1366, 168);
jj.setVisible(true);
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
});
ok3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String query3=tf3.getText();
try
{
if(jsp!=null)
jj.remove(jsp);
if(rs!=null)
rs.close();
String query="select * from info where 户口所在地 like '%"+query3+"%'";
try
{
conn=JDBC.getConnection();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(query);
if(!rs.next())
JOptionPane.showMessageDialog(jj, "查无此人!");
else
{
romodel = new ReadOnlyTableModel(rs);
SuperTable table = new SuperTable(romodel);
Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"};
table.changeSize(table,columnTitle);
jsp = new JScrollPane(table);
jj.add(jsp);
jj.validate();
jj.pack();
jj.setSize(1366, 580);
jj.setVisible(true);
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
});
}
void initFrame()
{
JPanel GImage = new JPanel()
{
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g)
{
ImageIcon icon = new ImageIcon("D:\\frame.jpg");
Image img = icon.getImage();
g.drawImage(img, 0, 0, icon.getIconWidth(),
icon.getIconHeight(), icon.getImageObserver());
jf.setSize(240, 240);
}
};
jf.add(GImage);
jf.pack();
jf.setVisible(true);
}
}
import java.sql.*;
import java.io.*;
import java.util.*;
class JDBC
{
static Connection getConnection()
throws SQLException, IOException , ClassNotFoundException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream("id.ini");
props.load(in);
in.close();
String driver=props.getProperty("driver");
String url = props.getProperty("url");
String user = props.getProperty("user");
String pass = props.getProperty("pass");
// 加载数据库驱动
Class.forName(driver);
// 取得数据库连接
return DriverManager.getConnection(url, user, pass);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
class Login
{
private JLabel num=new JLabel("账户");
private JLabel pas=new JLabel("密码");
private JTextField id=new JTextField(16);
private JPasswordField password=new JPasswordField(16);
private JButton login=new JButton("登陆");
private JButton register=new JButton("注册");
private JFrame jf=new JFrame("登陆");
private Box y=Box.createVerticalBox();
private Box x1=Box.createHorizontalBox();
private Box x2=Box.createHorizontalBox();
private Box x3=Box.createHorizontalBox();
void init() throws ClassNotFoundException
{
num.setForeground(Color.WHITE);
pas.setForeground(Color.WHITE);
x1.add(Box.createHorizontalStrut(10));
x1.add(num);
x1.add(id);
x2.add(Box.createHorizontalStrut(10));
x2.add(pas);
x2.add(password);
x3.add(Box.createHorizontalStrut(28));
x3.add(login);
x3.add(register);
y.add(Box.createVerticalStrut(28));
y.add(x1);
y.add(Box.createVerticalStrut(16));
y.add(x2);
y.add(Box.createVerticalStrut(26));
y.add(x3);
y.add(Box.createVerticalStrut(26));
jf.add(y);
jf.pack();
jf.setBounds(616, 288, 216, 200);
jf.setVisible(true);
register.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Register rg=new Register();
try
{
rg.register();
}
catch (Exception e1)
{
e1.printStackTrace();
}
rg.initFrame();
}
});
login.addActionListener(new ActionListener()
{
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{
if(validate(id.getText(),password.getText()))
{
Subject sb=new Subject();
sb.init();
jf.setVisible(false);
}
else
{
JOptionPane.showMessageDialog(jf,"登录失败");
}
}
});
}
void initFrame()
{
JPanel GImage = new JPanel()
{
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g)
{
ImageIcon icon = new ImageIcon("D:\\123.jpg");
Image img = icon.getImage();
g.drawImage(img, 0, 0, icon.getIconWidth(),
icon.getIconHeight(), icon.getImageObserver());
jf.setSize(240, 240);
}
};
jf.add(GImage);
jf.pack();
jf.setVisible(true);
}
private boolean validate(String a, String b)
{
try(
Connection conn = JDBC.getConnection();
PreparedStatement pstmt = conn.prepareStatement(
"select * from account where id=? and pass=?"))
{
pstmt.setString(1, a);
pstmt.setString(2, b);
try(
ResultSet rs = pstmt.executeQuery())
{
//如果查询的ResultSet里有超过一条的记录,则登录成功
if (rs.next())
{
return true;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return false;
}
public static void main (String[] args) throws Exception
{
Login lg=new Login();
lg.init();
lg.initFrame();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
class Register
{
private JLabel num=new JLabel("学号");
private JLabel name1=new JLabel("姓名");
private JLabel pas=new JLabel("密码");
private JLabel pass1=new JLabel("确认密码");
private JButton submite=new JButton("提交");
private JButton clear=new JButton("重置");
private JTextField id=new JTextField(16);
private JTextField name=new JTextField(16);
private JPasswordField pass2=new JPasswordField(16);
private JPasswordField password=new JPasswordField(14);
private JFrame jf=new JFrame("注册");
private Box y=Box.createVerticalBox();
private Box x1=Box.createHorizontalBox();
private Box x2=Box.createHorizontalBox();
private Box x3=Box.createHorizontalBox();
private Box x4=Box.createHorizontalBox();
private Box x5=Box.createHorizontalBox();
void register() throws Exception
{
x1.add(num);
x1.add(id);
x2.add(name1);
x2.add(name);
x3.add(pas);
x3.add(pass2);
x4.add(pass1);
x4.add(password);
x5.add(submite);
x5.add(clear);
y.add(Box.createVerticalStrut(16));
y.add(x1);
y.add(Box.createVerticalStrut(16));
y.add(x2);
y.add(Box.createVerticalStrut(16));
y.add(x3);
y.add(Box.createVerticalStrut(16));
y.add(x4);
y.add(Box.createVerticalStrut(16));
y.add(x5);
y.add(Box.createVerticalStrut(16));
jf.add(y);
jf.pack();
jf.setVisible(true);
clear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
id.setText("");
name.setText("");
pass2.setText("");
password.setText("");
}
});
submite.addActionListener(new ActionListener()
{
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{
if(exist(id.getText()))
JOptionPane.showMessageDialog(jf,"您已注册,请直接登录");
else
if(pass2.getText().equals(password.getText()))
{
try
{
insertinto(id.getText(),name.getText(),pass2.getText());
JOptionPane.showMessageDialog(jf, "恭喜您!注册成功!");
jf.setVisible(false);
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
else
{
JOptionPane.showMessageDialog(jf, "两次密码不一致,请重新输入");
pass2.setText("");
password.setText("");
}
}
});
}
private void insertinto(String id,String name,String pass2) throws Exception
{
try(
Connection conn = JDBC.getConnection();
PreparedStatement pstmt = conn.prepareStatement(
"insert into account values(?,?,?)"))
{
pstmt.setString(1, id);
pstmt.setString(2, name);
pstmt.setString(3, pass2);
pstmt.executeUpdate();
}
}
private boolean exist(String c)
{
try(
Connection conn =JDBC.getConnection();
PreparedStatement pstmt = conn.prepareStatement(
"select * from account where id=? "))
{
pstmt.setString(1, c);
try(
ResultSet rs = pstmt.executeQuery())
{
if (rs.next())
{
return true;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return false;
}
void initFrame()
{
JPanel GImage = new JPanel()
{
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g)
{
ImageIcon icon = new ImageIcon("D:\\frame.jpg");
Image img = icon.getImage();
g.drawImage(img, 0, 0, icon.getIconWidth(),
icon.getIconHeight(), icon.getImageObserver());
jf.setSize(240, 240);
}
};
jf.add(GImage);
jf.pack();
jf.setVisible(true);
}
}
import javax.swing.table.*;
import java.sql.*;
class ReadOnlyTableModel extends AbstractTableModel
{
private static final long serialVersionUID = 1L;
private ResultSet rs;
private ResultSetMetaData rsmd;
public ReadOnlyTableModel(ResultSet aResultSet)
{
rs = aResultSet;
try
{
rsmd = rs.getMetaData();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
// 重写getColumnName方法,用于为该TableModel设置列名
public String getColumnName(int c)
{
try
{
return rsmd.getColumnName(c + 1);
}
catch (SQLException e)
{
e.printStackTrace();
return "";
}
}
// 重写getColumnCount方法,用于设置该TableModel的列数
public int getColumnCount()
{
try
{
return rsmd.getColumnCount();
}
catch (SQLException e)
{
e.printStackTrace();
return 0;
}
}
// 重写getValueAt方法,用于设置该TableModel指定单元格的值
public Object getValueAt(int r, int c)
{
try
{
rs.absolute(r + 1);
return rs.getObject(c + 1);
}
catch(SQLException e)
{
e.printStackTrace();
return null;
}
}
// 重写getColumnCount方法,用于设置该TableModel的行数
public int getRowCount()
{
try
{
rs.last();
return rs.getRow();
}
catch(SQLException e)
{
e.printStackTrace();
return 0;
}
}
}
import javax.swing.table.*;
import java.sql.*;
class ResultSetTableModel extends AbstractTableModel
{
private static final long serialVersionUID = 1L;
private ResultSet rs;
private ResultSetMetaData rsmd;
public ResultSetTableModel(ResultSet aResultSet)
{
rs = aResultSet;
try
{
rsmd = rs.getMetaData();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
// 重写getColumnName方法,用于为该TableModel设置列名
public String getColumnName(int c)
{
try
{
return rsmd.getColumnName(c + 1);
}
catch (SQLException e)
{
e.printStackTrace();
return "";
}
}
// 重写getColumnCount方法,用于设置该TableModel的列数
public int getColumnCount()
{
try
{
return rsmd.getColumnCount();
}
catch (SQLException e)
{
e.printStackTrace();
return 0;
}
}
// 重写getValueAt方法,用于设置该TableModel指定单元格的值
public Object getValueAt(int r, int c)
{
try
{
rs.absolute(r + 1);
return rs.getObject(c + 1);
}
catch(SQLException e)
{
e.printStackTrace();
return null;
}
}
// 重写getColumnCount方法,用于设置该TableModel的行数
public int getRowCount()
{
try
{
rs.last();
return rs.getRow();
}
catch(SQLException e)
{
e.printStackTrace();
return 0;
}
}
// 重写isCellEditable返回true,让每个单元格可编辑
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return true;
}
// 重写setValueAt()方法,当用户编辑单元格时,将会触发该方法
public void setValueAt(Object aValue , int row,int column)
{
try
{
// 结果集定位到对应的行数
rs.absolute(row + 1);
// 修改单元格多对应的值
rs.updateObject(column + 1 , aValue);
// 提交修改
rs.updateRow();
// 触发单元格的修改事件
fireTableCellUpdated(row, column);
}
catch (SQLException evt)
{
evt.printStackTrace();
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.TableColumn;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import java.io.*;
import java.sql.*;
public class Subject
{
private JFrame jf=new JFrame("学生信息管理系统");
private JPanel jp=new JPanel();
private JButton refresh=new JButton("刷新");
private JButton query=new JButton("查询");
private JButton add=new JButton("添加");
private JButton update=new JButton("修改");
private JButton delete=new JButton("删除");
private JTree tree;
private DefaultMutableTreeNode root=new DefaultMutableTreeNode("学生信息管理系统");
private DefaultMutableTreeNode info=new DefaultMutableTreeNode("学生户籍资料");
private DefaultMutableTreeNode d01=new DefaultMutableTreeNode("机械工程学院");
private DefaultMutableTreeNode d02=new DefaultMutableTreeNode("汽车与交通学院");
private DefaultMutableTreeNode d03=new DefaultMutableTreeNode("信息科学与工程学院");
private DefaultMutableTreeNode d04=new DefaultMutableTreeNode("经济管理学院");
private DefaultMutableTreeNode d05=new DefaultMutableTreeNode("材料科学与工程学院");
private DefaultMutableTreeNode d06=new DefaultMutableTreeNode("外国语学院");
private DefaultMutableTreeNode d07=new DefaultMutableTreeNode("理学院");
private DefaultMutableTreeNode d08=new DefaultMutableTreeNode("环境与化学工程学院");
private DefaultMutableTreeNode d09=new DefaultMutableTreeNode("艺术设计学院");
private DefaultMutableTreeNode d10=new DefaultMutableTreeNode("装备工程学院");
private DefaultMutableTreeNode d03p01c1=new DefaultMutableTreeNode("一班");
private DefaultMutableTreeNode d03p01c2=new DefaultMutableTreeNode("二班");
private DefaultMutableTreeNode d03p01c3=new DefaultMutableTreeNode("三班");
private DefaultMutableTreeNode d03p01c4=new DefaultMutableTreeNode("四班");
private DefaultMutableTreeNode d03p01c5=new DefaultMutableTreeNode("五班");
private DefaultMutableTreeNode d03p02c1=new DefaultMutableTreeNode("一班");
private DefaultMutableTreeNode d03p02c2=new DefaultMutableTreeNode("二班");
private DefaultMutableTreeNode d03p02c3=new DefaultMutableTreeNode("三班");
private DefaultMutableTreeNode d03p03c1=new DefaultMutableTreeNode("一班");
private DefaultMutableTreeNode d03p03c2=new DefaultMutableTreeNode("二班");
private DefaultMutableTreeNode d03p03c3=new DefaultMutableTreeNode("三班");
private DefaultMutableTreeNode d03p04c1=new DefaultMutableTreeNode("一班");
private DefaultMutableTreeNode d03p04c2=new DefaultMutableTreeNode("二班");
private DefaultMutableTreeNode d03p04c3=new DefaultMutableTreeNode("三班");
private DefaultMutableTreeNode d03p04c4=new DefaultMutableTreeNode("四班");
private DefaultMutableTreeNode d03p04c5=new DefaultMutableTreeNode("五班");
private DefaultMutableTreeNode d03p05c1=new DefaultMutableTreeNode("一班");
private DefaultMutableTreeNode d03p05c2=new DefaultMutableTreeNode("二班");
private DefaultMutableTreeNode d03p05c3=new DefaultMutableTreeNode("三班");
private DefaultMutableTreeNode d03p05c4=new DefaultMutableTreeNode("四班");
private DefaultMutableTreeNode d03p05c5=new DefaultMutableTreeNode("五班");
private DefaultMutableTreeNode d03p06c1=new DefaultMutableTreeNode("一班");
private DefaultMutableTreeNode d03p06c2=new DefaultMutableTreeNode("二班");
private DefaultMutableTreeNode d01p01=new DefaultMutableTreeNode("机械设计制造及其自动化");
private DefaultMutableTreeNode d01p02=new DefaultMutableTreeNode("工业工程");
private DefaultMutableTreeNode d01p03=new DefaultMutableTreeNode("机械电子工程");
private DefaultMutableTreeNode d02p01=new DefaultMutableTreeNode("车辆工程");
private DefaultMutableTreeNode d02p02=new DefaultMutableTreeNode("交通运输");
private DefaultMutableTreeNode d02p03=new DefaultMutableTreeNode("热能与动力工程");
private DefaultMutableTreeNode d03p01=new DefaultMutableTreeNode("自动化");
private DefaultMutableTreeNode d03p02=new DefaultMutableTreeNode("测控技术与仪器");
private DefaultMutableTreeNode d03p03=new DefaultMutableTreeNode("电子科学与技术");
private DefaultMutableTreeNode d03p04=new DefaultMutableTreeNode("通信工程");
private DefaultMutableTreeNode d03p05=new DefaultMutableTreeNode("计算机科学与技术");
private DefaultMutableTreeNode d03p06=new DefaultMutableTreeNode("电子信息工程");
private DefaultMutableTreeNode d04p01=new DefaultMutableTreeNode("工商管理");
private DefaultMutableTreeNode d04p02=new DefaultMutableTreeNode("会计学");
private DefaultMutableTreeNode d05p01=new DefaultMutableTreeNode("冶炼金属");
private DefaultMutableTreeNode d05p02=new DefaultMutableTreeNode("有机化学");
private DefaultMutableTreeNode d06p01=new DefaultMutableTreeNode("英语");
private DefaultMutableTreeNode d07p01=new DefaultMutableTreeNode("物理");
private DefaultMutableTreeNode d08p01=new DefaultMutableTreeNode("园艺");
private DefaultMutableTreeNode d09p01=new DefaultMutableTreeNode("陶瓷艺术");
private DefaultMutableTreeNode d10p01=new DefaultMutableTreeNode("导弹制导");
private DefaultMutableTreeNode grade=new DefaultMutableTreeNode("学生成绩管理");
private DefaultMutableTreeNode addgrade=new DefaultMutableTreeNode("添加");
private DefaultMutableTreeNode updategrade=new DefaultMutableTreeNode("修改");
private DefaultMutableTreeNode querygrade=new DefaultMutableTreeNode("查询");
private DefaultMutableTreeNode erasegrade=new DefaultMutableTreeNode("删除");
private JScrollPane jsp;
private ReadOnlyTableModel romodel;
private ResultSet rs;
private Connection conn;
private Statement stmt;
private Statement stmt1;
void init()
{
show();
jp.add(refresh);
jp.add(query);
jp.add(add);
jp.add(update);
jp.add(delete);
info.add(d01);
info.add(d02);
info.add(d03);
info.add(d04);
info.add(d05);
info.add(d06);
info.add(d07);
info.add(d08);
info.add(d09);
info.add(d10);
d01.add(d01p01);
d01.add(d01p02);
d01.add(d01p03);
d02.add(d02p01);
d02.add(d02p02);
d02.add(d02p03);
d03.add(d03p01);
d03.add(d03p02);
d03.add(d03p03);
d03.add(d03p04);
d03.add(d03p05);
d03.add(d03p06);
d04.add(d04p01);
d04.add(d04p02);
d05.add(d05p01);
d05.add(d05p02);
d06.add(d06p01);
d07.add(d07p01);
d08.add(d08p01);
d09.add(d09p01);
d10.add(d10p01);
d03p01.add(d03p01c1);
d03p01.add(d03p01c2);
d03p01.add(d03p01c3);
d03p01.add(d03p01c4);
d03p01.add(d03p01c5);
d03p02.add(d03p02c1);
d03p02.add(d03p02c2);
d03p02.add(d03p02c3);
d03p03.add(d03p03c1);
d03p03.add(d03p03c2);
d03p03.add(d03p03c3);
d03p04.add(d03p04c1);
d03p04.add(d03p04c2);
d03p04.add(d03p04c3);
d03p04.add(d03p04c4);
d03p04.add(d03p04c5);
d03p05.add(d03p05c1);
d03p05.add(d03p05c2);
d03p05.add(d03p05c3);
d03p05.add(d03p05c4);
d03p05.add(d03p05c5);
d03p06.add(d03p06c1);
d03p06.add(d03p06c2);
grade.add(addgrade);
grade.add(updategrade);
grade.add(querygrade);
grade.add(erasegrade);
root.add(info);
root.add(grade);
tree=new JTree(root);
jf.add(jp,BorderLayout.SOUTH);
jf.add(tree,BorderLayout.WEST);
jf.pack();
jf.setSize(1366, 688);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
refresh.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(jsp!=null)
jf.remove(jsp);
if(rs!=null)
try
{
rs.close();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
String search="select * from info";
try {
conn=JDBC.getConnection();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
try
{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(search);
}
catch (SQLException e1)
{
e1.printStackTrace();
}
romodel = new ReadOnlyTableModel(rs);
SuperTable table = new SuperTable(romodel);
Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"};
table.changeSize(table,columnTitle);
jsp = new JScrollPane(table);
jf.add(jsp);
jf.validate();
jf.pack();
jf.setSize(1366,688);
jf.setVisible(true);
}
});
query.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Query qu=new Query();
qu.init();
qu.initFrame();
}
});
update.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Update up=new Update();
up.init();
up.initFrame();
}
});
add.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Add ad=new Add();
ad.init();
ad.initFrame();
}
});
delete.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Delete de=new Delete();
de.init();
de.initFrame();
}
});
//设置只能选择一个路径
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.addTreeSelectionListener(new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent e)
{
DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
//信息院
if(selectedNode==d03)
try
{
if(jsp!=null)
jf.remove(jsp);
if(rs!=null)
rs.close();
String query="select * from info where 学院='信息科学与工程学院'";
try
{
conn=JDBC.getConnection();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(query);
romodel = new ReadOnlyTableModel(rs);
SuperTable table = new SuperTable(romodel);
Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"};
table.changeSize(table,columnTitle);
jsp = new JScrollPane(table);
jf.add(jsp);
jf.validate();
}
catch (SQLException e1)
{
e1.printStackTrace();
}
//计算机科学与技术
if(selectedNode==d03p05)
try
{
if(jsp!=null)
jf.remove(jsp);
if(rs!=null)
rs.close();
String query="select * from info where 学院='信息科学与工程学院' and 专业='计算机科学与技术'";
try
{
conn=JDBC.getConnection();
}
catch (ClassNotFoundException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
, ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(query);
romodel = new ReadOnlyTableModel(rs);
SuperTable table = new SuperTable(romodel);
Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"};
table.changeSize(table,columnTitle);
jsp = new JScrollPane(table); jf.add(jsp); jf.validate(); } catch (SQLException e1){e1.printStackTrace();} //通信工程 if(selectedNode==d03p04) try { if(jsp!=null) jf.remove(jsp); if(rs!=null) rs.close(); String query="select * from info where 学院='信息科学与工程学院'
and 专业='通信工程'"; try { conn=JDBC.getConnection(); } catch (ClassNotFoundException e1) {e1.printStackTrace(); } catch (IOException e1) {e1.printStackTrace(); } stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE , ResultSet.CONCUR_UPDATABLE); rs=stmt.executeQuery(query);
romodel = new ReadOnlyTableModel(rs);
SuperTable table = new SuperTable(romodel);
Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"};
table.changeSize(table,columnTitle);
jsp = new JScrollPane(table); jf.add(jsp); jf.validate(); } catch (SQLException e1){e1.printStackTrace();} //二班 if(selectedNode==d03p05c2) try { if(jsp!=null) jf.remove(jsp); if(rs!=null) rs.close(); String query="select * from info where
学院='信息科学与工程学院'and 专业='计算机科学与技术' and 班级='二班'"; try { conn=JDBC.getConnection(); } catch (ClassNotFoundException e1) {e1.printStackTrace(); } catch (IOException e1) {e1.printStackTrace(); } stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE , ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(query); romodel = new ReadOnlyTableModel(rs); SuperTable table = new SuperTable(romodel); Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"}; table.changeSize(table,columnTitle); jsp = new JScrollPane(table);
jf.add(jsp); jf.validate(); } catch (SQLException e1){e1.printStackTrace();} //三班 if(selectedNode==d03p05c3) try { if(jsp!=null) jf.remove(jsp); if(rs!=null) rs.close(); String query="select * from info where 学院='信息科学与工程学院' and 专业='计算机科学与技术' and 班级='三班'";
try { conn=JDBC.getConnection(); } catch (ClassNotFoundException e1) {e1.printStackTrace(); } catch (IOException e1) {e1.printStackTrace(); } stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE , ResultSet.CONCUR_UPDATABLE); rs=stmt.executeQuery(query);
romodel = new ReadOnlyTableModel(rs);SuperTable table = new SuperTable(romodel);
Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"};
table.changeSize(table,columnTitle); jsp = new JScrollPane(table); jf.add(jsp); jf.validate(); } catch (SQLException e1){e1.printStackTrace();}}});} void show(){try { if(jsp!=null) jf.remove(jsp); String query="select * from info "; try { conn=JDBC.getConnection(); } catch (ClassNotFoundException e1) {e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } stmt1 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE , ResultSet.CONCUR_UPDATABLE); rs=stmt1.executeQuery(query); romodel = new ReadOnlyTableModel(rs);
SuperTable table = new SuperTable(romodel);
Object[] columnTitle={"序号","学号","姓名","性别","出生日期","户口所在地","身份证号","学院","专业","班级"};
table.changeSize(table,columnTitle); jsp = new JScrollPane(table); jf.add(jsp); jf.validate(); jf.pack(); jf.setSize(1366, 688); jf.setVisible(true); } catch (SQLException e1){e1.printStackTrace();}}}import javax.swing.*;import javax.swing.table.TableColumn;class SuperTable extends JTable{private static final long serialVersionUID = 1L;public SuperTable(ReadOnlyTableModel romodel) {super(romodel);}public SuperTable(ResultSetTableModel model) {super(model);}void changeSize(SuperTable st,Object [] columnTitle){ TableColumn num = st.getColumn(columnTitle[0]); num.setMaxWidth(30); TableColumn id = st.getColumn(columnTitle[1]); id. setMaxWidth(188); TableColumn name= st.getColumn(columnTitle[2]); name.setMaxWidth(66); TableColumn sex = st.getColumn(columnTitle[3]); sex.setMaxWidth(50); TableColumn bith= st.getColumn(columnTitle[4]); bith.setMaxWidth(80); TableColumn addr= st.getColumn(columnTitle[5]); addr.setPreferredWidth(120); TableColumn dept= st.getColumn(columnTitle[7]); dept.setPreferredWidth(48); TableColumn clas= st.getColumn(columnTitle[9]); clas.setMaxWidth(60); }}