Solved

Reading a ResultStatement(Oracle Database) into an array and then returning it to method.

Posted on 2003-11-15
7
392 Views
Last Modified: 2011-09-20
I have a program that uses the JDBC-ODBC bridge to read the firstName and lastName from and Oracle database, it saves the first and last names into a string and then saves each full_name into an array.  However, when I return my array it saves the 1st two names as null in the array and only gets the last full_name correctly.  The connection works fine, and it will even do a System.out.println of all the names fine, and while in the while(rs.next()) loop when I check to make sure the names got put in the array...they have.  But, once I'm out of the loop to return my array...the 1st two names are gone.  I also get 3 warnings...but..I don't think they are the cause of the problems,

Nov 15, 2003 8:00:51 PM java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0
x80000002. Windows RegCreateKeyEx(...) returned error code 5.

Nov 15, 2003 8:00:51 PM java.util.prefs.WindowsPreferences WindowsRegOpenKey1
WARNING: Trying to recreate Windows registry node Software\JavaSoft\Prefs at roo
t 0x80000002.

Nov 15, 2003 8:00:51 PM java.util.prefs.WindowsPreferences openKey
WARNING: Could not open windows registry node Software\JavaSoft\Prefs at root 0x
80000002. Windows RegOpenKey(...) returned error code 2.

The method with the problem is (the username and password have been ommitted),
public static String[] loadConnectionandNames()
 {
    try
            { System.out.println("Right before trying to load the driver class...");
                  //String url = "jdbc:odbc:Oracle";
                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                 System.out.println("Loaded the driver class!");
            }
            catch (ClassNotFoundException e)
            {
                  System.out.println("Unable to load Driver Class");
                return null;
            }
            
          try {
System.out.println("Right before trying to make connection");                  
Connection con = DriverManager.getConnection("jdbc:odbc:Oracle", " ", " ");
System.out.println("Created the connection...");
Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT lastName, firstName FROM CUSTOMER");
            while(rs.next()) {

                last_name = rs.getString("lastName");
                System.out.println("Got last Name");
                first_name = rs.getString("firstName");
                System.out.println("Got first Name");
                String cust_full_name = first_name + " " + last_name;
                System.out.println("Customers names = " + cust_full_name);
             
                full_name = new String[max_length];

                full_name[t] = cust_full_name;              
                System.out.println("Customers names = " + full_name[t] + "??? " + t);

                System.out.println("Fullname value " + full_name[t]);
                ++t;
                System.out.println("After t is added one = " + t); //return full_name;
                              }


 for (int z=0; z<t; ++z)
                {//This is where the 1st names are no longer in my array
System.out.println("final loop" + full_name[z] +"???"+t); } return full_name;
                 }
              catch (SQLException se) {
              System.out.println("A SQL Exception occurred." + se.getMessage());
              se.printStackTrace(System.out);

System.out.println("Returning null values for customers"); return null;         }

Sorry for all the System.out.println's, I've been trying to fix this for awhile,  If anyone could help
me it would be very much appreciated...Thanks! :)
0
Comment
Question by:rcm9445
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 150 total points
ID: 9757438
> full_name = new String[max_length];

You recreate your array for every row, should only do this one before the loop.

full_name = new String[max_length];
while(rs.next()) {
...
0
 
LVL 1

Author Comment

by:rcm9445
ID: 9757448
Wow...that was pretty stupid of me :)  Thanks alot!  Do you have any ideas about the warnings?
0
 
LVL 92

Expert Comment

by:objects
ID: 9757506
Not sure on the warning, have never used the new prefs package.
Have a look at this link and see if it helps u:
http://mindprod.com/jgloss/preferences.html
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:rcm9445
ID: 9757564
Well....I'm not using the prefs package either...I decided to import java.util to see if that would help ..and it didn't.  It doesn't come up with the errors until my entire program is finished.  I must just have something set up incorrectly in my java folders.
0
 
LVL 92

Expert Comment

by:objects
ID: 9757566
> I'm not using the prefs package either

Perhaps its a problem with your Java install.
0
 
LVL 1

Author Comment

by:rcm9445
ID: 9757585
Yep...perhaps so...its on the school computer though so...not gonna mess with it :)
Thanks again for you help.
0
 
LVL 92

Expert Comment

by:objects
ID: 9757618
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question