一起学习
package guestbook;
import java.sql.*;
public class guestbook{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:guestbook";
Connection conn = null;
ResultSet rs = null;
public guestbook() {
try {
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("guestbook(): " e.getMessage());
}
}
public void executeInsert(String sql) {
try {
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex) {
System.err.println("guestbook.executeUpdate: " ex.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("guestbook.executeQuery: " ex.getMessage());
}
return rs;
}
}
<html>
<head>
<title>My First Jsp GuestBook</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#CCCCFF" vlink="#000000" alink="#666666">
<div align="center">
<p><font size="7"><b><font size=" 6">Jsp GuestBook</font></b></font></p>
<form method="post" action="index.jsp">
<table width="75%" align="center" border=1>
<tr>
<td width="30%">
<div align="right">Name:</div>
</td>
<td width="70%">
<input type="text" name="name">
</td>
</tr>
<tr>
<td width="30%">
<div align="right">E-mail:</div>
</td>
<td width="70%">
<input type="text" name="email">
</td>
</tr>
<tr>
<td width="30%">
<div align="right">Site Url:</div>
</td>
<td width="70%">
<input type="text" name="url" value="http://">
</td>
</tr>
<tr>
<td width="30%">
<p>What do you want to</p>
<p>say?</p>
</td>
<td width="70%">
<textarea name="body" rows="5" cols="30"></textarea>
</td>
</tr>
<tr>
<td width="30%"> </td>
<td width="70%"> </td>
</tr>
<tr>
<td width="30%"> </td>
<td width="70%">
<table width="75%">
<tr>
<td>
<input type="submit" name="send" value="Send">
</td>
<td>
<input type="reset" name="reset" value="Reset">
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
<hr>
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="sqlbean" scope="page" class="guestbook.guestbook" />
<%! String name,email,url,body,sql; %>
<%
if (name!=null&&email!=null&&body!=null)
{
name = request.getParameter("name");
email = request.getParameter("email");
url = request.getParameter("url");
body = request.getParameter("body");
sql = "insert into guestbook (Name,Email,Url,Body) values('" name "','" email "','" url "','" body "')";
sqlbean.executeInsert(sql);
}else{
out.print("<b><font color=red>Sorry, Please enter your name,email and body!!!</font></b>");
}
%>
<p><font size="4"><b><a href="./index.jsp#see">See My Guestbook</a></b></font></p>
<%-- Because I don't konw a good method to konw the all items count,so.... --%>
<%
// ResultSet RScount = sqlbean.executeQuery("SELECT count(*) FROM guestbook");
ResultSet RScount = sqlbean.executeQuery("SELECT * FROM guestbook");
int i=0;
while (RScount.next()) {
i ;
}
%>
<%-- <p><a name=see><B>Now,<%= RScount %> Records.</B><a></p> --%>
<p><a name=see><B>Now,<%= i %> Records.</B><a></p>
<%
RScount.close();
i=0;
%>
<p> </p>
<%
ResultSet RS = sqlbean.executeQuery("SELECT * FROM guestbook ORDER BY RecordID DESC");
while (RS.next()){
name = RS.getString("Name");
email = RS.getString("Email");
url = RS.getString("Url");
body = RS.getString("Body");
%>
<table border=1 width=80%>
<tr>
<td width=30%>Name:</td><td width=70%><%= name %>
</tr>
<tr>
<td width=30%>Email:</td><td width=70%><a href="mailto:<%= email %>"><%= email %></a></td>
</tr>
<tr>
<td width=30%>SiteUrl:</td><td width=70%><a href="<%= url %>"><%= url %></a></td>
</tr>
<tr>
<td width=30%>Body:</td><td width=70%><%= body %></td>
</tr>
</table>
<hr>
<%
}
RS.close();
%>
</div>
</body>
</html>
|
下载本文示例代码
JSP记事本程序JSP记事本程序JSP记事本程序JSP记事本程序JSP记事本程序JSP记事本程序JSP记事本程序JSP记事本程序JSP记事本程序JSP记事本程序JSP记事本程序JSP记事本程序
阅读(109) | 评论(0) | 转发(0) |