• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

�JavaScript to Java and back again� A developers adventure

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a problem where the jsp gets the code via Java call to a database but then once I have the data fields put into Java objects (mostly strings, one date, etc) I need to manipulate the data as jsObjects. I am not sure where to do the conversion (or even how)
Here is kinda what I am trying to do
<%
String sql = "SELECT NAME, NUMBER, CITY FROM MYTABLE";

resultSet rs = statement.executeQuery(sql);

String name ="";
String number = "";
String city ="";

while(rs.next()){
name = getSttring(1);
number = getString(2);
city = getString(3);
}//So ideally here I would like to take name,number,city and drop that into a javascript array...
%>
I figured something like putting an int counter in the while loop and at the end of each loop doing a
var jsData = new Array();
jsData[<%counter%>] = {name, number, city};
I just don't know how to tie the pieces together yet.


Background on this...I am doing it so that I can take the javascript array and make a dynamic sortable table

Thanks as always
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All you need to do is write out the array from the loop like you said. I use the same technique to generate code for menus with my asp.net application.

Here is what I would do in a quick example


If you do not want to use the if statement you can do:
theString += "'" + data + "',"
and rip the last character(comma) off the string after the loop. That will speed up the loop.

Eric
 
Kevin Hamrick
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I am trying to do: A user hits the JSP on the web, the page hits an Oracle database and dynamically builds a sortable table.
Known problems: Besides the fact that it just isn�t working?
Passing the y value (a counter) to the JSP but even when I
Hard code in a value for y I get nothing.
Nothing displays.
I�ve extracted out most of the code and this should build a table within a &ITdiv> on another page but even when ran as a stand alone
I get errors. When I put it into the &ITdiv> All I see in the window where a table should be is my source code.
I�ve posted this in the JSP section and in Java section because I do not know where it should go.
As always, Thanks for the insights. I need some help as I have been struggling all week-end with this.
Side notes-I had to replace all "less then" signs with "&It and "onclick" with "onklick" before I could post it.
&IT%@ page import="java.sql.*" %>
&IT%@ page import="java.text.SimpleDateFormat" %>
&IT%@ include file="connectDB.jsp" %>
&ITHTML>
&ITBODY>
&ITSCRIPT FOR=window EVENT=onload LANGUAGE="javascript">

&IT% System.out.println("--------------------starting page--------------------");%>
&ITSCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
&ITtable id="tblMISSION" width="100%">
&ITthead>
&ITtr bgcolor="lightyellow" style="font-size:10;font-weight:bold;font-family:arial" style="position:relative; top:expression(this.offsetParent.scrollTop -2);left: -1">
&ITth>&ITa href="#" title="Number" onklick="return sortTable(this)">#&IT/a>&IT/th>
&ITth>&ITa href="#" title="NAME" onklick="return sortTable(this)">NAME&IT/a>&IT/th>
&ITth>&ITa href="#" title="CITY" onklick="return sortTable(this)">CITY&IT/a>&IT/th>
&ITth>&ITa href="#" title="GENDER" onklick="return sortTable(this)">GENDER&IT/a>&IT/th>

&IT!--
&ITtd title="Number">#&IT/td>
&ITtd title="NAME">NAME&IT/td>
&ITtd title="CITY" >CITY&IT/td>
&ITtd title="GENDER" >GENDER&IT/td>
-->
&IT/tr>

&IT%
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yy");

String sql = "SELECT NUMBER, NAME, CITY, GENDER FROM MYTABLE ";

ResultSet rs = statement.executeQuery(sql);
String num="";
String name="";
String city="";
String gender="";

int counter=0;
System.out.println("--------------------calling while("+rs.next()+")--------------------");
while (rs.next())
{
//System.out.println("--------------------Hitting database--------------------");
num=rs.getString(1);
if(num == null){num = "";}
name=rs.getString(2);
if (name==null){name="";}
city=rs.getString(3);
if(city == null){city = "";}
gender=rs.getString(5);
if (gender==null){gender="";}


// add a step to convert from java to jsScript and put into an array
%>
&IT!-- Make a call to js to put all the data in a js array of data objects to be sorted. -->
&ITSCRIPT LANGUAGE="JavaScript">
SDBObject(num,name,city,gender, &IT% counter %> ;
&IT/SCRIPT>

&IT%
counter++;


%>
&IT!�I was just hard coding a table in here originally
&IT%
}
%>

&IT/thead>
&ITtbody id="sortData">&IT/tbody>
&IT/table>
&ITSCRIPT LANGUAGE="JavaScript">drawTable("sortDate");&IT/SCRIPT>
&IT%
statement.close();
connection.close();
%>
&IT/BODY>
&IT/HTML>

&ITSCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
&IT!--
function drawTable(tbody) {
&IT%System.out.println("--------------------drawTable--------------------");%>
var tr, td;
tbody = document.getElementByID(tbody);
for(var k = 0; k &IT jsData.length; k++) {
tr = tbody.insertRow(tbody.row.length);
td = tr.insertCell(tr.cells.length);
td.setAttribute("align","center");
td.innerHTML = jsData[k].Num;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsData[k].NUM;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsData[k].NAME;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsData[k].CITY;
td = tr.insertCell(tr.cells.length);
td.innerHTML = jsData[k].GENDER;
td = tr.insertCell(tr.cells.length);

}
}
function sortTable(link) {
&IT%System.out.println("--------------------sortTable()--------------------");%>
switch(link.firstChild.nodeValue){
case "NUM":
js.Data.sort(sortByNumber);
break;
case "MISSION":
jsData.sort(sortByMission);
break;
}
drawTable("sortData")
return false;
}//end sortTable(link) function
function SDBObjects(name, city,gender, i){
&IT%System.out.println("--------------------SDBObjects()--------------------");%>
jsData[i] = {Num: i,NAME: name, CITY: city, GENDER: gender };
}
function sortByMission(a,b){
a = a.location.toLowerCase();
b = b.location.toLowerCase();
return((a&ITb) ? -1 (a>b) ? 1:0));
}
-->
&IT/SCRIPT>
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic