473,387 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

can't put anything to hashtable

6
Hi all,

I have just changed my JDK, from 1.5.0 to 1.6.0, and also change my Netbeans, from NB 4.0 to NB 5.5. I've got a problem with my code, which I wrote on NB 4.0 + JDK 1.5.0.. I fail to put anything into my hashtable when I try the code on NB 5.5 + JDK 1.6.0. Here is the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     public Hashtable getIDUser (HttpServletRequest request, HttpServletResponse response, String sqlWhere, String namaUser) 
  3.     throws SQLException, IOException  {
  4.         ServletOutputStream out = response.getOutputStream();
  5.  
  6.         String sqlGetUserID = "SELECT ID, Nama, Password FROM " + strTblUser + " ";
  7.         if (sqlWhere == null) {
  8.             sqlGetUserID = sqlGetUserID;
  9.         } else {
  10.             sqlGetUserID = sqlGetUserID + sqlWhere;
  11.         }
  12.  
  13.         // check 1
  14.         out.println("sqlGetUserID : " + sqlGetUserID + "<br>");
  15.  
  16.         int a = 1;
  17.         try {
  18.             Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
  19.             Connection con1 = DriverManager.getConnection(DBurl, "", "");
  20.             Statement stmt1 = con1.createStatement();
  21.             ResultSet rs1 = stmt1.executeQuery(sqlGetUserID);
  22.  
  23.             // check 2
  24.             ResultSetMetaData rsmd = rs1.getMetaData();
  25.             int colCount = rsmd.getColumnCount();
  26.             out.println("colCount: " + colCount + "<br>");
  27.  
  28.             Hashtable HTArea = new Hashtable();
  29.  
  30.             while (rs1.next()) {
  31.                 HTArea.put(new Integer(a), rs1.getString(0));
  32.                 a = a + 1;
  33.             }
  34.  
  35.             // check 3
  36.             int isiHT;
  37.             isiHT = HTArea.size();
  38.             out.println("isiHT: " + isiHT + "<br>");
  39.  
  40.             rs1.close();
  41.             stmt1.close();
  42.             con1.close();
  43.             return HTArea;
  44.     }
  45.     catch (SQLException se) {
  46.             /** just catch */
  47.     }
  48.         catch (java.lang.Exception ex) {
  49.             /** just catch */
  50.     }
  51.         return new Hashtable();
  52.     }    
  53.  
  54.  
If you look at my code, you will see that I have put some lines to check the code. I have no problem with Check 1 and Check 2, but when I see Check 3, I know something was happen. There is nothing in my Hashtable. Did I do something wrong when I try to put ResultSet into my hashtable? Can someone help me? Thank you.


Regards,
Novan Ananda
Dec 10 '07 #1
4 2048
r035198x
13,262 8TB
Hi all,

I have just changed my JDK, from 1.5.0 to 1.6.0, and also change my Netbeans, from NB 4.0 to NB 5.5. I've got a problem with my code, which I wrote on NB 4.0 + JDK 1.5.0.. I fail to put anything into my hashtable when I try the code on NB 5.5 + JDK 1.6.0. Here is the code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     public Hashtable getIDUser (HttpServletRequest request, HttpServletResponse response, String sqlWhere, String namaUser) 
  3.     throws SQLException, IOException  {
  4.         ServletOutputStream out = response.getOutputStream();
  5.  
  6.         String sqlGetUserID = "SELECT ID, Nama, Password FROM " + strTblUser + " ";
  7.         if (sqlWhere == null) {
  8.             sqlGetUserID = sqlGetUserID;
  9.         } else {
  10.             sqlGetUserID = sqlGetUserID + sqlWhere;
  11.         }
  12.  
  13.         // check 1
  14.         out.println("sqlGetUserID : " + sqlGetUserID + "<br>");
  15.  
  16.         int a = 1;
  17.         try {
  18.             Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
  19.             Connection con1 = DriverManager.getConnection(DBurl, "", "");
  20.             Statement stmt1 = con1.createStatement();
  21.             ResultSet rs1 = stmt1.executeQuery(sqlGetUserID);
  22.  
  23.             // check 2
  24.             ResultSetMetaData rsmd = rs1.getMetaData();
  25.             int colCount = rsmd.getColumnCount();
  26.             out.println("colCount: " + colCount + "<br>");
  27.  
  28.             Hashtable HTArea = new Hashtable();
  29.  
  30.             while (rs1.next()) {
  31.                 HTArea.put(new Integer(a), rs1.getString(0));
  32.                 a = a + 1;
  33.             }
  34.  
  35.             // check 3
  36.             int isiHT;
  37.             isiHT = HTArea.size();
  38.             out.println("isiHT: " + isiHT + "<br>");
  39.  
  40.             rs1.close();
  41.             stmt1.close();
  42.             con1.close();
  43.             return HTArea;
  44.     }
  45.     catch (SQLException se) {
  46.             /** just catch */
  47.     }
  48.         catch (java.lang.Exception ex) {
  49.             /** just catch */
  50.     }
  51.         return new Hashtable();
  52.     }    
  53.  
  54.  
If you look at my code, you will see that I have put some lines to check the code. I have no problem with Check 1 and Check 2, but when I see Check 3, I know something was happen. There is nothing in my Hashtable. Did I do something wrong when I try to put ResultSet into my hashtable? Can someone help me? Thank you.


Regards,
Novan Ananda
You are returning a new (empty) hashtable at the end of your method. You should return the hashtable that you were adding stuff to.
Dec 10 '07 #2
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1.             Hashtable HTArea = new Hashtable();
  2.  
  3.             while (rs1.next()) {
  4.                 HTArea.put(new Integer(a), rs1.getString(0));
  5.                 a = a + 1;
  6.             }
  7.  
This throws an exception because column indexes start at one, not zero. At the
end (after the catch) you return an empty hash set.

kind regards,

Jos
Dec 10 '07 #3
BigDaddyLH
1,216 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. catch (SQLException se) {
  2.             /** just catch */
  3. }
  4. catch (java.lang.Exception ex) {
  5.             /** just catch */
  6. }
Maybe it's my inner Buddhist, but I'm always looking for examples where a person's behaviour is also his immediate punishment ;-)
Dec 10 '07 #4
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. catch (SQLException se) {
  2.             /** just catch */
  3. }
  4. catch (java.lang.Exception ex) {
  5.             /** just catch */
  6. }
Maybe it's my inner Buddhist, but I'm always looking for examples where a person's behaviour is also his immediate punishment ;-)
Even atheists only muffle exceptions away for a very good reason and even then:
they document them.

kind regards,

Jos ;-)
Dec 10 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Oberon | last post by:
My HashTable (Global.Games) is a static collection of objects of type Game. A Game object has 8 fields (exposed as properties). The key to the HashTable is also one of these fields (GameID, of type...
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
25
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My...
7
by: ad | last post by:
I have a hash table like: Hashtable myHT = new Hashtable(); myHT.Add("First", "Hello"); myHT.Add("Second", "World"); myHT.Add("Third", "!"); We can use key to find value like: myHT -> "Hello"...
4
by: Eric | last post by:
Hello, I have the following dataset that I want to bind to a repeater to be displayed as a table. Owner Animal Volume --------------------------- Eric Dog 6 Eric Cat ...
5
by: Sky | last post by:
What makes something a valid DataSource? What methods/iterators/etc? Why do I ask? I do understand that a DataSet is based on an XML structure...but it's too table structured for what I am...
4
by: WhiskyRomeo | last post by:
I am trying to implement a webservice that returns a HashTable as a result <WebMethod()> Public Function GetAppData() As Hashtable Dim List As New Hashtable 'omitted code Return List End...
7
by: SevDer | last post by:
Hi We have a static hashtable that is located in another tier in our n-tiered web application. And we are storing big but not huge objects in this hashtable and the key to the objects is...
8
by: Ashish Khandelwal | last post by:
-----See below code, string str = "blair"; string strValue = "ABC"; string str1 = "brainlessness"; string strValue1 = "XYZ"; int hash = str.GetHashCode() ; // Returns 175803953 int hash1 =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.