-2

I am using the Java Mysql connector and I have a problem with extracting results:

This is the query and prepared statement that needed to be executed:

String statement1 = " select sentence from sentences ; ";
PreparedStatement preparedstatement = conn.prepareStatement(statement1);
ResultSet res = preparedstatement.executeQuery();

Now I try to get the results in an array, so I use:

Array temp = res.getArray("sentence"); //(1)
String[] temp2 = (String[])temp.getArray();      //(2)

(1) returns a mysql.array which contains a locator, which is a logical pointer to the SQL ARRAY on the server, and (2) should do a typecast of temp and return a String[], however I am getting this error

java.sql.SQLFeatureNotSupportedException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:374)
    at com.mysql.jdbc.SQLError.notImplemented(SQLError.java:1342)
    at com.mysql.jdbc.ResultSetImpl.getArray(ResultSetImpl.java:1226)
    at com.mysql.jdbc.ResultSetImpl.getArray(ResultSetImpl.java:1243)
    at wikiparse.Wikiparse3.running(Wikiparse3.java:62) // this where I wrote (1) in my java file
    at wikiparse.ActivateWikiparse3.main(ActivateWikiparse3.java:27)

Any ideas?

14
  • I think after exection of preparedstatement.executeQuery(), the data is already on your side, rather than server side. Commented Jan 14, 2014 at 8:34
  • What is table sentences declaration? Commented Jan 14, 2014 at 8:40
  • 1
    @Rugal In the following statement, the ResultSet method getArray returns the value stored in the column ZIPS of the current row as the java.sql.Array object z: Array z = rs.getArray("ZIPS"); The variable z contains a locator, which is a logical pointer to the SQL ARRAY on the server; it does not contain the elements of the ARRAY itself. Being a logical pointer, z can be used to manipulate the array on the server. Commented Jan 14, 2014 at 8:44
  • 1
    This question appears to be off-topic because the documentation the OP links to in the question explains in the first sentence why this will not work with MySQL. Commented Jan 14, 2014 at 8:47
  • 1
    @kolonel can you see the poster use postgres rather than mysql? Commented Jan 14, 2014 at 8:47

4 Answers 4

2

You should iterate througth the ResultSet:

List<String> temp = new ArrayList<String>();
String statement1 = " select sentence from sentences  ";
PreparedStatement preparedstatement = conn.prepareStatement(statement1);
try
{
    ResultSet res = preparedstatement.executeQuery();

    while (res.next())
    {
       temp.add(res.getString("sentence"));
    }
}
finally{
    preparedstatement.close();
}
String[] temp2 = temp.toArray(new String[0]);
Sign up to request clarification or add additional context in comments.

5 Comments

ArrayList doesn't implement or extend Array. Did you compile your code?
This literally makes no sense.
Excuse-me, but.... why? This code gets all the values from a database table column and stores them in a memory array...
Array temp = new ArrayList(); (As ADS pointed out) ? Please, explain. (I think this is just a typo)
I went ahead and fixed it ;)
2

MySQL doesn't have an ARRAY type, so this method is not implemented.

See http://forums.mysql.com/read.php?39,160525,160573

5 Comments

@kolonel Yes, and the very first sentence says : "Note: MySQL and Java DB currently do not support the ARRAY SQL data type" ... not to mention because of that you couldn't possibly have that in your table definition.
@BrianRoach but it says this In the following statement, the ResultSet method getArray returns the value stored in the column ZIPS of the current row as the java.sql.Array object z: Array z = rs.getArray("ZIPS"); The variable z contains a locator, which is a logical pointer to the SQL ARRAY on the server; it does not contain the elements of the ARRAY itself. Being a logical pointer, z can be used to manipulate the array on the server.
@BrianRoach There is also this post http://stackoverflow.com/questions/14935016/convert-a-result-set-from-sql-array-to-array-of-strings
Apparently you also don't understand that MySQL and PostgreSQL are completely different databases with different feature sets.
0

There is another tutorial which shows how to retrieve data from ResultSet.

In your case it will be

List<String> result = new LinkedList<>();
while (while (rs.next()) {
    result.add(rs.getString(1));
}

Comments

0

Based on the extensive comments, you seem to have a fundamental misunderstanding of how a database works, and what the ARRAY type would mean even if MySQL supported it (which it does not).

well I want to iterate over the values in the ResultSet object , I could do that using the getString() method but then I was trying to see if there was a direct way to do it.

When you query a database, it returns a set of rows from your table that match the query. The JDBC represents this as a ResultSet. To get all the results for a given column, yes, you have to iterate through the ResultSet:

List<String> myList = new ArrayList<String>();
while (res.next()) {
    myList.add(res.getString("sentence"));
} 

There's no shortcut method to have the ResultSet do this for you and return a List

Edit to add: The ARRAY type in PostgreSQL (and possibly others) is an entirely different beast. It allows you to store an array of data in a single row and retrieve it that way. That's what the getArray() and the associated Array class is for.

5 Comments

the extensive comments are because I am trying to understand something , I was trying to see if something could be done in a certain way. The code in your answer is what I already have.
ESP is on the fritz. That little piece of information would have improved your question dramatically; we would have at least immediately understood your confusion. To be clear, you're confusing a datatype (and the methods/classes to access it) available in a database that is not MySQL with a data structure (ResultSet) in Java.
You need to add a throws clause in your code because unless you do it wont work. It seems you have a fundamental misunderstanding on how the ResultSet class works.
Troll harder. That was weak.
I'm not a troll, never.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.