database update Combobox

C

Clive_

Hi,

I need to update a combobox or list from a databse.
There needs to be a dynamic relationship between the database
cell and the selection.

The combobox code is easy:
<td> <select name="cboWFMasPointTenth"
onChange="TestWFMasPointTenthfunction(this.value)">
<option value="1">Yes</option>
<option value="0">No</option>
<option value="-1">-</option>
</td>

reading the database is:
<td> <select name="cboWFMasPointTenth"
onChange="TestWFMasPointTenthfunction(this.value)">
<option value=<%= rs.getInt(1)%> > </option>
</td>

The list maybe 1, 0, -1

The selected item needs to be set to the value in the database.
I cannot work out how to do this.

Could anyone help????

Thanks

Clive
 
D

derek

Where is the rest of your code? Where are you reading from a database?
I only see html.
 
C

Clive_

To the OP:
<http://www.physci.org/codes/sscce.html>
explains how to put together an example.

Hi,

I connect to a database and can get a combobox to read the cell value.
Did not put in all the code for space. It is standard connection code.

<%
String connectionUrl =("jdbc:jtds:sqlserver://localhost:1032/");
String dbName="Calibration";
String driver="net.sourceforge.jtds.jdbc.Driver";
String user="sa";
String password="admin";

// Establish the connection.
Class.forName(driver).newInstance();
con = DriverManager.getConnection(connectionUrl
+dbName,user,password);
stmt = con.createStatement() ;
rs = stmt.executeQuery(querySQL) ;

ResultSetMetaData rsmd = rs.getMetaData() ;

for(int i = 1 ; i <= rsmd.getColumnCount() ; i++) {
%>

<% } %>


</tr>

<% while(rs.next()) { %>
<tr>
<td> <select name="cboWFMasPointTenth"
onChange="TestWFMasPointTenthfunction(this.value)">
<option value=<%= rs.getInt(3)%> > </option>
<option value="1">Yes</option>
<option value="0">No</option>
<option value="-1">-</option>
</td>
<td> <%= rs.getInt(1)%> </td>
<td> <%= rs.getString(2) %> </td>
</tr>

The combobox will provide the user with a list. The selected value
will be read from the
database. If they change the value this will be updated in the list.

I believe that i need a session or event listner but have not idea how
to start.
Does anyone have a simple example or could provide a better
solution???

Thanks

Clive
 
D

derek

<td> <select name="cboWFMasPointTenth"
onChange="TestWFMasPointTenthfunction(this.value)">
<option value=<%= rs.getInt(3)%> > </option>
<option value="1">Yes</option>
<option value="0">No</option>
<option value="-1">-</option>
</td>
<td> <%= rs.getInt(1)%> </td>
<td> <%= rs.getString(2) %> </td>
</tr>
The combobox will provide the user with a list. The selected value
will be read from the
database. If they change the value this will be updated in the list.
I believe that i need a session or event listner but have not idea how
to start.
Does anyone have a simple example or could provide a better
solution???
Thanks
Clive

Are you trying to take the value the user selects in the combobox and do something with it in the database?
Sorry but i still dont understand what your question is.

You have a javascript method attached to your combobox, but i dont see the code for that, so i dont know if you are submitting
a form? executing some ajax? doing some validation? with the javascript.

If you are trying to get the selected option from the combobox, then you have to either submit the form or use ajax.
Either way you will need to get the data to the server again. I do not see any code for processing that either.
 
C

Clive_

Are you trying to take the value the user selects in the combobox and do something with it in the database?
Sorry but i still dont understand what your question is.

You have a javascript method attached to your combobox, but i dont see the code for that, so i dont know if you are submitting
a form? executing some ajax? doing some validation? with the javascript.

If you are trying to get the selected option from the combobox, then you have to either submit the form or use ajax.
Either way you will need to get the data to the server again. I do not see any code for processing that either.- Hide quoted text -

- Show quoted text -

Hi,

I have a combobox with values 1, 0, -1.

I want the selected item to equal the value in the database.
It may be 1, then the user changes it to 0.

You can 'read' the value from the database:
<option value=<%= rs.getInt(3)%> > </option>

How do you get the value in combobox to 'synchronise' with the
database. So if the value of rs.getInt(3) is 0.
The combox would have 0 selected.

PS got code for combobox & database from Java not JS??

Thanks

Clive
 
D

derek

Hi,
I have a combobox with values 1, 0, -1.
I want the selected item to equal the value in the database.
It may be 1, then the user changes it to 0.
You can 'read' the value from the database:
<option value=<%= rs.getInt(3)%> > </option>
How do you get the value in combobox to 'synchronise' with the
database. So if the value of rs.getInt(3) is 0.
The combox would have 0 selected.
PS got code for combobox & database from Java not JS??
Thanks
Clive

if you want a specific option to be selected, just print this html out.

<option value="whatever" selected> </option>

Note the "selected" keyword.

It will make the option show as selected.

You will want add an if check in your code to determine where to output the "selected" text though.
 
L

Lew

derek said:
if you want a specific option to be selected, just print this html out.

<option value="whatever" selected> </option>

Note the "selected" keyword.

It will make the option show as selected.

You will want add an if check in your code to determine where to output the "selected" text though.

For XHTML the attribute is
selected="selected"

You can set a String on the server side and make it a request attribute for
the forward to the JSP, for example,

<option value="whatever" ${whateverSelected} > </option>

where the logic set the variable whateverSelected to either
"selected=\"selected\"" or "".
 
R

Roedy Green

I need to update a combobox or list from a databse.
There needs to be a dynamic relationship between the database
cell and the selection.

If you were writing in pure Java, your problem is essentially the
database uses one representation and your JComboBox a different one.

You can convert back and forth with a switch e.g.

switch ( fromCombo )
{
case 'Y':
db = "true";
break;
case 'N':
db = "false";
break;
case '-':
db = null;
break;
}
 
C

Clive_

Hi,

You are saying that the 'Html code' is Javascript and to be Java it
has to be based on a Jtable ie Swing??
 
A

Andrew Thompson

Clive_ wrote:
...
You are saying that the 'Html code' is Javascript and to be Java it
has to be based on a Jtable ie Swing??

I am pretty sure that is not what Roedy was getting at.

JSP (or servlets) can make reference to JavaScript
in the resulting HTML (it does not matter if it is JSP
or servlet - to a browser, it is HTML). That JavaScript
might do things like help validate the form input on
the client side.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200711/1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top