JDBC Example – 1 (Connecting with MS-ACCESS Database)

To Run following example follow the stpes.

Step 1 -> Create Access Database (Employee) & Table

Table structure used for following example

Table Name :- Emp_Info

1) emp_id

2) name

3) designation

4) department

5) basic salary

Step 2-> Create a DSN

(Control Panel -> Administrative Tools -> Data Sources (ODBC) -> Add User DSN

Provide Database path & DSN Name(Emp))

Step 3 -> Create a servlet that list all employees information

d:\tomcat\webapps\JDBC\WEB-INF\classes\emp\EmployeeInfo.java

</pre>
package emp;

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;

public class EmployeeInfo extends HttpServlet
{
 public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
 {
 Connection con= null;
 Statement stmt = null;
 ResultSet rs = null;
 String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
 String url="jdbc:odbc:Emp";
 PrintWriter out;
 String query;
 res.setContentType("text/html");

 try
 {
 query = req.getParameter("query");
 Class.forName(driver);//.newInstance();
 con = DriverManager.getConnection(url,"","");
 stmt = con.createStatement();
 System.out.println("query : " + query);
 if (query!="")
 {
 rs = stmt.executeQuery(query);
 }
 else
 {
 rs = stmt.executeQuery("select * from employees");
 }
 out = res.getWriter();
 ResultSetMetaData rsmd = rs.getMetaData();
 int count = rsmd.getColumnCount();
 for (int i=1;i<=count;i++)
 {
 out.println (rsmd.getColumnName(i));
 }

 out.println ("<table border='1'>");

 while (rs.next())
 {
 out.println ("<tr><td>");

 for (int j=1;j<=rsmd.getColumnCount();j++)
 {
 out.println (rs.getString(j));
 out.println ("<td>");
 }

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

 }
 catch(Exception e)
 {
 System.out.println (e.getMessage());
 }
<span style="line-height: 1.4;">}</span>
}

&nbsp;
<pre>

web.xml file

</pre>
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements. See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 version="3.0"
 metadata-complete="true">

<description>
 Servlet and JSP Examples.
 </description>
 <display-name>Servlet and JSP Examples</display-name>
 <jsp-property-group>
 <url-pattern>*.jsp</url-pattern>
 <scripting-invalid>true</scripting-invalid>
 </jsp-property-group>

<servlet>
 <servlet-name>EmployeeInfo</servlet-name>
 <servlet-class>emp.EmployeeInfo</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>EmployeeInfo</servlet-name>
 <url-pattern>/EmployeeInfo</url-pattern>
 </servlet-mapping>

</web-app>

&nbsp;
<pre>

Creating custom tag to access body of tag Example

Step : 1 Create TagHandler class

c:\tomcat\webapps\customtag\WEB-INF\classes\mytag\ExampleTag.java

package mytagbody;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class ExampleTag extends SimpleTagSupport
{
 private String message = "Default Message";

 StringWriter sw = new StringWriter();
 public void doTag() throws JspException, IOException
 {
 getJspBody().invoke(sw);
 getJspContext().getOut().println(sw.toString());
 }
 public String getMessage()
 {
 return message;
 }
 public void setMessage(String message)
 {
 this.message = message;
 }
}

Note : To compile this file set classpath=”c:\tomcat\lib\jsp-api.jar”

Step 2: Create TLD (Tag Library Descriptor file) to map Taghnadler class with xml tag

c:\tomcat\webapps\customtag\WEB-INF\example.tld

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>csajsp-taglib</short-name>
<tag>
 <name>example</name>
 <tag-class>mytagbody.ExampleTag</tag-class>
 <body-content>tagdependent</body-content>
 <attribute>
 <name>message</name>
 <required>true</required>
 </attribute>
</tag>
</taglib>

Step 3: Use in JSP page using taglib directive

c:\tomcat\webapps\customtag\customtag1.jsp

<%@ taglib uri="/WEB-INF/exampel2.tld"
prefix="c" %>
<c:example message="testing...">
 this is for test
</c:example>


Creating Custom tag with attribute Example

Step : 1 Create TagHandler class

c:\tomcat\webapps\customtag\WEB-INF\classes\mytagattb\ExampleTag.java

package mytagattb;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class ExampleTag extends SimpleTagSupport
{
 private String message = "Default Message";

 public void doTag() throws JspException, IOException
 {
 JspWriter out =
 getJspContext().getOut();
 out.print("<b>Hello World!</b>");
 out.print(getMessage());
 }
 public String getMessage()
 {
 return message;
 }
 public void setMessage(String message)
 {
 this.message = message;
 }
}

Note : To compile this file set classpath=”c:\tomcat\lib\jsp-api.jar”

Step 2: Create TLD (Tag Library Descriptor file) to map Taghnadler class with xml tag

c:\tomcat\webapps\customtag\WEB-INF\example.tld

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>csajsp-taglib</short-name></pre>
<tag>
 <name>example</name>
 <tag-class>mytagattb.ExampleTag</tag-class>
 <body-content>scriptless</body-content>
 <attribute>
 <name>message</name>
 <required>true</required>
 </attribute>
</tag>

</taglib>

Step 3: Use in JSP page using taglib directive

c:\tomcat\webapps\customtag\customtag1.jsp

<%@ taglib uri="/WEB-INF/exampl1.tld"
prefix="c" %>
<c:example message="testing...">
 this is for test
</c:example>

1) Change in tld file set following in attribute tag


<attribute>
 <name>message</name>
 <required>false</required>
 </attribute>

and run jsp page without providing attribute.

Creating Custom tag Example

Step : 1 Create TagHandler class

c:\tomcat\webapps\customtag\WEB-INF\classes\mytag\ExampleTag.java

package mytag;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class ExampleTag extends SimpleTagSupport 
{
	public void doTag() throws JspException, IOException 
	{
		JspWriter out = 
		getJspContext().getOut();
		out.print("<b>Hello World!</b>");
	}
}

Note : To compile this file set classpath=”c:\tomcat\lib\jsp-api.jar”

Step 2: Create TLD (Tag Library Descriptor file) to map Taghnadler class with xml tag

c:\tomcat\webapps\customtag\WEB-INF\example.tld

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>csajsp-taglib</short-name>
<tag>
<description>Example tag</description>
<name>example</name>
<tag-class>mytag.ExampleTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>

Step 3: Use in JSP page using taglib directive

c:\tomcat\webapps\customtag\customtag1.jsp

<%@ taglib uri="/WEB-INF/example.tld"
prefix="c" %>
<c:example/>
<c:example></c:example>

IN below code this is for testing will give error as in tld file empty
Things to check :


<c:example>this is for testing...</c:example>

1) Change in tld file set body-content as scriptless then check in jsp file


<c:example>this is for testing...</c:example>

above will work where as following will give error


<c:example>this is for testing...<%= 1+2%></c:example>

2) Change in tld file set body-content as tagdependent then check in jsp file


<c:example>this is for testing...<%= 1+2%></c:example>

above will work where as not be evaluated.