In this project we doing user registration, saving user details in database. we used Oracle 10g as database.
To run this project we need following files:
1)web.xml
2)RegForm.jsp
3)RegisterData.java
4)DbConnection.java
package com.jexamples.dbconnect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbConnection
{
public static Connection getConn() throws ClassNotFoundException, SQLException
{
Class stringClass= Class.forName("oracle.jdbc.driver.OracleDriver");
//if we use "oracle.jdbc.OracleDriver" then also its working.
System.out.println("Class found: "+stringClass.getName());
String url="jdbc:oracle:thin:@localhost:1521:ORCLE";
Connection con=DriverManager.getConnection(url,"scott","tiger");
return con;
}
}
To run this project we need following files:
1)web.xml
2)RegForm.jsp
3)RegisterData.java
4)DbConnection.java
1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Reg Servlet</servlet-name>
<servlet-class>com.jexamples.web.RegisterData</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Reg Servlet</servlet-name>
<url-pattern>/RegServ.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2)RegForm.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="maroon">
<center>
<h1>Registration Form</h1>
<form name="uForm" method="get" action="RegServ.do">"
<table>
<tr><td colspan="2">Please register here</td></tr>
<tr><td>Enter user id</td><td><input type="text" name="uid" value="" size="25"></td> </tr>
<tr><td>Enter user name</td><td><input type="text" name="uname" value="" size="25"></td> </tr>
<tr><td> Gender</td><td><select name="ugender" size="1">
<option value="m">Male</option>
<option value="f">Female</option>
</select>
</td> </tr>
<tr><td>Enter mobile no.</td><td><input type="text" name="unum" value="" size="25"></td> </tr>
<tr><td>Address</td><td><input type="text" name="uaddr" value="" size="25"></td> </tr>
</table>
<br>
<input type="submit" value="Registe Now!"/>"
</form>
</center>
</body>
</html>
3)RegisterData.java
package com.jexamples.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jexamples.dbconnect.*;
public class RegisterData extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
int count=0;
Connection con=null;
Statement stmt=null;
try
{
con = DbConnection.getConn();
stmt = con.createStatement();
}
catch (Exception e)
{
}
String userid=request.getParameter("uid");
String name=request.getParameter("uname");
String gender=request.getParameter("ugender");
String numb=request.getParameter("unum");
String addr=request.getParameter("uaddr");
try
{
count = stmt.executeUpdate("INSERT INTO BEERTAB VALUES ('"+userid+"','"+name+"','"+gender+"','"+numb+"','"+addr+"')");
}
catch (Exception e)
{
}
if(count > 0)
{
out.println("<h1>Update Successful</h1>");
}
else
{
out.println("<h1>This id not available. Kindly choose other id!!!</h1>");
}
}
}
4)DbConnection.java
package com.jexamples.dbconnect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbConnection
{
public static Connection getConn() throws ClassNotFoundException, SQLException
{
Class stringClass= Class.forName("oracle.jdbc.driver.OracleDriver");
//if we use "oracle.jdbc.OracleDriver" then also its working.
System.out.println("Class found: "+stringClass.getName());
String url="jdbc:oracle:thin:@localhost:1521:ORCLE";
Connection con=DriverManager.getConnection(url,"scott","tiger");
return con;
}
}