我的hashtable是全局的,我用了一个函数constructtable来建这个表,然后在collectwordpair的函数中get,但是get到的都是null, 而只是的table size却又显示里面是有东西的。在constructtalbe函数里,我也试过get,是可以正确的get出东西的。请高手给我看看:
 private static void ConstructTable(){
        String word = new String();
        StringBuffer WordChain = new StringBuffer();
        byte Aword[] = null;
        ResultSet rs = null;
        Statement stm1 = null;
        PreparedStatement stm2 = null;
        Connection con1 = null;
        String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        String DATABASE_URL = "jdbc:mysql://localhost/wordbase"; 
        int i = 0;
        try{
           
           Class.forName(JDBC_DRIVER);
           con1 = DriverManager.getConnection(DATABASE_URL, "root", "root");
           stm1 = con1.createStatement();
           //stm2 = con1.prepareStatement("SELECT * FROM worddatas where word = ?");
           //construct w_table
           rs = stm1.executeQuery("SELECT *  FROM worddatas");
           rs.beforeFirst();
           while(rs.next()){
               word = rs.getString(1);
               Aword = word.getBytes("ISO-8859-1");
               word = new String(Aword, "GB2312");
               
               //System.out.println(word);
               W_table.put(Aword, word);
               //System.out.println((String)W_table.get(Aword));
               //i++;
               
           }
            
          
           //System.out.println(i);
            con1.close();
       }
       
       catch(Exception e){
           System.out.println(e);
           System.exit(1);
       }      } 
    
    // collect word pair
    private static void CollectWordPair(String artical){
        int i, j, k, m, n;
        char Line[] = new char[8192];        
        File InputFile = null;
        FileInputStream fin = null;
        InputStreamReader InArtical = null;
        int NumberOfRead = -1;
        StringBuffer Sen = new StringBuffer();
        String GWord_InSen = new String();
        String AWord_InSen = new String();
        String GWord_InDat = new String();
        String AWord_InDat = new String();
        String Sentence = new String();
        String pair_insen = new String();
        String pair_indat = new String();
        Connection con1 = null;
        String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        String DATABASE_URL = "jdbc:mysql://localhost/wordbase"; 
        PreparedStatement pstm1 = null;
        PreparedStatement pstm2 = null;
        PreparedStatement pstm3 = null;
        ResultSet rs_current = null;
        int T = 0;
        byte word[] = null;
        
        try{
            //connect database
            Class.forName(JDBC_DRIVER);
            con1 = DriverManager.getConnection(DATABASE_URL, "root", "root");
            pstm1 = con1.prepareStatement("UPDATE Probability_c SET Times = ? WHERE G_String = ? AND A_String = ?");
            pstm2 = con1.prepareStatement("INSERT INTO Probability_c (G_String, A_String, Times) VALUES (?, ?, ?)");
            pstm3 = con1.prepareStatement("SELECT * FROM Probability_c WHERE G_String = ? AND A_String = ?");
            System.out.println("connect database success");
            
            
            InputFile = new File(artical);
            fin = new FileInputStream(InputFile);
            InArtical = new InputStreamReader(fin);
            
            
            
            do{
               NumberOfRead = InArtical.read(Line, 0, 8192);
               for(i=0; i<8192; i++){
                   if(Line[i] != '\n'){
                       Sen.append(Line[i]);
                   }
                   else{
                       System.out.println("sentence constructed");
                       Sentence = Sen.toString();
                       Sen.delete(0, Sen.length());
                       Sentence.trim();
                      
                       System.out.println("begin to search word pair");
                       for(j=0; j<Sentence.length(); j++){
                           for(m=5; m>0; m--){
                               if(j+m > Sentence.length()-2)continue;   // m to large,extended the sentence
                               
                               GWord_InSen = Sentence.substring(j,j+m);
                               word = GWord_InSen.getBytes("ISO-8859-1");
                               //T = W_table.size();
                               //System.out.println(T);
                               GWord_InDat = (String)W_table.get(word);
                               //System.out.println(GWord_InDat);
                               if(GWord_InDat != null){
                                   System.out.println("a given word found!");
                                   for(n=5; n>0; n--){
                                       if(j+m+n > Sentence.length()-1)continue;        // n to large,extended the sentence
                                       AWord_InSen = Sentence.substring(j+m, j+m+n);
                                       AWord_InDat = (String)W_table.get(GWord_InSen);
                                       if(AWord_InDat != null){
                                           System.out.println("an after word found");
                                           pstm3.clearParameters();
                                           pstm3.setString(1, GWord_InSen);
                                           pstm3.setString(2, AWord_InSen);
                                           rs_current = pstm3.executeQuery();
                                           rs_current.first();
                                           if(rs_current.getRow() != 0){
                                              System.out.println("pair exist");
                                              T = rs_current.getInt(3) + 1;
                                              pstm1.clearParameters();
                                              pstm1.setInt(1, T);
                                              pstm1.setString(2, GWord_InSen);
                                              pstm1.setString(3, AWord_InSen);
                                              pstm1.execute();
                                              }
                                          else{
                                              System.out.println("pair not exist");
                                              pstm2.clearParameters();
                                              pstm2.setString(1, GWord_InSen);
                                              pstm2.setString(2, AWord_InSen);
                                              pstm2.setInt(3, 1);
                                              pstm2.execute();
                                              }
                                               
                                           }
                                       }
                                       
                                   }
                               }
                               
                           }
                       }
                   }
                              
            }while(NumberOfRead == 8192);
            
            con1.close();   
        }
        catch(Exception e){
            System.out.println(e);
            System.exit(1);
        }
    }