Database connectivity in JSP
To connect JSP file with database, we required a database
like ms-access, mysql, sql server or oracle. We can connect our jsp file with
any one of the database table.
1. Create a database file in access and create a table in it for example user table having fields
1. Create a database file in access and create a table in it for example user table having fields
Username, password, sname, email id and
address.
2.
Now create a DNS – data name server from the
administrative tool of control panel.
3.
Create a web application project in netbeans.:-
in this step we will add some .html file and .jsp files to add data in the
database table
Index.html
Index.jsp
<%--
Document : index
Created on : Sep 12, 2017, 10:38:49 PM
Author : rakhee
--%>
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Registerinsert.jsp
<%--
Document : registerinsert
Created on : Sep 12, 2017, 10:40:27 PM
Author : rakhee
--%>
<%@page contentType="text/html"%>
<%@page
pageEncoding="UTF-8"%>
<%@page
import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>Example of Java Server Page with JDBC</title>
</head>
<body>
<%
String
u=request.getParameter("userid");
String
p=request.getParameter("password");
String n=request.getParameter("sname");
String
e=request.getParameter("eid");
String
a=request.getParameter("addr");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:sgc","","");
/* Passing argument through the question
mark */
PreparedStatement
ps=con.prepareStatement("insert into login values(?,?,?,?,?)") ;
ps.setString(1,u);
ps.setString(2,p);
ps.setString(3,n);
ps.setString(4,a);
ps.setString(5,e);
int i=ps.executeUpdate(); /*Set the
Update query command */
if(i!=0)
{
response.sendRedirect("index.html?msg=Thank You for registering
with us in Mrbool !");
}
else
{
response.sendRedirect("registerinsert.jsp?msg=Insertion Failed!!
Please try again!!! ");
}
con.close();
}
catch(Exception ex)
{
out.println(ex);
}
%>
</body>
</html>
No comments:
Post a Comment