?
Solved

inserting session variables into a database

Posted on 2005-03-30
9
Medium Priority
?
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
The well known Cerber ransomware continues to spread this summer through spear phishing email campaigns targeting enterprises. Learn how it easily bypasses traditional defenses - and what you can do to protect your data.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

752 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