inserting session variables into a database

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!!
acslaterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bloodredsunCommented:
As an aside you should be using session.getAttibute(String name) rather than session.getAttibute(String name).

I'll ahve a look..
0
KuldeepchaturvediCommented:
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
bloodredsunCommented:
session.getValue("Page4Q16) is missing the second " after the value

should be

session.getValue("Page4Q16")
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

bloodredsunCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
acslaterAuthor Commented:
thanks i was missing  loads of the inverted commas in("Page3Q15") thanks for your help it would have took me ages
0
bloodredsunCommented:
Thanks :-)
0
bloodredsunCommented:
..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
KuldeepchaturvediCommented:
:-) that way I can also ask why my solution was not acceptable....:-)

( Just kidding)
0
acslaterAuthor Commented:
it was exceptable but bloodredsun fixed the problem i asked.sorry il give you better grades from now on
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.