Using Connector/MXJ with JSP


The following web application was provided by Pavan Venkatesh as an example of a JSP based application using Connector/MXJ to provide contact data. The example consists of two components, insertdata.jsp and response.jsp. The insertdata.jsp provides a form into which you can enter information to be stored within the MariaDB database provided by Connector/MXJ. The response.jsp file is called when you submit the form and then provides a table of the data.

Because the data is stored through Connector/MXJ the instantiation of the MariaDB database is handled dynamically on the fly, making the script lightweight and self-contained.

Example 20.13. insertdata.jsp

<%@page contentType='text/html' pageEncoding='UTF-8'%>
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
 'http://www.w3.org/TR/html4/loose.dtd'>
<%@ page import='java.sql.*'%>
<%@ page import='java.io.*'%>
<%@ page import='java.io.*'%>
<%@ page import='java.sql.*'%>
<HTML>
<HEAD>
<TITLE>insert data</TITLE>
<script language='Javascript'>
function validFields(){
if (document.form2.ID.value == '' || document.form2.ID.value == null){
alert ( 'Please enter ID / Field cannot be left blank' );
return false;
}
if (document.form2.names.value == '' || document.form2.names.value == null){
alert ( 'Please enter Name / Field cannot be left blank' );
return false;
}
if (document.form2.place.value == ''|| document.form2.place.value == null){
alert ( 'Please enter Place / Field cannot be left blank' );
return false;
}
if (document.form2.phone.value == ''|| document.form2.phone.value == null){
alert ( 'Please enter Phone number / Field cannot be left blank' );
return false;
}
return true;
}
</script>
</HEAD>
<BODY bgcolor='#ffffcc'>
<H1>Welcome to MariaDB world</H1>
<H2>MySQL with MXJ Connection</H2>
<P>Insert data in existing 'contactdetails2009' table under
'Directory' database</P>
<FORM action='response.jsp' method='get' name='form2'>
<TABLE style='background-color: #ECE5B6;' WIDTH='30%'>
 <TR>
 <TH width='50%'>
 <center>ID</center>
 </TH>
 <TD width='50%'><INPUT TYPE='text' NAME='ID'></TD>
 </TR>
 <TR>
 <TH width='50%'>
 <center>Name</center>
 </TH>
 <TD width='50%'><INPUT TYPE='text' NAME='names'></TD>
 </TR>
 <TR>
 <TH width='50%'>
 <center>City</center>
 </TH>
 <TD width='50%'><INPUT TYPE='text' NAME='place'></TD>
 </TR>
 <TR>
 <TH width='50%'>
 <center>Phone</center>
 </TH>
 <TD width='50%'><INPUT TYPE='text' NAME='phone'></TD>
 </TR>
 <TR>
 <TH></TH>
 <TD width='50%'><INPUT TYPE='submit' VALUE='Insert'
 OnClick='return validFields();'></TD>
 </TR>
</TABLE>
</FORM>
</BODY>

Example 20.14. response.jsp

Retornar