Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: java.lang.String cannot be cast to java.util.Hashtable

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy java.lang.String cannot be cast to java.util.Hashtable

    hi guys, im using the enumeration and hashtable. i have two separate classes wherein in the first one is the retrieving of data from database (the return type is enumeration) and other one is the printing of data..

    PHP Code:
    try
            {
                
    InsertSelect is = new InsertSelect();
                
    Hashtable ht is.selectData("unitcode""tblunit");
             
                
    Enumeration enu ht.keys();
                while(
    enu.hasMoreElements())
                {
                    
    ht = (Hashtable)enu.nextElement();
                   
    String hehe =ht.get("field").toString();
                    
    //cmbUnit.addItem(ht.get("field"));
                   
    System.out.println(hehe);
                }
             } 
    PHP Code:
    public class InsertSelect {
        public 
    InsertSelect() {}
        public 
    Hashtable selectData(String fieldString table){
            
    Hashtable hashSelect = new Hashtable();
            
    String query "SELECT "field +" " " FROM " +table;
            
    connection con = new connection(); 
            
            try{
            
    Statement st con.getCon().createStatement();
            
    ResultSet rs st.executeQuery(query);
                 while(
    rs.next()){
                     
    hashSelect.put("field",field);
                 }
            }
            catch(
    Exception e){System.out.println(e.getMessage());}
            return 
    hashSelect;
        }
                
        

    when i run the program i got the error: java.lang.String cannot be cast to java.util.Hashtable

    so what will i do? im new here?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java.lang.String cannot be cast to java.util.Hashtable

    Why do you expect the value to be a Hashtable?

    What does the keys() method return?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.String cannot be cast to java.util.Hashtable

    so what will i do? sorry, i am really new in hashtable and enumeration ..

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java.lang.String cannot be cast to java.util.Hashtable

    Explain what you want to do with the Hashtable that is returned by the method?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.String cannot be cast to java.util.Hashtable

    It must returned the unitcode(rl, m, RL) from a specific table of database.

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.String cannot be cast to java.util.Hashtable

    i got it now, but my problem is, it only return the last data from database (ie unicode = rl, m , RL) only RL returned.. this is my revise codes:

    public Hashtable selectData(String field, String table){
    Hashtable hashSelect = new Hashtable();

    String query = "SELECT "+ field + " FROM " +table;
    System.out.println(query);
    connection con = new connection();

    try{
    Statement st = con.getCon().createStatement();
    ResultSet rs = st.executeQuery(query);
    while(rs.next()){
    hashSelect.put("field",rs.getString(field));

    }
    }
    catch(Exception e){System.out.println(e.getMessage());}

    System.out.println( hashSelect.keys());
    return hashSelect;
    }


    and


    try
    {
    InsertSelect is = new InsertSelect();
    Hashtable ht = is.selectData("unitcode", "tblunit");
    String str;
    Enumeration enu = ht.keys();
    while(enu.hasMoreElements())
    {
    str = (String)enu.nextElement();

    cmbUnit.addItem(enu.get(str));

    }
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: java.lang.String cannot be cast to java.util.Hashtable

    only return the last data
    You should give every key a new value when adding to a hashtable. If you capture the value returned by the put() method you will see that you are continually replacing the hashtables entries not adding to it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java Error cannot be applied to (java.lang.String), phone book entry program.
    By iceyferrara in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 23rd, 2011, 06:32 AM
  2. Operator % cannot be applied to java.lang.String,int?
    By javarum in forum Java Theory & Questions
    Replies: 8
    Last Post: July 12th, 2011, 08:06 PM
  3. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  4. How to reverse a String using java.lang.StringBuilder
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: July 22nd, 2009, 09:42 AM
  5. [SOLVED] Facing java error "cannot be applied to (java.lang.String)"
    By tazjaime in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2009, 10:19 AM