Friday, 1 February 2013

Jsp connection with mysql database


Create a folder named user inside your web server (Tomcat) C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps and inside the user create WEB-INF and META-INF so that you can create a file inside META-INF  named Content.xml  and you need to create a empty folder WEB-INF  and you can create and place the file test.jsp inside C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\user

Content.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/user"/>

test.jsp
<!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.*" %>

<html>
<head>
<title>Connection with mysql database</title>
</head>
<body>
<h1>Connection status </h1>

<%
String connectionURL = "jdbc:mysql://localhost:3306/usermaster";
Connection connection = null;
ResultSet rst=null;
Statement st=null;

try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "a");
st = connection.createStatement();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
if(request.getParameter("action") != null)
{
String un=request.getParameter("un");
String pw=request.getParameter("pwd");
rst=st.executeQuery("select * from login");
%>

<h2>Login/h2>
<table border="1" cellspacing="0" cellpadding="0">
<tr>

<td><b>User name</b></td>
<td><b>Password</b></td>
</tr>
<%

while(rst.next()){
%>
<tr>

 <td><%=rst.getString("un")%></td>
 <td> <%=rst.getString("pwd")%></td>
</tr>

<%

rst.close();
st.close();
connection.close();

%>
</table>
</body>
</html>
Please provide feedback readers.
Thank you
Vikram.S

No comments: