成绩查询系统介绍
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.sql.*;
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){ }//建立JDBC-ODBC桥接器
Connection con=DriverManager.getConnection("jdbc:odbc:redsun","","");
//连接到数据库
Statement sql=con.createStatement();//向数据库发送SQL语句
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((name.trim()).equals(xingming.getText().trim()))
{
text.append("***"+"/n"+name+"的成绩:"+"/n"+"数学:"+math+
"/t英语:"+english+"/t物理:"+physics+"/t总分:"+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){ }
Connection con=DriverManager.getConnection("jdbc:odbc:redsun","","");
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 Example23_2
{
public static void main(String args[])
{
Window window=new Window();
window.pack();
}
}
本文地址:http://www.45fan.com/dnjc/73365.html