日期:2014-05-20  浏览次数:20826 次

Java取Access数据库,为什么只能取出一行记录?
//程序如下
//结果只输出: 姓名:张三
//就Process completed


import java.sql.*; 

public class database { 
  public static void main(String args[]) { 
  try{ 
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
  String url="jdbc:odbc:telbook"; 
  Connection connection=DriverManager.getConnection(url); 
  Statement statement = connection.createStatement(); 
  String sql="SELECT * FROM telbook"; 
  ResultSet rs = statement.executeQuery(sql); 
  String tt; 
  while (rs.next()) { 
  System.out.print("姓名:" + rs.getString("Name")); 
  System.out.println(" 电话:" + rs.getString("tel")); 
  System.out.println(" Email:" + rs.getString("email")); 
  } 
  rs.close(); 
  connection.close(); 
  } 
  catch(Exception ex){ 
  System.out.println(ex); 
  System.exit(0); 
  } 
  } 
}

高手同学帮忙解答,谢谢!

------解决方案--------------------
我觉得应该没问题吧!你看一下是不是你的字段名搞错了!
我这个和你的都差不多一样的,我的可以运行,没问题.你对照一下吧!
Java code

public class SQLDemo
{
    public static void main(String[] args) 
    {
        try
        {
        String id,name,sex;
        String   strURL   =  "jdbc:odbc:testData"; 
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection(strURL);
        Statement Stmt = con.createStatement();
        ResultSet rs = Stmt.executeQuery("SELECT id,name,sex FROM test1");
        System.out.println("id,name,sex");
        while(rs.next())
        {
            id = rs.getString("id");
            name = rs.getString("name");
            sex = rs.getString("sex");
            System.out.println(id+","+name+","+sex);
        }
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
}