package com.test3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.sql.*;
public class StuManager extends JFrame implements ActionListener{
JPanel jp1,jp2;
JButton jb1,jb2,jb3,jb4;
JLabel jl;
JTextField jtf;
JTable jt;
JScrollPane jsp;
Vector rowData,columnNames;
Connection ct=null;
PreparedStatement ps=null;
ResultSet rs=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
StuManager stu1=new StuManager();
}
public StuManager()
{
jp1=new JPanel();
jl=new JLabel("用户名");
jtf=new JTextField(10);
jb1=new JButton("查询");
jb1.addActionListener(this);
jp1.add(jl);
jp1.add(jtf);
jp1.add(jb1);
jb2=new JButton("添加");
jb3=new JButton("修改");
jb4=new JButton("删除");
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
columnNames =new Vector();
columnNames.add("学号");
columnNames.add("姓名");
columnNames.add("性别");
columnNames.add("年龄");
columnNames.add("籍贯");
columnNames.add("系别");
rowData=new Vector();
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
ct=DriverManager.getConnection("jdbc:microsofe:sqlserver://localhost:1433;databaseName=aa","sa","910526");
ps=ct.prepareStatement("select * from stu");
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));
rowData.add(hang);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
if(rs!=null)rs.close();
if(ps!=null)rs.close();
if(ct!=null)ct.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
jt=new JTable(rowData,columnNames);
jsp=new JScrollPane(jt);
this.add(jp1,BorderLayout.NORTH);
this.add(jsp);
this.add(jp2,BorderLayout.SOUTH);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true); |
|