×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

Let us try to pin down the problem

Dear NetMan,

I have no instant help to offer here. However, I
doubt if you have put any data in your table. If yes, let's go on.

Here is a small trick. Since you are suspicious about your while loop, I suggest you PROVE or DISPROOVE such a conjectore. Remove while loop. Add some code like System.out.println(rSet.next());, if you see true, it means the code in your while loop is problematic.

I am using DB2 but I do not access DB2 using JDBC. I may ask one of my friends to have a look at your code.
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / a jdbc problem
    本文发表在 rolia.net 枫下论坛I write a jdbc program to connect to a db2 database's table, table is created by:
    create table mytest(name varchar(20),age int).
    The following is my program:
    import java.sql.*;
    import java.io.*;
    class my{
    public static void main(String args[]){
    String dsn="jdbc:db2:mydb";
    String userid="mydb2user";
    String pwd="myuser";
    Connection conn=null;
    Statement stmt=null;
    ResultSet rSet=null;
    try{
    Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
    }
    catch(Exception e){
    e.printStackTrace();
    }
    try{
    conn=DriverManager.getConnection(dsn,userid,pwd);
    stmt=conn.createStatement();
    rSet=stmt.executeQuery("select * from mytest");
    System.out.println("the column of datetype");
    while(rSet.next())
    {
    int i=0;
    String nm=rSet.getString(1);
    int ag=rSet.getInt(2);
    System.out.println("The name is: "+nm);
    System.out.println("The age is: "+ag);
    System.out.println(">>>>>>>> "+i );
    i++;

    }
    rSet.close();
    stmt.close();
    conn.close();

    }
    catch(SQLException e){e.printStackTrace();}

    }
    }

    I can't get correct result,only get a loop:
    The age is: 0
    >>>>>>> 0
    The name is:
    >>>>>>> 0
    The age is: 0
    The name is:
    >>>>>>> 0
    ........
    It looked like the while() never continued.

    my machine is winnt+jdk1.2+db25.0
    I changed the driver to ODBC,but get the
    same result, who can tell me what's wrong
    in my program?更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • sorry, the definetion of "int i=0" should out of while()'s body. It seemed the while() is continued, but I still can't get correct data.
      • By the way, why do you introduce index i? you are mixing a for-loop with a while-loop. Not good...
        • I can't get the correct result, and I want to see how the while() goes, so I intruduce int i.
    • Let us try to pin down the problem
      Dear NetMan,

      I have no instant help to offer here. However, I
      doubt if you have put any data in your table. If yes, let's go on.

      Here is a small trick. Since you are suspicious about your while loop, I suggest you PROVE or DISPROOVE such a conjectore. Remove while loop. Add some code like System.out.println(rSet.next());, if you see true, it means the code in your while loop is problematic.

      I am using DB2 but I do not access DB2 using JDBC. I may ask one of my friends to have a look at your code.
      • Actually,I've put some data into the table...
        Jabber,thank you for your care.
        I've put two rows of data into the table, but the program never show me them, and the value of rSet.next() seems never to be false, so I got the endless while loop like this:

        The age is: 0
        The name is:
        >>>>>>> 0
        The age is: 0
        The name is:
        >>>>>>> 1
        The age is: 0
        The name is:
        >>>>>>> 2
        The age is: 0
        The name is:
        >>>>>>> 3
        ........
        • A few suggestions
          1) Check if ResultSet is null;
          2) get ResultMetaData
          3) Analyse this ResultMetaData to see the strcture
          I know you have been annoyed by this thing, but we are supposed to handle such things. This is the reason we are paid. :-):
          • My program has no problem
            Jabber, I changed the databae to Ms sql server, the program worked very well !
            Now I doubt the problem is the translation of chinese and english in java's database driver . My machine is chinese WinNt+jdk1.2+chinese db2 5.0 client. In my program, I connect to an English db2 server in an AIX machine. I know javac has an option "encoding ..." , is it used to change language ?
            • Actually, I talked my colleage for your code and he said your code is OK.
              Sorry, I have not yet used "encoding" option. Since you are a veteran, I just need simply mention the following fact: Go to sun's site and browse JDK specification, you will know what "encoding" means.
              • I'm not a veteran ,but a novice in java field.惭愧惭愧, thanks Jabber. :)