import java.awt.*;
import java.net.*;
import java.sql.*;
import java.awt.event.*;
class Window extends Frame implements ActionListener
{
TextArea text;
Panel panel;
TextField xingming;
Button button1,button2;
Window()
{
super("成绩查询");
setLayout(new BorderLayout());
setBackground(Color.cyan);
setBounds(150,150,300,120);
setVisible(true);
text=new TextArea();
button1=new Button("确定");
button2=new Button("查询所有人的成绩");
xingming=new TextField(16);
panel=new Panel();
panel.add(new Label("输入被查询的学生的姓名:"));
panel.add(xingming);
panel.add(button1);
add("North",panel);
add(text,"Center");
add(button2,"South");
text.setEditable(false);
text.setBackground(Color.pink);
button1.addActionListener(this);
button2.addActionListener(this);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){setVisible(false);System.exit(0);}});
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
text.setText("查询结果"+'\n');
try{Liststudent1();}
catch(SQLException ee){}
}
else if(e.getSource()==button2)
{
text.setText("查询结果"+'\n');
try{Liststudent2();}
catch(SQLException ee){}
}
}
public void Liststudent1() throws SQLException
{
String name;
int math,english,physics,sum;
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){}
Connection con=DriverManager.getConnection("jdbc:odbc:redsun","chengjibiao","ookk");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("SELECT*FROM chengjibiao");
while(rs.next())
{
name=rs.getString("姓名");
math=rs.getInt("数学");
physics=rs.getInt("物理");
english=rs.getInt("英语");
sum=english+math+physics;
if((xingming.getText()).trim().equals(name)) // 为什么上面这句运行正常而下边有异常
//if(name.equals((xingming.getText()).trim())) // 这两句有什么区别?
{
text.append("***"+"\n"+name+"的成绩:"+"\n"+"数学:"+math+
"英语:"+english+"物理:"+physics+"总数:"+sum+'\n');
}
}
if((text.getText().trim()).equals("查询结果"))
{
text.setText("没有该人的成绩");
}
}
public void Liststudent2() throws SQLException
{
String name;
int math,english,physics,sum;
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e){IO.showMessage("error!");}
Connection con=DriverManager.getConnection("jdbc:odbc:redsun","snow","ookk");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("SELECT*FROM chengjibiao");
while(rs.next())
{
name=rs.getString("姓名");
math=rs.getInt("数学");
physics=rs.getInt("物理");
english=rs.getInt("英语");
sum=english+math+physics;
text.append("***"+"\n"+name+"的成绩:"+"\n"+"数学"+math+
"英语:"+english+"物理:"+physics+"总数:"+sum+'\n');
}
}
}
public class Example
{
public static void main(String args[])
{
Window window=new Window();
window.pack();
}
}