By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

Arrays to DB

P: 3
Hi! Im trying to get an array into a db and later on retrieving it.
Im not sure if serialization is the way to go since I dont know how to do the first one.

************************************************** *****************************************

public void storeMemDB(String state, String cEmail, String cSms) {
try {
Class.forName("com.mysql.jdbc.Driver"); // anvnder denna databas anslutning
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/blabla",
"root", "blabla");
Statement s = con.createStatement();
String sql = "INSERT INTO member (memFname,memSname,memUname,memPass," +
"memEmail,memLocality,memGym,memCell,memWeight,mem Pulse,memFysic,wayOfContact)" +
"VALUES ('" +
this.fnamn + "','" +
this.enamn + "','" +
this.anamn + "','" +
this.losen + "','" +
this.email + "','" +
this.ort + "','" +
this.gym + "','" +
this.nummer + "','" +
this.vikt.add(getWeight()) + "','" +
this.puls.add(getHeartrate()) + "','" +
state + "','" +
this.contact.add(cEmail + cSms) + "')" ;
s.executeUpdate(sql);

}catch (ClassNotFoundException e1) {
System.out.println(e1.toString());
} catch (SQLException e2) {

System.out.println(e2.toString());
} catch (Exception e3) {
// Andra avbrott
System.out.println(e3.toString());
}

}
************************************************** ************************************
How should the db look like? Is it a blob , a varchar...? what?

Im new to this and would preciate any help what so ever.
TANX a lot.
/Racoon.
Dec 11 '06 #1
Share this Question
Share on Google+
2 Replies


P: 26
Hi,

you can use serialization. This post on java's forum might be useful:

http://forum.java.sun.com/thread.jspa?threadID=721459&tstart=210

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.util.Arrays;
  8.  
  9. public class ForumQuestion {
  10.     public static void main( String[] args ) throws ClassNotFoundException, IOException {
  11.         // knock up an array for testing
  12.         int[] array = new int[25];
  13.  
  14.         for( int i = 0; i < array.length; i++ ) {
  15.             array[i] = i;
  16.         }
  17.  
  18.         // SERIALIZING
  19.  
  20.         // convert array something you can use as a BLOB
  21.         ByteArrayOutputStream os = new ByteArrayOutputStream();
  22.         ObjectOutputStream out = new ObjectOutputStream( os );
  23.         out.writeObject( array );
  24.  
  25.         byte[] blob = os.toByteArray();
  26.  
  27.         // store 'blob' in your database
  28.         writeBlob( blob );
  29.  
  30.         // DESERIALIZING
  31.  
  32.         // read the serialized array
  33.         byte[] serialized = readBlob();
  34.  
  35.         ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( serialized ) );
  36.  
  37.         int[] myArray = (int[])in.readObject();
  38.  
  39.         System.out.println( Arrays.toString( myArray ) );
  40.     }
  41.  
  42.     // my 'database' here is just a variable, you'll store it in MySQL or whatever
  43.     private static byte[] blob;
  44.  
  45.     private static void writeBlob( byte[] serialized ) {
  46.         blob = serialized;
  47.     }
  48.  
  49.     private static byte[] readBlob() {
  50.         return blob;
  51.     }
  52. }
  53.  
  54.  
Dec 12 '06 #2

P: 3
Ok tanx Ill try that.
Dec 12 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.