Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

inserting session variables into a database

Posted on 2005-03-30
9
Medium Priority
?
194 Views
Last Modified: 2010-04-01
Hi there im trying to insert results into a database using session varibales here is my code:





stmt = connection.createStatement();

String sInsert = "INSERT INTO Test_Results( UserName,Q1Ans, Q2Ans, Q3Ans, Q4Ans, Q5Ans, Q6Ans, Q7Ans, Q8Ans, Q9Ans, Q10Ans,Q11Ans, Q12Ans, Q13Ans, Q14Ans, Q15Ans, Q16Ans, Q17Ans, Q18Ans, Q19Ans, Q20Ans, Total_Comp, Total_MM, Total_ACC, Total_AIC, Total_Co) VALUES ( ' " + session.getValue("UserName") + "' ,' " + session.getValue("Page1Q1") + "' , ' " + session.getValue("Page1Q2") + "',' " + session.getValue("Page1Q3") + "',' " + session.getValue("Page1Q4") + "',' " + session.getValue("Page1Q5") + "',' " + session.getValue("Page2Q6") + "',' " + session.getValue("Page2Q7") + "',' " + session.getValue("Page2Q8") + "',' " + session.getValue("Page2Q9") + "', ' " + session.getValue("Page2Q10") + "', ' "+ session.getValue("Page3Q11)+"', ' "+session.getValue("Page3Q12)+"', ' " + session.getValue("Page3Q13)+"', ' "+ session.getValue("Page3Q14)+"',' "+ session.getValue("Page3Q15)+"',' "+ session.getValue("Page4Q16)+"',' "+ session.getValue("Page4Q17)+"','"+ session.getValue("Page4Q18)+"',' "+ session.getValue("Page4Q19)+"',' "+ session.getValue("Page3Q20)+"', ' " + scores[ 0 ] + " ' , ' " + scores[ 1] + " ' , ' " + scores[ 2 ] + " ' , ' " + scores[ 3 ] + " '  , ' " + scores[ 4 ] + " '  )";

stmt.execute( sInsert );
out.println( "<TT>" + sInsert + "</TT>" );

the error im getting is unclosed character literal. please help thanks!!
0
Comment
Question by:acslater
  • 5
  • 2
  • 2
9 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13662840
As an aside you should be using session.getAttibute(String name) rather than session.getAttibute(String name).

I'll ahve a look..
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13662914
its much better to do it with preparedstatement..
write it in following manner..

PreparedStatement pstmt = connection.prepareStatement("INSERT INTO Test_Results( UserName,Q1Ans, Q2Ans, Q3Ans, Q4Ans, Q5Ans, Q6Ans, Q7Ans, Q8Ans, Q9Ans, Q10Ans,Q11Ans, Q12Ans, Q13Ans, Q14Ans, Q15Ans, Q16Ans, Q17Ans, Q18Ans, Q19Ans, Q20Ans, Total_Comp, Total_MM, Total_ACC, Total_AIC, Total_Co) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);
then
pstmt.setString(1,session.getValue("UserName") );
pstmt.setString(2,session.getValue("Page1Q1") );
pstmt.setString(3,session.getValue("Page1Q2") );
pstmt.setString(4,session.getValue("Page1Q3") );
pstmt.setString(5,session.getValue("Page1Q4") );
pstmt.setString(6,session.getValue("Page1Q5") );
pstmt.setString(7,session.getValue("Page1Q6") );
>>>>>
>>>>
>>>>
pstmt.setString(25,score[3] );
pstmt.setString(26,score[4] );

pstmt.execute();
out.println( "<TT>" + sInsert + "</TT>" );

its much better and a cleaner way of doing JDBC calls
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13662919
session.getValue("Page4Q16) is missing the second " after the value

should be

session.getValue("Page4Q16")
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Accepted Solution

by:
bloodredsun earned 750 total points
ID: 13662953
This should work:

String sInsert = "INSERT INTO Test_Results( UserName,Q1Ans, Q2Ans, Q3Ans, Q4Ans, Q5Ans, Q6Ans, Q7Ans, Q8Ans, Q9Ans, Q10Ans,Q11Ans, Q12Ans, Q13Ans, Q14Ans, Q15Ans, Q16Ans, Q17Ans, Q18Ans, Q19Ans, Q20Ans, Total_Comp, Total_MM, Total_ACC, Total_AIC, Total_Co) VALUES ( '" + session.getAttribute("UserName") + "' ,'" + session.getAttribute("Page1Q1") + "' , '" + session.getAttribute("Page1Q2") + "', '" + session.getAttribute("Page1Q3") + "','" + session.getAttribute("Page1Q4") + "',' " + session.getAttribute("Page1Q5") + "',' " + session.getAttribute("Page2Q6") + "',' " + session.getAttribute("Page2Q7") + "',' " + session.getAttribute("Page2Q8") + "',' " + session.getAttribute("Page2Q9") + "', ' " + session.getAttribute("Page2Q10") + "', ' "+ session.getAttribute("Page3Q11")+ "', '" + session.getAttribute("Page3Q12")+"', ' " + session.getAttribute("Page3Q13") + "', '" + session.getAttribute("Page3Q14")+"',' "+ session.getAttribute("Page3Q15") + "',' "+ session.getAttribute("Page4Q16")+"',' "+ session.getAttribute("Page4Q17")+"','"+ session.getAttribute("Page4Q18")+"',' "+ session.getAttribute("Page4Q19")+"',' "+ session.getAttribute("Page3Q20")+"', ' " + scores[ 0 ] + " ' , ' " + scores[ 1] + " ' , ' " + scores[ 2 ] + " ' , ' " + scores[ 3 ] + " '  , ' " + scores[ 4 ] + " '  )";
            
0
 

Author Comment

by:acslater
ID: 13663088
thanks i was missing  loads of the inverted commas in("Page3Q15") thanks for your help it would have took me ages
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13663169
Thanks :-)
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13663198
..but can I ask why only a B grade? EE grades are different than you might think and it's important to us experts http://www.experts-exchange.com/help.jsp#hi55
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 13663344
:-) that way I can also ask why my solution was not acceptable....:-)

( Just kidding)
0
 

Author Comment

by:acslater
ID: 13680087
it was exceptable but bloodredsun fixed the problem i asked.sorry il give you better grades from now on
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As a matter of fact, Outlook OST files are of much importance in relation to Exchange mailbox. OST files are independent as they are simply copy of data of a user’s mailbox on Exchange Server. Though, if the server’s status is changed or it is dama…
The article explains the process to deploy a Self-Service password reset portal I developed a few years ago. Hopefully, it will prove useful to someone.  Any comments, bug reports etc. are welcome...
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

578 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