473,503 Members | 7,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Arrays to DB

3 New Member
Hi! I´m trying to get an array into a db and later on retrieving it.
I´m 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"); // använder 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?

I´m new to this and would preciate any help what so ever.
TANX a lot.
/Racoon.
Dec 11 '06 #1
2 1423
mharrison
26 New Member
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
Racoon
3 New Member
Ok tanx I´ll try that.
Dec 12 '06 #3

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

Similar topics

19
2821
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
3897
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
29232
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
2820
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
8686
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
4874
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
6
13136
by: Robert Bravery | last post by:
Hi all, Can some one show me how to achieve a cross product of arrays. So that if I had two arrays (could be any number) with three elements in each (once again could be any number) I would get:...
1
2433
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
2508
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
29
35379
weaknessforcats
by: weaknessforcats | last post by:
Arrays Revealed Introduction Arrays are the built-in containers of C and C++. This article assumes the reader has some experiece with arrays and array syntax but is not clear on a )exactly how...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.