当前位置: 代码迷 >> Java相关 >> rs.next()的一点小问题
  详细解决方案

rs.next()的一点小问题

热度:211   发布时间:2007-07-21 08:17:23.0
rs.next()的一点小问题

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();
}
}

搜索更多相关的解决方案: next  import  JTextField  new  java  

----------------解决方案--------------------------------------------------------
回复:(gxppa2006)rs.next()的一点小问题

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Win extends JFrame implements ActionListener
{
String url="jdbc:odbc:info";
Connection con=null;
ResultSet rs = null;

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);

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,"","");
Statement stmt=con.createStatement();
rs=stmt.executeQuery("select * from info");
setVisible(true);
setSize(400,400);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
{
try
{
int id,age;
String name,sex;
if(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();
}
}

试试
----------------解决方案--------------------------------------------------------
刚我试了  怎么还是不好使  就出来第一条   呵呵
----------------解决方案--------------------------------------------------------
以下是引用gxppa2006在2007-7-21 8:17:23的发言:

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;
Connection con=null;
ResultSet rs;
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);

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:info";
con=DriverManager.getConnection(url,"","");
Statement stmt=con.createStatement();
rs=stmt.executeQuery("select * from info");
catch(Exception e){

}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
{


try
{

rs.next() //这里我想没点一下确定顺序显示一条记录
int id,age;
String name,sex;//用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();
}
}

试试

[此贴子已经被作者于2007-7-21 14:03:58编辑过]


----------------解决方案--------------------------------------------------------
呃  还是不行。。。
----------------解决方案--------------------------------------------------------
回复:(野蛮女人)以下是引用gxppa2006在2007-7-21 8...

我的那个可能是点了一下连接就关闭了的缘故

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Win extends JFrame implements ActionListener
{
String url="jdbc:odbc:info";
Connection con=null;
ResultSet rs = null;

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);

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,"","");
Statement stmt=con.createStatement();
rs=stmt.executeQuery("select * from info");
setVisible(true);
setSize(400,400);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
{
try
{
int id,age;
String name,sex;
if(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)
{

}

}
if(e.getSource()==exit)
{
try
{
con.close();
}
catch(Exception ee)
{

}
System.exit(0);
}
}
}


public class DataTest
{
public static void main(String[] args)
{
new Win();
}
}

再试试


----------------解决方案--------------------------------------------------------
呵呵   感谢楼上拉   赞
----------------解决方案--------------------------------------------------------
  相关解决方案