import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Win extends JFrame implements ActionListener
{
JButton ok,exit;
JTextField name_f,sex_f,age_f,id_f;
Win()
{
ok=new JButton("确定");
exit=new JButton("退出");
name_f=new JTextField();
sex_f=new JTextField();
id_f=new JTextField();
age_f=new JTextField();
Container con=this.getContentPane();
con.setLayout(new GridLayout(5,2));
con.add(new JLabel("ID"));
con.add(id_f);
con.add(new JLabel("姓名"));
con.add(name_f);
con.add(new JLabel("性别"));
con.add(sex_f);
con.add(new JLabel("年龄"));
con.add(age_f);
con.add(ok);
con.add(exit);
ok.addActionListener(this);
exit.addActionListener(this);
setVisible(true);
setSize(400,400);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
{
Connection con=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:info";
con=DriverManager.getConnection(url,"","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from info");
int id,age;
String name,sex;
while(rs.next()) //这里我想没点一下确定顺序显示一条记录
{ //用while的话只能显示最后一条记录
name=rs.getString("name");//用if的话只显示第一条记录
sex=rs.getString("sex");//用absolute感觉是不会还是怎么的不出结果
id=rs.getInt("id");
age=rs.getInt("age");
name_f.setText(name);
sex_f.setText(sex);
id_f.setText(""+id);
age_f.setText(""+age);
}
}
catch(Exception ew)
{
}
finally
{
try
{
con.close();
}
catch(Exception ee)
{
}
}
}
if(e.getSource()==exit)
{
System.exit(0);
}
}
}
public class DataTest
{
public static void main(String[] args)
{
new Win();
}
}