是那里不能运行呢!
可以打印出sql语句吗!

解决方案 »

  1.   

    以下是错误信息........JdbcConnect.java [7:1] unreported exception java.lang.ClassNotFoundException; must be caught or declared to be thrown
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                             ^
    JdbcConnect.java [9:1] unreported exception java.sql.SQLException; must be caught or declared to be thrown
                Connection con = DriverManager.getConnection(source);
                                                            ^
    JdbcConnect.java [10:1] unreported exception java.sql.SQLException; must be caught or declared to be thrown
                Statement stmt  = con.createStatement();
                                                     ^
    JdbcConnect.java [12:1] unreported exception java.sql.SQLException; must be caught or declared to be thrown
                ResultSet rs = stmt.executeQuery(sql);
                                                ^
    JdbcConnect.java [14:1] unreported exception java.sql.SQLException; must be caught or declared to be thrown
                while (rs.next())
                              ^
    JdbcConnect.java [16:1] unreported exception java.sql.SQLException; must be caught or declared to be thrown
                    FldName=rs.getString("FldName");
                                        ^
    JdbcConnect.java [17:1] unreported exception java.sql.SQLException; must be caught or declared to be thrown
                    FldCaption=rs.getString("FldCaption");
                                           ^
    JdbcConnect.java [18:1] unreported exception java.sql.SQLException; must be caught or declared to be thrown
                    FldID=rs.getString("FldID");
                                      ^
    JdbcConnect.java [22:1] unreported exception java.sql.SQLException; must be caught or declared to be thrown
                 rs.close();
                         ^
    JdbcConnect.java [23:1] unreported exception java.sql.SQLException; must be caught or declared to be thrown
                 stmt.close();
                           ^
    JdbcConnect.java [24:1] unreported exception java.sql.SQLException; must be caught or declared to be thrown
                 con.close();
                          ^
    11 errors
    编译  JdbcConnect. 时出错
      

  2.   

    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String source = "jdbc:odbc:MyODBC";  //  MyODBC 我已经配置正确
                Connection con = DriverManager.getConnection(source);
                Statement stmt  = con.createStatement();
                String sql = "select * from Sys_FldInfo";
                ResultSet rs = stmt.executeQuery(sql);
                String FldName,FldCaption,FldID;
                while (rs.next())
                {
                    FldName=rs.getString("FldName");
                    FldCaption=rs.getString("FldCaption");
                    FldID=rs.getString("FldID");
                    System.out.println("=========== 数据库表 ==========");
                    System.out.println(FldName+"  "+FldCaption+"  "+FldID);
                 }    
    }catch(ClassNotFoundException e){
    }catch(SQLException e){
    }
      

  3.   

    我加了 try..catch 之后 错误确实没有了!但是也没有打印出我想要的结果!  1:为什么java连接数据库必须要 捕获异常 ,不捕获不行吗?
      2:为什么还是不能打印结果呢?
      3:while (rs.next()) 我觉得这里有错误!
         在delphi中应该是
         while not rs.eof do
         begin
           ... 
           rs.next;    
         end;
         java 中为什么没有判断 eof 
      

  4.   

    1.捕获异常是java语言必须的
    2.不能打映结果?有什么出错信息?
      

  5.   


    javac JdbcConnect.java
    java  JdbcConnect我这2个都能正常运行,就是没有一个错误信息
      

  6.   

    rs.next()为null
    sql语句的查询为空!
      

  7.   

    To:fantasyCoder
      你好,你有QQ吗? 我的是 99804335 .
      

  8.   

    java不用判断eof,ResultSet.next()的功能就是移动游标,判断eof。
    它的原型是boolean next() throws SQLException(这里的throws 决定
    了你必须加上try和catch,因为异常必须"一抛到底",包括createStatement
    这些方法全部都throws SQLException,所以要全部包在try中)没有结果是什么意思?试一下getString(int)?或者是其他类型的getXXX()?
      

  9.   

    没有结果就是 
    我运行了  java  JdbcConnect  一点反应也没有,没有错误,没有结果。
    等几秒之后就到了  C:\
      

  10.   

    有那位愿意帮我在你们自己的机器测试一下
    String source = "jdbc:odbc:MyODBC";  
    String sql = "select * from Sys_FldInfo";
    把这2句稍微修改一下,看是否能够正常运行!
      

  11.   

    郑重申明
    select * from Sys_FldInfo 这个表里面绝对有数据
    这里不是有输出语句吗 ?System.out.println(FldName+"  "+FldCaption+"  "+FldID);
      

  12.   

    TO:fantasyCoder(牛仔+T恤) 
       
      你好,首先感谢你的热情回答。我看 java 确实没有几天。所以矛盾一大堆!
      你能说说我这个程序错在那里不?
      你有没有 MSN , QQ 之类的 这样聊起来方便些,也不会浪费服务器资源!
      

  13.   

    FldName=rs.getString("FldName");
    FldCaption=rs.getString("FldCaption");
    FldID=rs.getString("FldID");好象有问题,改成
    FldName=rs.getString(1);
    FldCaption=rs.getString(2);
    FldID=rs.getString(3);试试看
      

  14.   

    Please show us the DDL of your table
      

  15.   

    import java.sql.*;
    public class JdbcConnect
    {
        public static void main(String arg[])
        {            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String source = "jdbc:odbc:MyODBC";  //  MyODBC 我已经配置正确
                Connection con = DriverManager.getConnection(source);
                Statement stmt  = con.createStatement();
                String sql = "select * from Sys_FldInfo";
                ResultSet rs = stmt.executeQuery(sql);
                String FldName,FldCaption,FldID;
                while (rs.next())
                {
                    FldName=rs.getString("FldName");
                    FldCaption=rs.getString("FldCaption");
                    FldID=rs.getString("FldID");
                    System.out.println("=========== 数据库表 ==========");
                    System.out.println(FldName+"  "+FldCaption+"  "+FldID);
                 }    
                 rs.close();
                 stmt.close();
                 con.close();
         }
    }
    java程序发生错误会有很多具体的原因,我提供以下可能供参考:
    1、数据库连接有问题,MyODBC名称和你的windows中的odbc名称不符,或者在你的odbc中MyODBC的默认的数据库不对。
    2、sql是否能在sql查询器中选出数据?
    3、由于你使用的是select * 所以选出的列名和数据库表的列名一样,
       rs.getString("FldID")中的FldID必须和你的数据库表的列名一样,或者你使用FldID=rs.getString(1);但是这样你应该确认取出的第一列为FldID;