R4R
Right Place For Right Person TM
 
R4R Right ArrowJavaRight ArrowJSP Example
Previous Home Next

Database connectivity in jsp


Introduction:

JSP provide a simple way to connect to the database in our web application in this we can use all database in form of the backend for our web application. 


Descriptions:
 

 
In the connection first task of this programmer is to load database driver. 

String driver = "oracle.jdbc.driver.OracleDriver";
Class.forName(driver).newInstance();

The next task is to make a connection. 

String url="jdbc:oracle:thin:@localhost:1521:xe","system","system";
con=DriverManager.getConnection(url);

When url is passed into getConnection() method of DriverManager class it  returns connection object. 

Executing Query or Accessing data from database:

This is done using following code :-

stmt=con.createStatement(); //create a Statement object 
rst=stmt.executeQuery("select * from books_details");


SourceCode:

 

<%@ 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">
<%@ page language="java" import="java.sql.*" %>
<%
	String driver = "oracle.jdbc.driver.OracleDriver";
	Class.forName(driver).newInstance();
	
	Connection con=null;
	ResultSet rst=null;
	Statement stmt=null;
	
	try{
		String url="jdbc:oracle:thin:@localhost:1521:xe","system","system";
		con=DriverManager.getConnection(url);
		stmt=con.createStatement();
	}
	catch(Exception e){
		System.out.println(e.getMessage());
	}
	if(request.getParameter("action") != null){ 
		String bookname=request.getParameter("goodsname");
		String author=request.getParameter("status");
		stmt.executeUpdate("insert into goods(goods_name,
status) values('"+goodsname+"','"+status+"')");
		rst=stmt.executeQuery("select * from goods");
		%>
		<html>
		<body>
		<center>
			<h2>Goods List</h2>
			<table border="1" cellspacing="0" cellpadding="0">
			<tr>
				<td><b>S.No</b></td>
				<td><b>Goods Name</b></td>
				<td><b>Status</.b></td>
			</tr>
			 	<%
				int no=1;
				while(rst.next()){
				%>
				<tr>
				  <td><%=no%></td>
				  <td><%=rst.getString("goods_name")%></td>
				  <td> <%=rst.getString("status")
%> </td>
				</tr>
				<%
				no++;
	}
	rst.close();
	stmt.close();
	con.close();
%>
			</table>
			</center>
		</body>
	</html>
<%}else{%>
	<html>
	<head>
		<title>Goods Entry FormDocument</title>
		<script language="javascript">
		    function validate(objForm){
			if(objForm.goodsname.value.length==0){
			alert("Please enter Goods Name!");
			objForm.goodsname.focus();
			return false;
			}
			if(objForm.status.value.length==0){
			alert("Please enter Status!");
			objForm.status.focus();
			return false;
			}
			return true;
				}
			</script>
		</head>
		
		<body>
			<center>
<form action="GoodsEntryForm.jsp" method="post" 
name="entry" onSubmit="return
 validate(this)">
	<input type="hidden" value="list" name="action">
	<table border="1" cellpadding="0" cellspacing="0">
	<tr>
		<td>
			<table>
				<tr>
				<td colspan="2" align="center">
<h2>Goods Entry Form</h2></td>
				</tr>
				<tr>
				<td colspan="2">&nbsp;</td>
				</tr>
				<tr>
				<td>Goods Name:</td>
				<td><input name="bookname" type=
"text" size="50"></td>
				</tr>
				<tr>
				<td>Status:</td><td><input name=
"author" type="text" size="50"></td>
				</tr>
				<tr>
					<td colspan="2" align="center">
<input type="submit" value="Submit"></td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</form>
			</center>
		</body>
	</html>
<%}%>




Output of SourceCode:
 

Database connectivity in jsp

Previous Home Next
R4R Right ArrowJavaRight ArrowJSP Example

New Updates

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R