Nitobi
Customer Login
Services
Products
About
Blogs
Contact
Home -> Product Knowledgebase Order Online | Free Trial Downloads

Nitobi Product Knowledgebase

Saving Data (JSP)


The Grid sends only the changes to data to the server for saving. It does this to save on bandwidth in order to maximize responsiveness and server performance.

First you must import all the required java classes, including the EBA gethandler and savehandler libraries.

<%@ page import="java.sql.*" %>
<%@ page import="eba.gethandler.Record" %>
<%@ page import="eba.savehandler.SaveHandler" %>

Next, create a database connection and instantiate the savehandler class.

String pathAfterContext = request.getServletPath();

// remove the get.jsp part of URL
pathAfterContext = pathAfterContext.substring(0,pathAfterContext.lastIndexOf("')).replace('/','\\');

// get the complete path on the server of our database.
String path = config.getServletContext().getRealPath("') + pathAfterContext + "\\data\\NorthWindUltra.mdb";

// get the database connection
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection con=DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=" + path,"","");

// get the ResultSet of the table tblPricing in our database Statement st=con.createStatement();
// Instanciate the eBusiness Applications' class 'SaveHandler'. This class is defined in ebaxmlconverter.jar
SaveHandler mySaveHandler=new SaveHandler(request,out);

Next, we begin by processing our INSERT, UPDATE, and DELETE instructions.

Begin by processing the inserts

// Begin by processing the inserts
Record[] insertRecords = mySaveHandler.getInsertRecords();
for (int i=0; i< insertRecords.length; i++) {
String sql= "tblCustomers (CustomerName, ContactName, ContactEmail, ContactTitle, PhoneNumber, Address, Country) VALUES (" ;

sql+= "'" + insertRecords[i].getField("CustomerName") + "',";
sql+= "'" + insertRecords[i].getField("ContactName") + "',";
sql+= "'" + insertRecords[i].getField("ContactEmail") + "',";
sql+= "'" + insertRecords[i].getField("ContactTitle") + "',";
sql+= "'" + insertRecords[i].getField("PhoneNumber") + "',";
sql+= "'" + insertRecords[i].getField("Address") + "',";
sql+= "'" + insertRecords[i].getField("Country") + "'";
sql+= "); ";
st.executeUpdate(sql);
}
Continue by processing our updates. Here we are only updating the ADDRESS and COUNTRY fields, the other columns have been set to non-updatable in the column declaration with editable="false" attribute.
 // Continue by processing our updates
Record[] updateRecords = mySaveHandler.getUpdateRecords();
for (int i=0; i< updateRecords.length; i++) {

String sql = "UPDATE tblCustomers ";
sql+= "SET ";
sql+= "Address = '" + updateRecords[i].getField("Address") + "', ";
sql+= "Address = '" + updateRecords[i].getField("Country") + "' ";
sql+=" WHERE CustomerID = " + updateRecords[i].getID() + "";
st.executeUpdate(sql);
}
Finish by processing our deletes:
// Finish by processing our deletes
Record[] deleteRecords = mySaveHandler.getDeleteRecords();
for (int i=0; i< deleteRecords.length; i++) {
String sql = "DELETE FROM tblCustomers WHERE CustomerID = '" + deleteRecords[i].getID() + "'";
st.executeUpdate(sql);
}
     st.close();
     con.close(); 

     // reports back to the Grid that the update has successfully been completed
     mySaveHandler.writeToClient("UTF-8");

Here is the entire code sample.
// Finish by processing our deletes

<%@ page import="java.sql.*" %>
<%@ page import="eba.gethandler.Record" %>
<%@ page import="eba.savehandler.SaveHandler" %>

<%
/** *******************************
This file is used as a Save Handler for the Grid control. When the user clicks
the save button in index.jsp a datagram is sent to this script.
The script in turn looks at each update in the datagram and processes them accordingly.

We have provided all the necessary functionality in the SaveHandler class to extract any of the update instructions.
For more details please have a look at our shipped online help under Reference/Server
*********************************** */

/** *******************************
Figure out the path to the mdb file that has the needed data

We need to create:
C:\Program Files\eBusiness Applications\Combo 3.X\Jsp Trial\samples\Basic\Filter\data\customerdb.mdb

This breaks down to the following:
[C:\Program Files\eBusiness Applications\Combo 3.X\Jsp Trial\]+[samples\Basic\Filter]+[\data\customerdb.mdb]

The above breakdown maps to the following:
getRealPath("') ---> [C:\Program Files\eBusiness Applications\Combo 3.X\Jsp Trial\]
getServletPath() ---> [samples\Basic\Filter] (get.jsp is removed)
hard code string ---> [\data\customerdb.mdb]

*/


// from the URL we need to remove the first three tokens [http:/][/localhost:8080][/v32betaJSPSamples/]
// Token 4 to N need to be added to the value returned from getRealPath
// Also the last token is the name of the get handler
String pathAfterContext = request.getServletPath();
// remove the get.jsp part.
pathAfterContext = pathAfterContext.substring(0,pathAfterContext.lastIndexOf("')).replace('/','\\');

// get the complete path on the server of our database.
String path = config.getServletContext().getRealPath("') + pathAfterContext + "\\data\\NorthWindUltra.mdb";

// get the database connection
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection con=DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=" + path,"","");

// get the ResultSet of the table tblPricing in our database
Statement st=con.createStatement();

// Instanciate the eBusiness Applications' class 'SaveHandler'. This class is defined in ebaxmlconverter.jar
SaveHandler mySaveHandler=new SaveHandler(request,out);


// Begin by processing the inserts
Record[] insertRecords = mySaveHandler.getInsertRecords();
for (int i=0; i< insertRecords.length; i++) {
String sql= "tblCustomers (CustomerName, ContactName, ContactEmail, ContactTitle, PhoneNumber, Address, Country) VALUES (" ;

sql+= "'" + insertRecords[i].getField("CustomerName") + "',";
sql+= "'" + insertRecords[i].getField("ContactName") + "',";
sql+= "'" + insertRecords[i].getField("ContactEmail") + "',";
sql+= "'" + insertRecords[i].getField("ContactTitle") + "',";
sql+= "'" + insertRecords[i].getField("PhoneNumber") + "',";
sql+= "'" + insertRecords[i].getField("Address") + "',";
sql+= "'" + insertRecords[i].getField("Country") + "'";
sql+= "); ";
st.executeUpdate(sql);
}

// Continue by processing our updates
Record[] updateRecords = mySaveHandler.getUpdateRecords();
for (int i=0; i< updateRecords.length; i++) {

String sql = "UPDATE tblCustomers ";
sql+= "SET ";
sql+= "Address = '" + updateRecords[i].getField("Address") + "' ";
sql+=" WHERE CustomerID = " + updateRecords[i].getID() + "";
st.executeUpdate(sql);
}

// Finish by processing our deletes
Record[] deleteRecords = mySaveHandler.getDeleteRecords();
for (int i=0; i< deleteRecords.length; i++) {
String sql = "DELETE FROM tblCustomers WHERE CustomerID = '" + deleteRecords[i].getID() + "'";
st.executeUpdate(sql);
}

st.close();
con.close();

// reports back to the Grid that the update has successfully been completed
mySaveHandler.writeToClient();

%>

View Printable Version

Comments:


Poster: Keith (Friday, August 31, 2007)

There is some missing code in this sample. when inserting a new row "INSERT INTO " must be written before tblCustomers(etc, etc) is written.

Name:

Type the text you see above:

Comments:


Knowledgebase

To be notified of new articles when they're available, subscribe to our RSS feed.

Support Resources

Take advantage of our knowledgebase of product tutorials and tips, and our support forums!

Search Site


Sign up for our Newsletter:

Get industry articles and Nitobi company news in your inbox every couple of months — here's a sample!

Email:




Site RSS Feed  | All contents are (c) Copyright 2006, Nitobi Software Inc. All rights Reserved