Project file Tree
DataBase
index.java
<%@page language="Java" import="java.sql.*" %>
<jsp:useBean id="db" scope="request" class="SQLBean.DbBean" />
<jsp:setProperty name="db" property="*" />
<%!
ResultSet rs = null ;
int i;
%>
<HTML>
<HEAD><TITLE>DataBase Search</TITLE></HEAD>
<BODY>
<h2> Results from </h2>
<hr>
<br><br>
<table border="1">
<%
db.connect();
try {
rs = db.execSQL("select * from EMPLOYEE");
i = db.updateSQL("UPDATE employee set empid = 'e005' where empname='ravi'");
}catch(SQLException e) {
throw new ServletException("Your query is not working", e);
}
%>
<tr>
<th>Emp ID</th>
<th>NAME</th>
<th>ADDRESS</th>
</tr>
<%
while(rs.next()) {
%>
<tr>
<td><%= rs.getString("empid") %></td>
<td><%= rs.getString("empname") %></td>
<td><%= rs.getString("empadd") %></td>
</tr>
<%
}
%>
<% db.close();
%>
</table>
</body>
<HTML>
DBBean.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package SQLBean;
import java.sql.*;
//import java.io.*;
/**
*
* @author Administrator
*/
public class DbBean {
String dbURL = "jdbc:odbc:test";
String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
private Connection dbCon;
public DbBean(){
super();
}
public boolean connect() throws ClassNotFoundException,SQLException{
Class.forName(dbDriver);
dbCon = DriverManager.getConnection(dbURL);
return true;
}
public void close() throws SQLException{
dbCon.close();
}
public ResultSet execSQL(String sql) throws SQLException{
Statement s = dbCon.createStatement();
ResultSet r = s.executeQuery(sql);
return (r == null) ? null : r;
}
public int updateSQL(String sql) throws SQLException{
Statement s = dbCon.createStatement();
int r = s.executeUpdate(sql);
return (r == 0) ? 0 : r;
}
}
No comments:
Post a Comment