JSP简单添加,查询功能代码
本文实例讲述了JSP简单添加,查询功能。分享给大家供大家参考。具体如下:
JSP技术:
publicclassISOtoGb2312
{
publicstaticStringconvert(Stringstr)
{
try
{
byte<>bytesStr=str.getBytes("ISO-8859-1");
returnnewString(bytesStr,"gb2312");
}
catch(Exceptionex)
{
returnstr;
}
}
}
<%@pagecontentType="text/html;charset=GB2312"%> <html> <head> <title> 输入数据 </title> </head> <bodybgcolor="#ffffff"> <h1> 请输入数据: </h1> <hr> <formmethod="POST"action="insert.jsp"> <!--webbotbot="SaveResults"U-File="fpweb:///_private/form_results.txt" S-Format="TEXT/CSV"S-Label-Fields="TRUE"--> <p></p> <p></p> <p><imgborder="0"src="img/cisco.gif"width="70"height="81"> <fontsize="5"color="#0000FF"><b>学号: </b></font><inputtype="text"name="id"size="25"><imgborder="0"src="img/cisco.gif"width="70"height="81"> <fontsize="5"color="#0000FF"><b>性别</b></font><fontsize="5"color="#0000FF"><b>: </b></font><inputtype="text"name="sex"size="24"></p> <p><imgborder="0"src="img/cisco.gif"width="70"height="81"> <fontsize="5"color="#000080"><b>姓名: </b></font><inputtype="text"name="name"size="25"><imgborder="0"src="img/cisco.gif"width="70"height="81"> <fontsize="5"color="#0000FF"><b>年龄:</b></font><inputtype="text"name="age"size="24"></p> <p><imgborder="0"src="img/cisco.gif"width="70"height="81"> <fontsize="5"color="#000080"><b>地址: </b></font><inputtype="text"name="addr"size="84"></p> <p></p> <p> <inputtype="submit"value="提交"name="B1"style="font-size:14pt;font-weight:bold"> <inputtype="reset"value="全部重写"name="B2"style="font-size:14pt;font-weight:bold"> </p> </form> </body> </html>
<%@pagecontentType="text/html;charset=GB2312"%>
<%@pageimport="java.sql.*"%>
<%@pagelanguage="java"%>
<%@pageimport="test.ISOtoGb2312"%>
<html>
<head>
<title>
添加数据
</title>
</head>
<bodybgcolor="#ffffff">
<h1>
接收数据,添加到数据库.
</h1>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//载入驱动程序类别
Connectioncon=DriverManager.getConnection("jdbc:odbc:zjyds1");//建立数据库链接
Statementstmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
StringstrSQL;
strSQL="INSERTINTOtab02(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);
ResultSetrs;//建立ResultSet(结果集)对象
rs=stmt.executeQuery("SELECT*FROMtab02");//执行SQL语句
%>
<CENTER>
<TABLEbgcolor=pink>
<TRbgcolor=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())
{
%>
<TRbgcolor=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><ahref="jsp1.jsp">返回</a></h3>
</body>
</html>
jsp1:
<%@pagecontentType="text/html;charset=GB2312"%> <%@pageimport="com.borland.internetbeans.*,com.borland.dx.dataset.*,com.borland.dx.sql.dataset.*"%> <%@tagliburi="/internetbeans.tld"prefix="ix"%> <%@tagliburi="http://java.sun.com/jstl/sql"prefix="sql"%> <html> <head> <title> jsp1 </title> </head> <jsp:useBeanid="jsp1BeanId"scope="session"class="test.Jsp1Bean"/> <jsp:setPropertyname="jsp1BeanId"property="*"/> <bodybgcolor="#ff00ff"> <h1> JBuilderGeneratedJSP <br> <hr> 这是第一次使用JSP技术!!!----2004/3/1--南昌大学软件学院---- </h1> <h3><ahref="tmp/page_1.htm">另一页</a></h3> <br> <h3><ahref="jsp2.jsp">下一页</a></h3> <br> <h3><ahref="DBBean.jsp">数据库</a></h3> <formmethod="post"> <br>Enternewvalue:<inputname="sample"><br> <br> <inputtype="submit"name="Submit"value="Submit"> <inputtype="reset"value="Reset"> <br> ValueofBeanpropertyis:<jsp:getPropertyname="jsp1BeanId"property="sample"/> </form> <h3><ahref="jsp4.jsp">登录</a></h3> </body> </html>
Jsp1Bean.java
packagetest;
/*aaaaaaa
bbbbbbb
ccccccccc*/
/**
*<p>Title:</p>
*<p>Description:</p>
*<p>Copyright:Copyright(c)2004</p>
*<p>Company:</p>
*@authornotattributable
*@version1.0
*/
publicclassJsp1Bean{
privateStringsample="Startvalue";
//Accesssampleproperty
publicStringgetSample(){
returnsample;
}
//Accesssampleproperty
publicvoidsetSample(StringnewValue){
if(newValue!=null){
sample=newValue;
}
}
}
jsp2:
<%@pagecontentType="text/html;charset=GB2312"%>
<%@pageimport="java.sql.*"%>
<%@pagelanguage="java"%>
<HTML>
<HEAD>
<TITLE>顺序取得数据</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONTSIZE=5COLOR=blue>顺序取得数据</FONT>
</CENTER>
<BR>
<HR>
<BR>
<CENTER>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//载入驱动程序类别
Connectioncon=DriverManager.getConnection("jdbc:odbc:zjyds1");//建立数据库链接
Statementstmt=con.createStatement();//建立Statement对象
ResultSetrs;//建立ResultSet(结果集)对象
rs=stmt.executeQuery("SELECT*FROMtab01");//执行SQL语句
%>
<TABLEbgcolor=pink>
<TRbgcolor=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())
{
%>
<TRbgcolor=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:
<%@pagecontentType="text/html;charset=GB2312"%> <html> <head> <title> jsp3 </title> </head> <jsp:useBeanid="jsp3BeanId"scope="session"class="test.Jsp3Bean"/> <jsp:setPropertyname="jsp3BeanId"property="*"/> <bodybgcolor="#ffffc0"> <h1> JBuilderGeneratedJSP </h1> <formmethod="post"> <br>Enternewvalue:<inputname="sample"><br> <br><br> <inputtype="submit"name="Submit"value="Submit"> <inputtype="reset"value="Reset"> <br> ValueofBeanpropertyis:<jsp:getPropertyname="jsp3BeanId"property="sample"/> </form> </body> </html>
jsp4:
<%@pagecontentType="text/html;charset=GB2312"%> <html> <head> <title> 登录 </title> </head> <bodybgcolor="#ffffc0"> <formmethod="POST"action="jsp6.jsp"> <palign="center"> 用户名:<inputtype="text"name="username"size="20"></p> <palign="center"> 密码:<inputtype="password"name="password"size="20"></p> <palign="center"> <inputtype="radio"value="manage"checkedname="select"> 管理 <inputtype="radio"name="select"value="statistic">统计</p> <palign="center"><inputtype="submit"value="登录"name="login"> <inputtype="reset"value="重写"name="reset"></p> </form> </body> </html>
jsp6:
<%@pagecontentType="text/html;charset=GB2312"%>
<html>
<head>
<title>
接收数据
</title>
</head>
<bodybgcolor="#ffffff">
<%
Stringuser,pwd,choice;
user=request.getParameter("username");
pwd=request.getParameter("password");
choice=request.getParameter("select");
if(choice.equals("manage")){
//userselectmanage.
%>
<jsp:forwardpage="jsp7.jsp">
<jsp:paramname="username"value="<%=user%>"/>
<jsp:paramname="password"value="<%=pwd%>"/>
</jsp:forward>
<%
}else{
//userselectstatistic
%>
<jsp:forwardpage="jsp8.jsp">
<jsp:paramname="username"value="<%=user%>"/>
<jsp:paramname="password"value="<%=pwd%>"/>
</jsp:forward>
<%
}
%>
</body>
</html>
jsp7:
<%@pagecontentType="text/html;charset=GB2312"%>
<html>
<head>
<title>
jsp7
</title>
</head>
<bodybgcolor="#ffffff">
<h1>
这是管理页!!!
</h1>
<br>
<%
Stringuser,pwd;
user=request.getParameter("username");
pwd=request.getParameter("password");
%>
usernameis:<%=user%><br>
passwordis:<%=pwd%><br>
</body>
</html>
jsp8:
<%@pagecontentType="text/html;charset=GB2312"%>
<html>
<head>
<title>
jsp8
</title>
</head>
<bodybgcolor="#ffffff">
<h1>
这是统计页!!!
</h1>
<br>
<%
Stringuser,pwd;
//user=request.getParameter("username");
user=newString(request.getParameter("username").getBytes("ISO8859_1"));
pwd=request.getParameter("password");
%>
usernameis:<%=user%><br>
passwordis:<%=pwd%><br>
</body>
</html>
input.html
<HTML> <HEAD> <TITLE>网页引导</TITLE> </HEAD> <BODY> <CENTER> <FONTSIZE=5COLOR=blue>网页引导</FONT> </CENTER> <BR> <HR> <BR> <FORMaction="sendRedirect.jsp"method=postname=form1> <P>姓名:<INPUTname=inputName></P> <P>E-Mail:<INPUTname=inputE_Mail></P> <INPUTname=submittype=submitvalue=送出> </FORM> </BODY> </HTML
sendRedirect.jsp:
<%@pagecontentType="text/html;charset=GB2312"%>
<%@pagelanguage="java"%>
<%
StringName=request.getParameter("inputName");
StringE_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>
<FONTSIZE=5COLOR=blue>网页引导</FONT>
</CENTER>
<BR>
<HR>
<BR>
<P>您的大名是:
<%=Name%>
</P>
<P>E-Mail帐号为:
<%=E_Mail%>
</P>
<p><ahref="../jsp1.jsp">返回</a></p>
</BODY>
</HTML>
sendRedirect.html:
<HTML> <HEAD> <TITLE>网页引导</TITLE> </HEAD> <BODY> <CENTER> <FONTSIZE=5COLOR=blue>网页引导</FONT> </CENTER> <BR> <HR> <BR> <FORMaction="sendRedirect.jsp"method=postname=form1> <fontsize=5color=red> 你输入的信息不完整,请重新输入! </font> <br> <P>姓名:<INPUTname=inputName></P> <P>E-Mail:<INPUTname=inputE_Mail></P> <INPUTname=submittype=submitvalue=送出> </FORM> </BODY> </HTML>
希望本文所述对大家的JSP程序设计有所帮助。