<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="formAction.jsp" method="get" name="form1" id="form1">
<table>
<tr>
<td><strong>UserName:</strong> <input type="text" name="username" id="username" size="30"/></td>
</tr>
<tr>
<td><strong>Password:</strong> <input type="password" name="password" id="password" size="30"/></td>
</tr>
<tr>
<td><strong>Url:</strong> <input type="text" name="url" id="url" size="50"/></td>
</tr>
<tr><td><input type="submit" value="Submit" name="submit"/> <input type="reset" name="reset" value="Reset"/><a href="outPut.jsp">Show Database</a> </td></tr>
</table>
</form>
</body>
</html>
// Create Mysql Database Connection with jsp//
// Insert Data in Database //
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import = "javax.sql.*" %>
<%
Connection con = null;
Statement stmt = null;
PreparedStatement psmt = null;
ResultSet rs = null;
if((request.getParameter("submit")!=null)&&(request.getParameter("submit")!=""))
{
String username = request.getParameter("username");
String password = request.getParameter("password");
String url = request.getParameter("url");
try{
String _class = "com.mysql.jdbc.Driver";
String _driver = "jdbc:mysql://localhost:3306/test";
String _user = "root";
String _pass = "public";
int updateQuery;
Class.forName(_class); //Load the driver
con = DriverManager.getConnection(_driver,_user,_pass); //Create the connection
String Query1 = "Insert into cromext(username,password,url)values(?,?,?)";
psmt = con.prepareStatement(Query1);
psmt.setString(1,username);
psmt.setString(2,password);
psmt.setString(3,url);
updateQuery = psmt.executeUpdate();
if(updateQuery !=0)
%>
<TABLE style="background-color: #E3E4FA;" WIDTH="30%" border="1">
<tr><th>Data is inserted successfully in database.</th></tr>
</TABLE>
<%
}
catch(Exception ex) {
out.println("Unable to process the database."+ex);
}
finally{
//rs.close();
psmt.close();
con.close();
}
}
%>
// Show the result of database through jsp//
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.text.*" %>
<%
Connection con = null;
Statement stmt = null;
PreparedStatement psmt = null;
ResultSet rs = null;
String username = request.getParameter("username");
String password = request.getParameter("password");
String url = request.getParameter("url");
try{
String _class = "com.mysql.jdbc.Driver";
String _driver = "jdbc:mysql://localhost:3306/test";
String _user = "root";
String _pass = "public";
Class.forName(_class);
con = DriverManager.getConnection(_driver,_user,_pass);
stmt = con.createStatement();
String Query1 = "select username,url from cromext";
rs = stmt.executeQuery(Query1);
%>
<style>
a:link { color: red; text-decoration: none; }
a:visited { color: red; text-decoration: none; }
a:curser {}
a:hover { color: Green; }
</style>
<center><H2>Show Database</H2><h4><a href="index.html">Home</a> <a href="update.jsp" >Update Data</a> <a href="delete.jsp">Delete Data</a></h4></center>
<TABLE cellpadding="3" border="1" style="background-color:#e2e8e1; border-collapse:collapse; border-color:Black; border-style:solid;color:black; " align="center">
<th> No. </th>
<th> User Name</th>
<th> Url </th>
<%
int i=0;
while(rs.next()){
i++;
%>
<tr bgcolor="#ffd1d7">
<td><%out.print(i); %></td>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
</tr>
<% if(rs.next()){
i++; %>
<tr bgcolor="#f1efe2">
<td><%out.print(i); %></td>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
</tr>
<% } }%>
</TABLE>
<%
}
catch(Exception ex){
out.print("Unable to get the Data."+ex);
}
finally{
rs.close();
stmt.close();
con.close();
}
%>
No comments:
Post a Comment