Solved

How would you write this database method for sending and [ ] [ ] back

Posted on 2011-09-28
10
193 Views
Last Modified: 2012-05-12
public String[][] getTheHistory(String flightNo, String date)
  {
    // Columns = {"Bokningsnummer", "Flightnummer", "Date", "PersonId", "FirstName", "Lastname", "Type", "Help", "SeatNr", "Price"};
    String [][] temp = new String[1][10];
    temp[0][0] = "123";
    temp[0][1] = "LK202";
    temp[0][2] = "2010-01-01";
    temp[0][3] = "1";
    temp[0][4] = "Anders";
    temp[0][5] = "Paulsson";
    temp[0][6] = "M";
    temp[0][7] = "Y";
    temp[0][8] = "35";
    temp[0][9] = "Black";

    return temp;
  }


I cant really spot how to write it.
0
Comment
Question by:Mickeys
  • 10
10 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36719250
insert into table_name (Bokningsnummer, Flightnummer, Date, PersonId, FirstName, Lastname, Type, Help, SeatNr, Price) values
 (
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36719252
Sorry for that - I din'mean to post it
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36719265
Mickeys,

One importantpoint is the waty you write inset staetment
depends not on bhowe looks your data but on how the fields
are defined in the database - therefiore the contebnts of your array do n not tell use how to insert it into
the tabel
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36719269
You need to go to the databse and say

desc table_name

then it will print you which tables are varchar, which are numbers
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36719279
Even thought you have temp[0][0] = "123";
it does not tell us if youe column
Bokningnummer
is defined as varchar2 or as number
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36719292
In the sql statments all strings should be enclosed in single quotes
the numbers should not have enclosing single quotes, thetefor if you Bokningsnummer
is defined at table creation as number, then you need to have in input string , just

...,123, ...

if it is defined as varchar or varchr2  then you have to have it

....,'123',...

and this is very important - otherwise you'll get eerriors
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36719381
Please type
desc table_name
and post the printout

(and I hope desc table_name should work on MySQL also)
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36719621


Bokningsnummer  number,
 Flightnummer  varchar, 
Date date,
 PersonId number,
 FirstName varchar,
 Lastname varchar,
 Type varchar, 
Help varchar,
 SeatNr varchar,
 Price number

Open in new window


then you'll have it in java:
ststement.executeUpdate("insert into table_name (Bokningsnummer, Flightnummer, Date, PersonId, FirstName, Lastname, Type, Help, SeatNr, Price) values
(" +  temp[0][0] + ",'" + temp[0][1] + "','" +  temp[0][2] + "'," +  temp[0][3] + ",'" +  temp[0][4] + "','" +  temp[0][5] + "','" +  temp[0][6] + "','" +  temp[0][7] +
"','" +  temp[0][8] + "'," +  temp[0][9] + ")");

Open in new window


The date is in the default format so you put it  in the same format as a string with signgle quotes

I can't understand how Price can correspiond to temp[0][9] = "Black" - so check on that


0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36719628
But the main thing to rememeber - the syntax depednes not on your java types and values but on how each filed is
declared in the databse when the table was created
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36719811
If you expect you'll be having a lot of these interactions between java and database back and forth you may think
about using in future Hibernate which is designed to ensure persistence of java objects in relational databases.

Still it definitely makes sense at least to have some experience with doing it in stariohtforward way
through JDBC - without  at least basic knowledge of that, it is not so easy to understand the stuff about Hibernate

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now