JavaScript + JSP + MySql

Hi,

I am working on a project, for which I am using JSP & MySql.

I Have a page index.jsp, which contain a table which displays the content of the database( Using J connector) . There is also a button which when clicked will fold/unfold the table ( meaning.. hide / make it visible ). What I intend to do is that moment the user clicks the button , somehow the table should be updated and be displayed without refreshing the entire page.

Eg:

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="Connections/test_xp.jsp" %>

<%
Driver DriverRecordset1 = (Driver)Class.forName(MM_test_xp_DRIVER).newInstance();
Connection ConnRecordset1 = DriverManager.getConnection(MM_test_xp_STRING,MM_test_xp_USERNAME,MM_test_xp_PASSWORD);
PreparedStatement StatementRecordset1 = ConnRecordset1.prepareStatement("SELECT * FROM mysql.sample_xp");
ResultSet Recordset1 = StatementRecordset1.executeQuery();
boolean Recordset1_isEmpty = !Recordset1.next();
boolean Recordset1_hasData = !Recordset1_isEmpty;
Object Recordset1_data;
int Recordset1_numRows = 0;
%>
<%
int Repeat1__numRows = -1;
int Repeat1__index = 0;
Recordset1_numRows += Repeat1__numRows;
%>

This is the First Part of my index.jsp

What I am trying to do is something like this ( I know it doesnot work ;) )
<%!

public static void update_table()
{

out.println("<tr>");
out.println("<td width=\"20%\"> </td>");
out.println("<td>Name</td>");
out.println("<td>Group</td>");
out.println("<td>Search_String</td>");
out.println("<td>Date_Of_Access</td>");
out.println("</tr>");

while ((Recordset1_hasData)&&(Repeat1__numRows-- != 0)) {
Object url = Recordset1.getObject("URL") ;

out.println("<tr>");
out.println("<td width=\"20%\"> </td>");

out.println("</tr>");

Repeat1__index++;
Recordset1_hasData = Recordset1.next();
}

out.println("</table>");
}
%>

This is followed by <head>
…….

And in the body part

<button idbut1" class="button-small" onclick="update_table()" style="width:121; height: 33"> <font size="2"> Show Table </font> </button>

Is is possible to achieve what I wanted ?
[2609 byte] By [rakeshxp] at [2007-11-19 1:49:09]
# 1 Re: JavaScript + JSP + MySql
this is what i cud come up with...maybe it helps...maybe not...
the body tag looks like

<BODY >
<table id=tab style=visibility:hidden>
<tr>
<td>
Name
</td>
<td>
Address
</td>
</tr>
<tr>
<td id=name_row_1>
Myname
</td>
<td id=add_row_1>
MyAddress
</td>
</tr>
<tr>
<td id=name_row_2>
YourName
</td>
<td id=add_row_2>
YourAddress
</td>
</tr>
</table>
<!--the above table can be created in a function the first time. -->
<input type=button onclick=disp_table(); value="Show Table">
</BODY>

the functions...

function disp_table()
{
//i assume that the button works as a toggle to show/hide the table...something ur code doesnt seem to be doing though!

if (tab.style.visibility=='hidden')
{
/*
read from the database and write to the table <td>'s as
for(counter=1;counter<total_no_of_recs_in_database;counter++)
name_row_counter.innerHTML= nameField.value;
add_row_counter.innerHTML= addField.value;

just a guess as im not sure how ur database is structured...but u can work out that part! :)
*/
//show the table
}
else
//hide the table and we need not update the table as its not visible.


}

hope it helps u in some way.
PallaviDalvi at 2007-11-8 0:20:10 >
# 2 Re: JavaScript + JSP + MySql
That is precisely the problem . Since I am using JSP, I have no idea as to how to create a function to access the database
rakeshxp at 2007-11-8 0:21:09 >
# 3 Re: JavaScript + JSP + MySql
dont think thats ur prob...the code u posted originally has the code for extracting the data from the database... however if u are looking out for a client side(javascript) function that reads from the database... well thats not possible...the database is placed at the server side and thus HAS to be accessed by a server side script only...unless u read it and store in a javascript array... which can then be accessed in a javascript function.
PallaviDalvi at 2007-11-8 0:22:08 >
# 4 Re: JavaScript + JSP + MySql
The code that I had put was a JSP code which is executed only when the page is loaded.. it is not a function.. could u tell me how to make it into a function & call it ?
rakeshxp at 2007-11-8 0:23:04 >
# 5 Re: JavaScript + JSP + MySql
sorry for confusing u... but i was quite confused myself!
well heres what u need to do... the show table button shud not be a normal button but a submit button. on every click(rather when the table is to be displayed)...the form shud be submitted... the updated details fetched and then displayed. hopefully this helps u. and sorry once again! :)
PallaviDalvi at 2007-11-8 0:24:14 >