1

In my web service I'm making a query to a database, and I would like to return 2 columns of the database and put these columns in a 2d array.

Also I would like to convert the array to JSON and send it to the client. The client using gson parses the message from the server in a 2d array. Is it possible?

I have tried a lot but no luck till now. Thank you in advance.

The last version i've tried is this:

private static String[][] db_load_mes (String u){
 ArrayList<String> array1 = new ArrayList<String>();
 ArrayList<String> array2 = new ArrayList<String>();
 JSONObject messages = new JSONObject();
 Connection c = null;
    try{
    // Load the driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    c = DriverManager.getConnection("jdbc:odbc:dsn1","mymsg","mymsg");
    Statement s = c.createStatement(); 

    // SQL code:   
    ResultSet r;
    r = s.executeQuery("select * from accounts ");

    int i = 0, j = 0;
    int k = 0;
    String x,y;
     while(r.next()) {
        x = r.getString("username");
        array1.add(x);
        y = r.getString("password");
        array2.add(y);

        k = k + 1;
        }

     int count = array1.size();
     String[][] row = new String[count][2];
        Iterator<String> iter = array1.iterator();
        while (iter.hasNext()) {
            row[i][0]=iter.next();  
            i++;
        }
        Iterator<String> iter2 = array2.iterator();
        while (iter2.hasNext()) {
            row[j][1]=iter2.next();     
            j++;
        }
        for(int z=0;z<count;z++)
        System.out.println(row[z][0] + "\t" + row[z][1] + "\n");


     if (k == 0)
                System.err.println("no accounts!");

     c.close();
     s.close(); 


     }
     catch(SQLException se)
     {
     System.err.println(se);
     } catch (ClassNotFoundException e) {
        e.printStackTrace();
     }

    return ...;
    }

With the above code I can create the 2d array but how can I send this array to the client.

5
  • Ragarding the datbase my last test was using arrayList, but it din;t worked. How can I put two columns of my database in a 2d array. And I don't know how can I convert a multidimensional array in JSON. Commented Oct 2, 2011 at 0:57
  • Show us what you have tried. How to Ask Commented Oct 2, 2011 at 0:59
  • I have based on this stackoverflow.com/questions/5010930/…. Commented Oct 2, 2011 at 1:15
  • Show us your version of it that is NOT working please, we cant do anything with "I have tried a lot but no luck til now." What doesn't work exactly?! Commented Oct 2, 2011 at 2:37
  • I've edited my question. I don't have errors in my code I am just seeking a way to implement what I want (generate a 2d array from my database, convert this array using JSON and send it to the client.) Commented Oct 2, 2011 at 13:33

1 Answer 1

3

Here is how I made it using Gson from google... Download gson from here include it in your project.

package javaapplication1;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JavaApplication1 {

    public static void main(String[] args) {

        int rows = 3;
        String records[][] = new String[][]{{"bob", "123-pass"},
        {"erika", "abc123"},
        {"richard", "123123123"}
        };

        Gson gson = new Gson();
        String recordsSerialized = gson.toJson(records);

        System.out.println(recordsSerialized);

        /* prints this        
         [["bob","123-pass"],["erika","abc123"],["richard","123123123"]]        
         */

        // if you want a better output import com.google.gson.GsonBuilder;
        Gson gsonPretty = new GsonBuilder().setPrettyPrinting().create();
        String recordsSerializedPretty = gsonPretty.toJson(records);
        System.out.println(recordsSerializedPretty);
            /* PRINTS  IN different lines.. I can't paste it here */

        // for retrieval
        String retrievedArray[][] =    gsonPretty.fromJson(recordsSerializedPretty, String[][].class);

        for (int i = 0; i < retrievedArray.length; i++) {
            for (int j = 0; j < retrievedArray[0].length; j++) {
                System.out.print(retrievedArray[i][j]+" ");
            }
            System.out.println("");
        }
        // PRINTS THIS

        /*
        bob 123-pass 
        erika abc123 
        richard 123123123 
        */
    }

}
Sign up to request clarification or add additional context in comments.

Comments

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.