?
Solved

How to store ResultSet to an Array?

Posted on 2008-10-21
11
Medium Priority
?
21,607 Views
Last Modified: 2011-04-14
I got this error

Got an exception!
null

import java.sql.*; 
import javax.swing.*;
public class NewClass {
 
    public static void main (String[] args) { 
 
        try { 
            String url = "jdbc:mysql://localhost:3306/test1"; 
            Connection conn = DriverManager.getConnection(url,"root","mypassword"); 
            
            PreparedStatement stmt = conn.prepareStatement("SELECT * FROM inventorytable");
            ResultSet rs; 
            rs=stmt.executeQuery();
  
            while ( rs.next() ) { 
                
                Array itemnameloc = rs.getArray("itemname");
                String [] itemnameArray = (String [])itemnameloc.getArray();
                System.out.println(itemnameArray);
                
            } 
            conn.close(); 
        } catch (Exception e) { 
            System.err.println("Got an exception! "); 
            System.err.println(e.getMessage()); 
        } 
    } 
}

Open in new window

0
Comment
Question by:glynco
[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
  • 4
  • 2
  • +1
11 Comments
 
LVL 6

Expert Comment

by:manuel_m
ID: 22767756
Can you please post the stacktrace where the error occurred?

write in your catch block.
try {
  // your code..
} catch (Exception e) {
   e.printStackTrace();
}

Open in new window

0
 

Author Comment

by:glynco
ID: 22767881
More errors with that.
0
 
LVL 6

Expert Comment

by:manuel_m
ID: 22767938
Please add the e.printStackTrace() method to your catch block and post the error messages that is printed.
Otherwise the "null" error message is not significant enough.
import java.sql.*; 
import javax.swing.*;
public class NewClass {
 
    public static void main (String[] args) { 
 
        try { 
            String url = "jdbc:mysql://localhost:3306/test1"; 
            Connection conn = DriverManager.getConnection(url,"root","mypassword"); 
            
            PreparedStatement stmt = conn.prepareStatement("SELECT * FROM inventorytable");
            ResultSet rs; 
            rs=stmt.executeQuery();
  
            while ( rs.next() ) { 
                
                Array itemnameloc = rs.getArray("itemname");
                String [] itemnameArray = (String [])itemnameloc.getArray();
                System.out.println(itemnameArray);
                
            } 
            conn.close(); 
        } catch (Exception e) { 
            System.err.println("Got an exception! "); 
            System.err.println(e.getMessage()); 
            // prints a detailed error listing
            e.printStackTrace();
        } 
    } 
}

Open in new window

0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:glynco
ID: 22767974
Your code gives more errors.

Got an exception!
null
java.sql.SQLFeatureNotSupportedException
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at java.lang.Class.newInstance0(Class.java:355)
        at java.lang.Class.newInstance(Class.java:308)
        at com.mysql.jdbc.SQLError.notImplemented(SQLError.java:1274)
        at com.mysql.jdbc.ResultSetImpl.getArray(ResultSetImpl.java:1154)
        at com.mysql.jdbc.ResultSetImpl.getArray(ResultSetImpl.java:1171)
        at NewClass.main(NewClass.java:17)
0
 
LVL 6

Expert Comment

by:manuel_m
ID: 22768144
It seems that the line:
Array itemnameloc = rs.getArray("itemname");
causes the error.
Do you have a column in your table named "itemname"?
0
 

Author Comment

by:glynco
ID: 22768191
Yes there is "itemname" column in the table.
0
 
LVL 6

Expert Comment

by:javaexperto
ID: 22768219
what do you want to do exactly? print the column name?  print all the rows of that column?
0
 
LVL 6

Expert Comment

by:manuel_m
ID: 22768224
Could you please check if the line in the error message:
at NewClass.main(NewClass.java:17)

is the same you have posted here ? => Array itemnameloc = rs.getArray("itemname");

If not, please post the line that matches line 17 in your source code in your IDE.
0
 

Expert Comment

by:itbalaji
ID: 22768259
If you think rs.getArray() will return all values of the column in a Array then you misunderstood it. If the underlying data is an array type then it returns the array. Very few databases support Array types. MySQL does not support it yet. You have to do as shown below.
import java.sql.*; 
import javax.swing.*;
import java.util.*;
public class NewClass {
 
    public static void main (String[] args) { 
 
        try { 
            String url = "jdbc:mysql://localhost:3306/test1"; 
            Connection conn = DriverManager.getConnection(url,"root","mypassword"); 
            
            PreparedStatement stmt = conn.prepareStatement("SELECT * FROM inventorytable");
            ResultSet rs; 
            rs=stmt.executeQuery();
            ArrayList list = new ArrayList();
  
            while ( rs.next() ) { 
                
                list.add(rs.getString("itemname"));
            } 
            String [] itemnameArray = new String [list.size()];
            list.toArray(itemnameArray);
            System.out.println(itemnameArray);
            conn.close(); 
        } catch (Exception e) { 
            System.err.println("Got an exception! "); 
            System.err.println(e.getMessage()); 
            // prints a detailed error listing
            e.printStackTrace();
        } 
    } 
}

Open in new window

0
 

Author Comment

by:glynco
ID: 22772610
I got this error with that code

Note: C:\Documents and Settings\ALCO\My Documents\NetBeansProjects\JavaApplication59\src\NewClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
0
 
LVL 6

Accepted Solution

by:
javaexperto earned 2000 total points
ID: 22772759
well that's not an error, just a warning, try this:
import java.sql.*; 
import javax.swing.*;
import java.util.*;
public class NewClass {
 
    public static void main (String[] args) { 
 
        try { 
            String url = "jdbc:mysql://localhost:3306/test1"; 
            Connection conn = DriverManager.getConnection(url,"root","mypassword"); 
            
            PreparedStatement stmt = conn.prepareStatement("SELECT * FROM inventorytable");
            ResultSet rs; 
            rs=stmt.executeQuery();
            ArrayList<String> list = new ArrayList<String>();
  
            while ( rs.next() ) { 
                
                list.add(rs.getString("itemname"));
            } 
            String [] itemnameArray = new String [list.size()];
            list.toArray(itemnameArray);
            System.out.println(itemnameArray);
            conn.close(); 
        } catch (Exception e) { 
            System.err.println("Got an exception! "); 
            System.err.println(e.getMessage()); 
            // prints a detailed error listing
            e.printStackTrace();
        } 
    } 
}

Open in new window

0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

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…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

770 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