Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

database update Combobox

Thread view: 
Clive_ - 30 Oct 2007 18:26 GMT
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
derek - 30 Oct 2007 18:40 GMT
Where is the rest of your code? Where are you reading from a database?
I only see html.
Lew - 31 Oct 2007 00:45 GMT
> Where is the rest of your code? Where are you reading from a database?
> I only see html.

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

Signature

Lew

Clive_ - 31 Oct 2007 10:49 GMT
> > Where is the rest of your code? Where are you reading from a database?
> > I only see html.
[quoted text clipped - 5 lines]
> --
> Lew

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
derek - 31 Oct 2007 13:49 GMT
> <td> <select name="cboWFMasPointTenth"
> onChange="TestWFMasPointTenthfunction(this.value)">
[quoted text clipped - 15 lines]
> 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.
Clive_ - 31 Oct 2007 14:55 GMT
> > <td> <select name="cboWFMasPointTenth"
> > onChange="TestWFMasPointTenthfunction(this.value)">
[quoted text clipped - 26 lines]
>
> - 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
derek - 31 Oct 2007 17:31 GMT
> Hi,
> I have a combobox with values 1, 0, -1.
[quoted text clipped - 8 lines]
> 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.
Lew - 31 Oct 2007 23:52 GMT
> if you want a specific option to be selected, just print this html out.
>
[quoted text clipped - 5 lines]
>
> 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 "".

Signature

Lew

Roedy Green - 03 Nov 2007 18:17 GMT
>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;
}
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Clive_ - 12 Nov 2007 14:52 GMT
Hi,

You are saying that the 'Html code' is Javascript and to be Java it
has to be based on a Jtable ie Swing??
Andrew Thompson - 12 Nov 2007 15:47 GMT
...
>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.

Signature

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

Clive_ - 12 Nov 2007 16:02 GMT
Okay,

Was not clear about that.

Thanks

Clive


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.