combo box in JavaScript and store the combo box values into database

i am a developer, i have a task ie.. i have combo box in JavaScript and i have to store the combo box values into database through JavaServerPage.. i please every one to have a look on this and please reply soon....
[229 byte] By [senthil_yogaa] at [2007-11-27 2:40:01]
# 1
HiUse request.getParameterValues("comboboxname") method to get the selected values in the combo boxI Hope this helps you
bachea at 2007-7-12 3:02:33 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Dear sir,

By your information, i am able to retrieve but not able to store into database ,

i will show you the codings, please give your valuable suggestions

<%

String s1=(String)session.getAttribute("eid");

String s3[]=request.getParameterValues("language");

int j=0;

try {

for (int i = 0; i < s3.length; i++)

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:langskill");

PreparedStatement pStmt = con.prepareStatement("update lang set(language) values (?) where eid=? ");

// pStmt.setString(2, s3);

pStmt.setString(1, s1);

pStmt.execute();

}

}

catch(Exception e)

{

System.out.println(e);

}

%>

<%

if(j>0)

{

%>

datas are updated

<%

}

else

{

%>

datas are not updated

<%

}

%>

senthil_yogaa at 2007-7-12 3:02:33 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Please use code tags ( select all relevant text and click the code button on top of the message window while posting ) it makes things easier for everyone.

What exactly is the problem? What exception are you getting? Please post the stack trace.

As far as I can tell, you're not setting the second placeholder in your preparedStatement and also, is your SQL query for update correct? I'm not sure about the syntax but it seems to be wrong to me.

nogoodatcodinga at 2007-7-12 3:02:33 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

thank you a lot sir for ur valuable suggestions,

i have changed my codings but still i am not able to store, i will show the

codings, please give ur valuable suggestions soon as possible sir !!!!

<%

String s1=(String)session.getAttribute("eid");

String s3[]=request.getParameterValues("language");

int j=0;

//int i=0;

try {

for (int i = 0; i < s3.length; i++)

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:langskill");

PreparedStatement pStmt = con.prepareStatement("update lang set language='" + s3[i] + "' where eid=" + s1+"");

pStmt.setString(1, s1);

pStmt.executeUpdate();

}

}

catch(Exception e)

{

System.out.println(e);

}

%>

<%

if(j>0)

{

%>

<%=(String)session.getAttribute("ename")%>

datas are updated

<%

}

else

{

%>

<%=(String)session.getAttribute("ename")%>

datas are not updated

<%

}

%>

senthil_yogaa at 2007-7-12 3:02:33 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

This still isn't correct. You're not using the PreparedStatement correctly.

It should be something like this:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:langskill");

PreparedStatement pStmt = con.prepareStatement("update lang set language= ? where eid=" + s1+"");

for (int i = 0; i < s3.length; i++)

{

pStmt.setString(1, s3[i]);

pStmt.executeUpdate();

}

Also, you should be closing your connections and statements, preferably in the finally block.

This is only guess work because you still have NOT posted your stack trace. If you're not going to tell us what exceptions you're getting, we can't help you out much till we run the code ourselves! So give the exceptions next time!

You've put

catch(Exception e)

{

System.out.println(e);

}

right? So why won't you put up what it's printing out?

nogoodatcodinga at 2007-7-12 3:02:33 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Thank you for your valuable suggestions.....

i have applied as you said , its not showing any error but the datas

are not updated and i am getting the message in the next jsp page as not updated...

i will show you the codings as what i have did

<%

String s1=(String)session.getAttribute("eid");

String s3[]=request.getParameterValues("language");

int j=0;

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:langskill");

PreparedStatement pStmt = con.prepareStatement("update lang set language= ? where eid=" + s1+"");

for (int i = 0; i < s3.length; i++)

{

pStmt.setString(1, s3[i]);

pStmt.executeUpdate();

}

}

catch(Exception e)

{

System.out.println(e);

}

%>

<%

if(j>0)

{

%>

<%=(String)session.getAttribute("ename")%>

datas are updated

<%

}

else

{

%>

<%=(String)session.getAttribute("ename")%>

datas are not updated

<%

}

%>

senthil_yogaa at 2007-7-12 3:02:33 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Dear sir,Now its working out but the problem is ,if i select 5 datas , only the first one is getting updated....what shoukl i do inorder to update the datas in array
senthil_yogaa at 2007-7-12 3:02:33 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

>are not updated and i am getting the message in the next jsp page as not updated...

Obviously you'll get the message as not updated, you're not changing the value of 'j'. You initialize it to 0 and then check whether or not it's zero. It's value is not being modified anywhere.

And are you sure you're not getting any exceptions? With System.out.println(), they'll get printed in your server logs. With Tomcat, it'll be in stdout.txt under the logs folder.

And if you really aren't getting any errors, then there is no record in your database that has the value of eid that s1 has.

Also, you're getting 'language' from a combo-box right? Is it multiple select? Or a drop-down? Because if it's a drop down, there is no point in using getParameterValues(), you'll get a single value only. So you might as well use getParameter().

And secondly, if you are getting multiple values, what is the point of the SQL update? You'll just overwrite the language attribute for eid = s1 multiple times and will always end up with just one value, the absolute last one that you get.

Your code has more than one problems. I think you need to rethink what you're trying to do and also maybe brush up on JDBC basics.

nogoodatcodinga at 2007-7-12 3:02:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

[nobr]dear sir,

your suggestions are really greater the god.............

i have applied as you said , now i am the page as updated and also i nform you that its multi select ....

i will show the codings , then u will get a clear identification

<script language= "JavaScript">

<!--

function one2two() {

m1len = m1.length ;

for ( i=0; i<m1len ; i++){

if (m1.options[i].selected == true ) {

m2len = m2.length;

m2.options[m2len]= new Option(m1.options[i].text);

}

}

for ( i = (m1len -1); i>=0; i--){

if (m1.options[i].selected == true ) {

m1.options[i] = null;

}

}

}

function two2one() {

m2len = m2.length ;

for ( i=0; i<m2len ; i++){

if (m2.options[i].selected == true ) {

m1len = m1.length;

m1.options[m1len]= new Option(m2.options[i].text);

}

}

for ( i=(m2len-1); i>=0; i--) {

if (m2.options[i].selected == true ) {

m2.options[i] = null;

}

}

}

//-->

</script>

<form method="POST" name="theForm" action="update.jsp">

<table bgcolor="white" border="1" cellpadding="5" cellspacing="2" align="center">

<tr><td align="center">

<select id=menu1 size=10 multiple>

<option>javascript</option>

<option>php</option>

<option>Zeo</option>

<option>asp</option>

<option>jsp</option>

<option>ajax</option>

<option>struts</option>

</select><br />

<p align="center"><input type="button" onClick="one2two()" value=" >> ">

</td><td align="center">

Languages you know:<BR>

<SELECT NAME="language" multiple>

<OPTION VALUE="c">C

<OPTION VALUE="c++">C++

</SELECT>

<p align="center"><input type="button" onClick="two2one()" value=" << " >

</td></tr></table>

<center><input type="submit" value="update"></center>

</form>

<a href="valid.jsp"><h4><u>Back<h4></a>

<script language= "JavaScript">

var m1 = document.theForm.menu1;

var m2 = document.theForm.language;

</script>

</body>

</html>

[/nobr]

senthil_yogaa at 2007-7-12 3:02:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

Dear sir,

can you please help me in solving this , please sir........

How to select a particular data from database .........

i will show you the codings .....please refer it

<%@page import="java.sql.*" %>

<%@page import="java.io.*" %>

<%

String s2=request.getParameter("language");

%>

<%

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:langskill");

PreparedStatement pr=con.prepareStatement("select eid from lang where language =?");

pr.setString(2,s2);

// pr.setString(1,eid);

ResultSet rs=pr.executeQuery();

if(rs.next())

{

out.println("the employees are "+eid);

}

}

catch(Exception e)

{

System.out.println(e);

}

//rs.close();

%>

not found

<a href="search.jsp"><h4>Back</h4></a>

</body>

</html>

senthil_yogaa at 2007-7-12 3:02:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

senthil_yoga, you really need to read up on JDBC first.

You don't seem to know where and when to use Statement and/or PreparedStatement.

And you seem to posting ( and double-posting! ) after every single step of making your application! ( http://forum.java.sun.com/thread.jspa?threadID=5171211&tstart=15, http://forum.java.sun.com/thread.jspa?threadID=5166401&tstart=0 ). I dont' think anyone's gonna have the patience to hold you hand and spoon-feed you every step of the way. And it is particularly frustrating to see the same question posted twice in two different topics. Please stick to just one.

PreparedStatements should be used when you're going to be running the same query with different parameters, its more efficient. For example, the update queries you had questioned about before.

In this case, you're running a select query. and you're running it once. What's the point of using PreparedStatement? You should be using a simple Statement. Please, please read up on JDBC first; because most obviously have not yet. In fact, you just seem to be trying your hand at it and it seems like you want a tutorial on these forums.

I, for one, will not be answering your queries till you ask something a little less basic! :)

Cheers.

nogoodatcodinga at 2007-7-12 3:02:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12

Dear sir,

i able to update only a single data , but i am sending only through array (pStmt.setString(1, s3);) ,

but still other data's are not updating.....

i will show u the code ...please provide ur valuable suggestionss

<%@page import="java.lang.*" %>

<%@page import="java.sql.*" %>

<%@page import="java.io.*" %>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body >

<center><h4><u> Employee Assessment Application</u></h4></center>

<%

String s1=(String)session.getAttribute("eid");

String s3[]=request.getParameterValues("language");

int i=0;

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:langskill");

PreparedStatement pStmt = con.prepareStatement("update lang set language=? where eid=" + s1+"");

for ( i = 0; i < s3.length; i++)

{

pStmt.setString(1, s3[i]);

pStmt.executeUpdate();

out.println(s3[i]);

}

}

catch(Exception e)

{

System.out.println(e);

}

%>

<%

if(i>0)

{

%>

<%=(String)session.getAttribute("ename")%>

datas are updated

<%

}

else

{

%>

<%=(String)session.getAttribute("ename")%>

datas are not updated

<%

}

%>

senthil_yogaa at 2007-7-12 3:02:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13

Look, your SQL query is wrong for what you want to do.

It is very basic stuff but you don't seem to know either SQL or the java.sql package. Read up on both.

If the value of eid is "myEid" and you array s3 holds {"1", "2", "3"};

your first query will update the table for eid = myEid and set language = 1

eid| language

-+

myEid| 1

The next query updates the table, again for eid = myEid and sets language = 2, overwriting the last value

eid| language

-+

myEid| 2

The third query in the loop once again overwrites this with 3

eid| language

-+

myEid| 3

Do you understand that much? I think you need to change your query to 'insert into...' instead of 'update...' because update will change values of existing data.

If you still do not get it, then I seriously suggest you stop posting on the forum, get one book each of SQL and JDBC and read them first.

nogoodatcodinga at 2007-7-12 3:02:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14

Dear sir,

thank u for spending ur precious moments for me.....

how can i use insert query for a particular employee....

i think ,only thru update we update data for a particular employee

please refer this, and help me sir......

<%

String s1=(String)session.getAttribute("eid");

String s3[]=request.getParameterValues("language");

int i=0;

//int j=0;

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:langskill");

PreparedStatement pStmt = con.prepareStatement("insert into lang(language) values(?) ");

pStmt.setString(1, s3[i]);

pStmt.executeUpdate();

}

catch(Exception e)

{

System.out.println(e);

}

%>

senthil_yogaa at 2007-7-12 3:02:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...