程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> JSP簡單添加,查詢功能代碼,jsp查詢功能代碼

JSP簡單添加,查詢功能代碼,jsp查詢功能代碼

編輯:關於JSP

JSP簡單添加,查詢功能代碼,jsp查詢功能代碼


本文實例講述了JSP簡單添加,查詢功能。分享給大家供大家參考。具體如下:

JSP技術:

public class ISOtoGb2312
{
public static String convert( String str )
{
try
{
byte<> bytesStr=str.getBytes( "ISO-8859-1" ) ;
return new String( bytesStr, "gb2312" ) ; 
}
catch( Exception ex)
{
return str ;
}
}
}

<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>
輸入數據
</title>
</head>
<body bgcolor="#ffffff">
<h1>
請輸入數據:
</h1>
<hr>
<form method="POST" action="insert.jsp">
<!--webbot bot="SaveResults" U-File="fpweb:///_private/form_results.txt"
S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
<p> </p>
<p> </p>
<p><img border="0" src="img/cisco.gif" width="70" height="81">
<font size="5" color="#0000FF"><b>學 號:
</b></font><input type="text" name="id" size="25"> <img border="0" src="img/cisco.gif" width="70" height="81">
<font size="5" color="#0000FF"><b>性 別</b></font><font size="5" color="#0000FF"><b>:
</b></font><input type="text" name="sex" size="24"></p>
<p><img border="0" src="img/cisco.gif" width="70" height="81">
<font size="5" color="#000080"><b>姓 名:
</b></font><input type="text" name="name" size="25"> <img border="0" src="img/cisco.gif" width="70" height="81">
<font size="5" color="#0000FF"><b>年 齡: </b></font><input type="text" name="age" size="24"></p>
<p><img border="0" src="img/cisco.gif" width="70" height="81">
<font size="5" color="#000080"><b>地 址:
</b></font><input type="text" name="addr" size="84"> </p>
<p> </p>
<p>
<input type="submit" value="提交" name="B1" >
<input type="reset" value="全部重寫" name="B2" >
</p>
</form>
</body>
</html>

<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import = "java.sql.*"%>
<%@ page language = "java"%>
<%@ page import = "test.ISOtoGb2312"%>
<html>
<head>
<title>
添加數據
</title>
</head>
<body bgcolor="#ffffff">
<h1>
接收數據,添加到數據庫.
</h1>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //載入驅動程序類別
Connection con = DriverManager.getConnection("jdbc:odbc:zjyds1"); //建立數據庫鏈接
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
String strSQL;
strSQL = "INSERT INTO tab02(id, name, sex, " +
"age, addr) Values (" +
ISOtoGb2312.convert( request.getParameter("id")) + "," +
ISOtoGb2312.convert( request.getParameter("name")) + "," +
ISOtoGb2312.convert( request.getParameter("sex")) + "," +
ISOtoGb2312.convert( request.getParameter("age")) + "," +
ISOtoGb2312.convert( request.getParameter("addr")) + ")";
stmt.executeUpdate(strSQL);
ResultSet rs; //建立ResultSet(結果集)對象
rs = stmt.executeQuery("SELECT * FROM tab02"); //執行SQL語句
%>
<CENTER>
<TABLE bgcolor=pink>
<TR bgcolor=silver>
<TD><B>編號</B></TD><TD><B>姓 名 </B></TD><TD><B>性 別</B></TD><TD><B> 年 齡</B></TD><TD><B>地 址</B></TD>
</TR>
<%
//利用while循環將數據表中的記錄列出
while (rs.next())
{
%>
<TR bgcolor=white>
<TD><B><%= rs.getString("id") %></B></TD>
<TD><B><%= rs.getString("name") %></B></TD>
<TD><B><%= rs.getString("sex") %></B></TD>
<TD><B><%= rs.getString("age") %></B></TD>
<TD><B><%= rs.getString("addr") %></B></TD>
</TR>
<%
}
rs.close(); //關閉ResultSet對象
stmt.close(); //關閉Statement對象
con.close(); //關閉Connection對象
%>
</TABLE>
</CENTER>
<h3><a href="jsp1.jsp">返回</a></h3>
</body>
</html>

jsp1:

<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="com.borland.internetbeans.*,com.borland.dx.dataset.*,com.borland.dx.sql.dataset.*" %>
<%@ taglib uri="/internetbeans.tld" prefix="ix" %>
<%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql" %>
<html>
<head>
<title>
jsp1
</title>
</head>
<jsp:useBean id="jsp1BeanId" scope="session" class="test.Jsp1Bean" />
<jsp:setProperty name="jsp1BeanId" property="*" />
<body bgcolor="#ff00ff">
<h1>
JBuilder Generated JSP
<br>
<hr>
這是第一次使用JSP技術 !!!----2004/3/1--南昌大學軟件學院----
</h1>
<h3><a href="tmp/page_1.htm">另一頁</a></h3>
<br>
<h3><a href="jsp2.jsp">下一頁</a></h3>
<br>
<h3><a href="DBBean.jsp">數據庫</a></h3>
<form method="post">
<br>Enter new value : <input name="sample"><br>
<br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
<br>
Value of Bean property is :<jsp:getProperty name="jsp1BeanId" property="sample" />
</form>
<h3><a href="jsp4.jsp">登錄</a></h3>
</body>
</html>

Jsp1Bean.java

package test;
/*aaaaaaa
bbbbbbb
ccccccccc*/
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Jsp1Bean {
private String sample = "Start value";
//Access sample property
public String getSample() {
return sample;
}
//Access sample property
public void setSample(String newValue) {
if (newValue!=null) {
sample = newValue;
}
}
}

jsp2:

<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="java.sql.*" %>
<%@ page language="java" %>
<HTML>
<HEAD>
<TITLE>順序取得數據</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE = 5 COLOR = blue>順序取得數據</FONT>
</CENTER>
<BR>
<HR>
<BR>
<CENTER>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //載入驅動程序類別
Connection con = DriverManager.getConnection("jdbc:odbc:zjyds1"); //建立數據庫鏈接
Statement stmt = con.createStatement(); //建立Statement對象
ResultSet rs; //建立ResultSet(結果集)對象
rs = stmt.executeQuery("SELECT * FROM tab01"); //執行SQL語句
%>
<TABLE bgcolor=pink>
<TR bgcolor=silver>
<TD><B>學 號</B></TD><TD><B>姓 名 </B></TD><TD><B>性 別 </B></TD><TD><B>年 齡 </B></TD><TD><B>地 址</B></TD>
</TR>
<%
//利用while循環將數據表中的記錄列出
while (rs.next())
{
%>
<TR bgcolor=white>
<TD><B><%= rs.getString("id") %></B></TD>
<TD><B><%= rs.getString("name") %></B></TD>
<TD><B><%= rs.getString("sex") %></B></TD>
<TD><B><%= rs.getString("age") %></B></TD>
<TD><B><%= rs.getString("addr") %></B></TD>
</TR>
<%
}
rs.close(); //關閉ResultSet對象
stmt.close(); //關閉Statement對象
con.close(); //關閉Connection對象
%>
</TABLE>
</CENTER>
</BODY>
</HTML>

jsp3:

<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>
jsp3
</title>
</head>
<jsp:useBean id="jsp3BeanId" scope="session" class="test.Jsp3Bean" />
<jsp:setProperty name="jsp3BeanId" property="*" />
<body bgcolor="#ffffc0">
<h1>
JBuilder Generated JSP
</h1>
<form method="post">
<br>Enter new value : <input name="sample"><br>
<br><br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
<br>
Value of Bean property is :<jsp:getProperty name="jsp3BeanId" property="sample" />
</form>
</body>
</html>

jsp4:

<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>
登錄
</title>
</head>
<body bgcolor="#ffffc0">
<form method="POST" action="jsp6.jsp">
<p align="center">
用戶名:<input type="text" name="username" size="20"></p>
<p align="center">
密 碼:<input type="password" name="password" size="20"></p>
<p align="center">
<input type="radio" value="manage" checked name="select">
管理  
<input type="radio" name="select" value="statistic">統計</p>
<p align="center"><input type="submit" value="登 錄" name="login">
<input type="reset" value="重 寫" name="reset"></p>
</form>
</body>
</html>

jsp6:

<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>
接收數據
</title>
</head>
<body bgcolor="#ffffff">
<%
String user,pwd,choice;
user=request.getParameter("username");
pwd=request.getParameter("password");
choice=request.getParameter("select");
if(choice.equals("manage")){
//user select manage.
%>
<jsp:forward page="jsp7.jsp">
<jsp:param name="username" value="<%=user%>"/>
<jsp:param name="password" value="<%=pwd%>"/>
</jsp:forward>
<%
}else{
//user select statistic
%>
<jsp:forward page="jsp8.jsp">
<jsp:param name="username" value="<%=user%>"/>
<jsp:param name="password" value="<%=pwd%>"/>
</jsp:forward>
<%
}
%>
</body>
</html>

jsp7:

<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>
jsp7
</title>
</head>
<body bgcolor="#ffffff">
<h1>
這是管理頁 !!!
</h1>
<br>
<%
String user,pwd;
user=request.getParameter("username");
pwd=request.getParameter("password");
%>
username is: <%=user%><br>
password is: <%=pwd%><br>
</body>
</html>

jsp8:

<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>
jsp8
</title>
</head>
<body bgcolor="#ffffff">
<h1>
這是統計頁 !!!
</h1>
<br>
<%
String user,pwd;
//user=request.getParameter("username");
user=new String(request.getParameter("username").getBytes("ISO8859_1"));
pwd=request.getParameter("password");
%>
username is: <%=user%><br>
password is: <%=pwd%><br>
</body>
</html>

input.html

<HTML>
<HEAD>
<TITLE>網頁引導</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE = 5 COLOR = blue>網頁引導</FONT>
</CENTER>
<BR>
<HR>
<BR>
<FORM action="sendRedirect.jsp" method=post name=form1>
<P>姓名 : <INPUT name=inputName ></P>
<P>E-Mail : <INPUT name=inputE_Mail ></P>
<INPUT name=submit type=submit value=送出>
</FORM>
</BODY>
</HTML

sendRedirect.jsp:

<%@ page contentType="text/html; charset=GB2312" %>
<%@ page language="java" %>
<%
String Name = request.getParameter("inputName");
String E_Mail = request.getParameter("inputE_Mail");
if(Name.equals("") || E_Mail.equals(""))//檢查Name或E_Mail是否完成資料輸入
response.sendRedirect("sendRedirect.html"); //若未完成資料輸入則將網頁導向sendRedirect.html
%>
<HTML>
<HEAD>
<TITLE>網頁引導</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE = 5 COLOR = blue>網頁引導</FONT>
</CENTER>
<BR>
<HR>
<BR>
<P>您的大名是:
<%= Name %>
</P>
<P>E-Mail帳號為:
<%= E_Mail %>
</P>
<p><a href="../jsp1.jsp">返回</a></p>
</BODY>
</HTML>

sendRedirect.html:

<HTML>
<HEAD>
<TITLE>網頁引導</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE = 5 COLOR = blue>網頁引導</FONT>
</CENTER>
<BR>
<HR>
<BR>
<FORM action="sendRedirect.jsp" method=post name=form1>
<font size=5 color=red>
你輸入的信息不完整,請重新輸入!
</font>
<br>
<P>姓名 : <INPUT name=inputName ></P>
<P>E-Mail : <INPUT name=inputE_Mail ></P>
<INPUT name=submit type=submit value=送出>
</FORM>
</BODY>
</HTML>

希望本文所述對大家的JSP程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved