Results 1 to 8 of 8
  1. #1
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default How do i store an array from database?

    Hey, got a code that gets values from my databse:
    Java Code:
    private void valuesFromDatabase(){
        try {
           String sql = "SELECT group_name FROM Groups WHERE user_name = 'test'";
           pst = conn.prepareStatement(sql);    
           rs = pst.executeQuery(sql);  
           while (rs.next()){
           group_name_values = rs.getString("group_name ");   
           }  
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }   
    }
    This code only stores 1 value from database because 'group_name_values ' is just a String, for example if i have 2 or more group_name's, then i could only use the last one..

    How do i make it so it would store all the data instead of 1? do i need to make an array instead of string? how do i do that?

    If i make Public static String[] group_name_values; , how would i call it in my code?
    Last edited by iNko; 12-10-2012 at 11:40 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: How do i store an array from database?

    List<String>?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: How do i store an array from database?

    cant find any tutorials that would help me with this list<string>, idk how to use it

  4. #4
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: How do i store an array from database?

    Hi,
    Your first question is

    "How do i make it so it would store all the data instead of 1? do i need to make an array instead of string?"


    you use below method the array will be convert as a string:

    a=> is your array name

    StringBuffer result = new StringBuffer();
    for (int i = 0; i < a.length; i++) {
    result.append( a[i]+ "," );
    //result.append( optional separator );
    }
    String mynewstring = result.toString();

    you store this string in to your database


    After that u retrive this string from database and convert string as a array that's like below

    String[] arr=mynewstring .split(",");
    System.out.println("Array :"+arr.length);
    for(int i=0;i<arr.length;i++)
    {
    System.out.println("array"+i+" :"+arr[i]);
    }

    I hop u need this.

    Thanks,

  5. #5
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: How do i store an array from database?

    Created new arraylist:
    Java Code:
    public static ArrayList list = new ArrayList();
    Put all values into the list:
    Java Code:
    private void valuesFromDatabase(){
        try {
           String sql = "SELECT group_name FROM Groups WHERE user_name = 'test'";
           pst = conn.prepareStatement(sql);    
           rs = pst.executeQuery(sql);  
           while (rs.next()){
          list.add(rs.getString("group_name "));   
           }  
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }   
    }
    It stores values but idk how to call all of them at the same time..
    If i write
    System.out.println(list.get(0); - it gives me the 1st value
    If i write
    System.out.println(list.get(1) - it gives me the 2nd value...

    How do i make it so it would give me all the values?

    Tried this:
    Java Code:
    private void valuesFromDatabase(){
        try {
           String sql = "SELECT group_name FROM Groups WHERE user_name = 'test'";
           pst = conn.prepareStatement(sql);    
           rs = pst.executeQuery(sql);  
           while (rs.next()){
    list.add(rs.getString("group_name ")); 
    for (int i=0; i < list.size(); i++) {
      String s = (String) list.get(i);
    }
          
           }  
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }   
    }
    then calling with this:
    Java Code:
    System.out.println(list.get(s)
    but didnt work

  6. #6
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: How do i store an array from database?

    yes this code didn't work because u store reading values from arraylist directly into string that is wrong.

    So first u store that values into stringbuffer use append method after that u covert stringbuffer into string
    i was already give that code above. use it..

  7. #7
    iNko is offline Member
    Join Date
    Dec 2012
    Posts
    55
    Rep Power
    0

    Default Re: How do i store an array from database?

    it works now, thx

  8. #8
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

Similar Threads

  1. how to store images in access database
    By sridarshan in forum JDBC
    Replies: 11
    Last Post: 06-26-2010, 02:26 AM
  2. What would be the best way to store a large Database with J2me
    By father jack hackett in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-08-2010, 06:08 PM
  3. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  4. How to store Multiple calendars in PIM Database
    By thirupathik in forum CDC and Personal Profile
    Replies: 0
    Last Post: 07-13-2008, 01:45 PM
  5. How to store extracted xml values in a database?
    By palanikumark in forum Advanced Java
    Replies: 6
    Last Post: 05-30-2008, 12:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •