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> } <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> <pre>