这是Java和access连接的一个GUI,目的就是在数据库里返回一条记录、
全部代码如下:
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.sql.*;
public class Card1 implements ActionListener{

JFrame jf=new JFrame("校园卡");

    //JPanel jp = new JPanel();
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JPanel jp3=new JPanel();
JPanel jp4=new JPanel();
///JLable
JLabel jl_card=new JLabel("请输入卡号:");
JLabel jl_name=new JLabel("姓   名 :");
JLabel jl_class=new JLabel("班   级 :");
JLabel jl_money=new JLabel("余   额 :");
JLabel jl_namek=new JLabel("  ");
JLabel jl_classk=new JLabel("  ");
JLabel jl_moneyk=new JLabel("  ");
//JTextField
JTextField jtf_card=new JTextField(25);
JTextField jtf_name=new JTextField(25);
JTextField jtf_class=new JTextField(25);
JTextField jtf_money=new JTextField(25);
         ImageIcon icon=new ImageIcon("a.jpg");
        //布局
GridLayout gl=new GridLayout(4,2);
FlowLayout fl_card=new FlowLayout(FlowLayout.CENTER,80,50);
FlowLayout fl_name=new FlowLayout(FlowLayout.CENTER,80,30);
FlowLayout fl_class=new FlowLayout(FlowLayout.CENTER,80,30);
FlowLayout fl_money=new FlowLayout(FlowLayout.CENTER,80,30);
Button button=new Button("check");
public void actionPerformed(ActionEvent e){
     String s1=jtf_card.getText();
     select(s1);
     System.out.println(s1);
    
    }
    
    
////////////////////////////////////////////////////////////////////////////////////////////主要错在这里
    void select(String s)
     {        Connection con = null;
Conn cc = new Conn();
try {
con = cc.getConn();
Statement stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
System.out.println("前面");
ResultSet rst = stm.executeQuery("select * from tableA where cardnum='"+ s + "'");
     System.out.println (rst.getString(3));
System.out.println("后面");
while (rst.next()) {
         jtf_name.setText(rst.getString(3));
jtf_class.setText(rst.getString(4));
jtf_money.setText(rst.getString(5));

}
} catch (Exception e) {
System.out.println("查寻失败!");
e.printStackTrace();
} finally {
cc.closeConn(con);
}
}
//////////////////////////////////////////////////////////////////////////////////



Card1(){
JPanel jp = new JPanel()
        
       {protected void paintComponent(Graphics g)
            {
               g.drawImage(icon.getImage(),0,0, null);
 
               super.paintComponent(g);
            }
       };
                 
jf.add(jp);
jp.setLayout(gl);
jp.add(jp1);
jp.add(jp2);
jp.add(jp3);
jp.add(jp4);

jp1.add(jl_card);
jp1.add(jtf_card);
jl_card.setFont(new Font("宋体",Font.BOLD,20));
jl_card.setForeground(Color.green);
jtf_card.setFont(new Font("宋体",Font.BOLD,20));
jtf_card.setForeground(Color.green);
jtf_card.setOpaque(false);
    jp1.add(button);
    button.addActionListener(this);
jp1.setLayout(fl_card);
jp2.add(jl_name);
jl_name.setFont(new Font("宋体",Font.BOLD,20));
jl_name.setForeground(Color.green);
jp2.add(jtf_name);
jtf_name.setFont(new Font("宋体",Font.BOLD,20));
jtf_name.setForeground(Color.green);
jtf_name.setOpaque(false);
jp2.add(jl_namek);
jl_namek.setFont(new Font("宋体",Font.BOLD,20));

jp2.setLayout(fl_name);
jl_name.setFont(new Font("宋体",Font.BOLD,20));
jl_name.setForeground(Color.green);


jp3.add(jl_class);
jl_class.setFont(new Font("宋体",Font.BOLD,20));
jl_class.setForeground(Color.green);
jp3.add(jtf_class);
jtf_class.setFont(new Font("宋体",Font.BOLD,20));
jtf_class.setForeground(Color.green);
jtf_class.setOpaque(false);
jp3.add(jl_classk);
jl_classk.setFont(new Font("宋体",Font.BOLD,20));
jp3.setLayout(fl_class);

jp4.add(jl_money);
jl_money.setFont(new Font("宋体",Font.BOLD,20));
jl_money.setForeground(Color.green);
jp4.add(jtf_money);
jtf_money.setFont(new Font("宋体",Font.BOLD,20));
jtf_money.setForeground(Color.green);
jtf_money.setOpaque(false);
jp4.add(jl_moneyk);
jl_moneyk.setFont(new Font("宋体",Font.BOLD,20));
jp4.setLayout(fl_money);

jp1.setOpaque( false );
         jp2.setOpaque( false );
         jp3.setOpaque( false );
         jp4.setOpaque( false );
         jp.setOpaque(false);
jf.setBounds(320,110,700,500);
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void main(String args[]){
new Card1();
   }
}
数据库:
cardnum xnum name calsses moneymoney
123 200801 张三 软件 200
都是文本类型的。

解决方案 »

  1.   

    rs游标位置初始位置是在第一条记录之前,此时对rs取值就会报出无效的光标状态
    先调用一次rs.next(),使得rs游标指向返回列的第一行;  
    然后打印结果字段时还得加判断if(rs.next())   或   while(rs.next())   ,决定所指向列是否为空
    System.out.println("后面");
    rst.next();
    while (rst.next()) {
      

  2.   

    我在
    System.out.println("后面"); 后面加了rst.next(); 
    但是错误还是“java.sql.sqlexception microsoft odbc 驱动程序管理器 无效的游标状态”
      

  3.   

    直接设置游标位置
    rst.first()
      

  4.   

    把rst.next(); 改成rst.first();也是一个错误。
    这个异常会不会是别的错误引起的。。
      

  5.   

    恩,知道那错了,////////////////////////////////////////////////////////////////////////////////////////////主要错在这里 
        void select(String s) 
        {        Connection con = null; 
    Conn cc = new Conn(); 
    try { 
    con = cc.getConn(); 
    Statement stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); 
    System.out.println("前面"); 
    ResultSet rst = stm.executeQuery("select * from tableA where cardnum='"+ s + "'"); 
        System.out.println (rst.getString(3)); ///////这里不该在这里了
    System.out.println("后面"); 
    while (rst.next()) { 
            jtf_name.setText(rst.getString(3)); 
    jtf_class.setText(rst.getString(4)); 
    jtf_money.setText(rst.getString(5)); } 
    } catch (Exception e) { 
    System.out.println("查寻失败!"); 
    e.printStackTrace(); 
    } finally { 
    cc.closeConn(con); 


    ////////////////////////////////////////////////////////////////////////////////// 
    谢谢帮忙!谢谢!
      

  6.   

    晕,没注意看中间的代码,只关注while(rst.next())了