Saturday, November 12, 2011

JSP with MSaccess Database

<%--
Document : File2
Created on : Nov 11, 2011, 3:00:10 PM
Author : Spectrum
--%>
<%@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">
<html>
<head><title>Enter to database</title></head>
<body>
<table>
<%@ page import="java.util.*" %>
<%@ page import="javax.sql.*;" %>
<%
java.sql.Connection con;
java.sql.Statement s;
java.sql.ResultSet rs;
java.sql.PreparedStatement pst;
con=null;
s=null;
pst=null;
rs=null;
 
// Remember to change the next line with your own environment
/*String url= "jdbc:jtds:sqlserver://nameofyourdatabaseserver.or.ipaddress/yourdatabasename";
String id= "username";
String pass = "password";*/
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = java.sql.DriverManager.getConnection("jdbc:odbc:emaildb", "", "");
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
String sql = "select * from emaillists";
try
{
s = con.createStatement();
rs = s.executeQuery(sql);
%>
<%while( rs.next() )
{
%>
<tr>
<td><%= rs.getString(1) %></td>
<td><%= rs.getString(2) %></td>
<td><%= rs.getString(3) %></td>
</tr>
<%
} %>
<%}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(rs!=null) rs.close();
if(s!=null) s.close();
if(con!=null) con.close();
}
%>
</body>
</html>