📄 stuserchframe.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.table.*;
class StuserchFrame extends JFrame implements ActionListener
{ String strSQL="adnid";
// String str="adnid";
JFrame f=new JFrame("学生信息选择查询 ");
JRadioButton rbtnid=new JRadioButton("学号 "),
rbtnname=new JRadioButton("姓名 "),
rbtnsex=new JRadioButton("性别 "),
rbtnbirth=new JRadioButton("生日 "),
rbtnmajor=new JRadioButton("专业 "),
rbtnphone=new JRadioButton(" 电话"),
rbtnjiguan=new JRadioButton(" 籍贯");
JLabel lbtj=new JLabel("请选择查询的条件 "),
lbshuru=new JLabel("请输入查询的内容 ");
JButton btnserch=new JButton("查询 ");
JButton btnexit=new JButton("退出 ");
ButtonGroup bgp=new ButtonGroup();
JPanel p=new JPanel(),
p1=new JPanel(),
p2=new JPanel();
JTextField tfshuru=new JTextField(25);
JScrollPane sp=new JScrollPane();
Vector tempvector=new Vector(1,1);
DefaultTableModel model=new DefaultTableModel();
JTable TableResult=new JTable(model);
public StuserchFrame()
{Container cp=f.getContentPane();
cp.setLayout(new BorderLayout());
p.setLayout(new GridLayout(2,1));
p1.add(lbtj);
p1.add(rbtnid);p1.add(rbtnname);
p1.add(rbtnsex);p1.add(rbtnbirth);p1.add(rbtnmajor);
p1.add(rbtnphone);p1.add(rbtnjiguan);
bgp.add(rbtnid);bgp.add(rbtnname);
bgp.add(rbtnsex);bgp.add(rbtnbirth);bgp.add(rbtnmajor);
bgp.add(rbtnphone);bgp.add(rbtnjiguan);
p2.add(lbshuru);p2.add(tfshuru);p2.add(btnserch);p2.add(btnexit);
p.add(p1);p.add(p2);
rbtnid.addActionListener(this);
rbtnname.addActionListener(this);
rbtnsex.addActionListener(this);
rbtnbirth.addActionListener(this);
rbtnmajor.addActionListener(this);
rbtnphone.addActionListener(this);
rbtnjiguan.addActionListener(this);
btnserch.addActionListener(this);
btnexit.addActionListener(this);
model.addColumn("学号") ;
model.addColumn("姓名") ;
model.addColumn("性别") ;
model.addColumn("生日") ;
model.addColumn("专业") ;
model.addColumn("电话") ;
model.addColumn("籍贯") ;
sp.setSize(50,50);
sp.getViewport().add(TableResult,null);
cp.add("North",p);
cp.add("South",sp);
f.setSize(650,540);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==rbtnid ) {strSQL="select * from stuinfor where id='"+tfshuru.getText().trim()+"'";}
if (e.getSource()==rbtnname ) {strSQL="select * from stuinfor where name='"+tfshuru.getText().trim()+"'";}
if (e.getSource()==rbtnsex ) {strSQL="select * from stuinfor where sex='"+tfshuru.getText().trim()+"'";}
if (e.getSource()==rbtnbirth ) { strSQL="select * from stuinfor where birth='"+tfshuru.getText().trim()+"'";}
if (e.getSource()==rbtnmajor ) {strSQL="select * from stuinfor where major='"+tfshuru.getText().trim()+"'";}
if (e.getSource()==rbtnphone ) {strSQL="select * from stuinfor where phone='"+tfshuru.getText().trim()+"'";}
if (e.getSource()==rbtnjiguan ) { strSQL="select * from stuinfor where jiguan='"+tfshuru.getText().trim()+"'";}
/*if(e.getSource()==rbtnid) {str="id";}
if(e.getSource()==rbtnname) {str="name";}
if(e.getSource()==rbtnsex) {str="sex";}
if(e.getSource()==rbtnbirth){ str="birth";}
if(e.getSource()==rbtnmajor) {str="major";}
if(e.getSource()==rbtnphone) {str="phone";}
if(e.getSource()==rbtnjiguan){ str="jiguan";}
*/
if(e.getSource()==btnexit) {f.setVisible(false);}
if(e.getSource()==btnserch)
{
for(int i=model.getRowCount()-1;i>=0;i--)
{ model.removeRow(i); }
try{
Connection con=DriverManager.getConnection("jdbc:odbc:Xsstudent");
Statement s=con.createStatement();
System.out.println(strSQL);
// System.out.println(str);
//String strSQL="select * from stuinfor where str='"+tfshuru.getText()+"'";
ResultSet rs=s.executeQuery(strSQL);
while(rs.next())
{
tempvector=new Vector(1,1);
tempvector.add(rs.getString("id"));
tempvector.add(rs.getString("name"));
tempvector.add(rs.getString("sex"));
tempvector.add(rs.getString("birth"));
tempvector.add(rs.getString("major"));
tempvector.add(rs.getString("phone"));
tempvector.add(rs.getString("jiguan"));
model.addRow(tempvector);
}
s.close();
con.close();
}
catch(Exception ee)
{System.out.println("error: "+ee.getMessage()); }
}
}
public static void main(String args[])
{ new StuserchFrame();
String JDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String conURL="jdbc:odbc:Xsstudent";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e)
{System.out.println("加载驱动失败");}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -