JSP simple add query function code

  • 2021-07-06 11:35:54
  • OfStack

In this paper, the simple addition and query functions of JSP are described. Share it for your reference. The details are as follows:

JSP Technology:


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>
 Input data 
</title>
</head>
<body bgcolor="#ffffff">
<h1>
 Please enter data :
</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> Learning   No. :
</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> Sex   Don't </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> Last name   Name :
</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> Year   Age : </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> Ground   Address :
</b></font><input type="text" name="addr" size="84"> </p>
<p> </p>
<p>
<input type="submit" value=" Submit " name="B1" style="font-size: 14pt; font-weight: bold">
<input type="reset" value=" Rewrite all " name="B2" style="font-size: 14pt; font-weight: bold">
</p>
</form>
</body>
</html>


<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import = "java.sql.*"%>
<%@ page language = "java"%>
<%@ page import = "test.ISOtoGb2312"%>
<html>
<head>
<title>
 Add data 
</title>
</head>
<body bgcolor="#ffffff">
<h1>
 Receive data , Add to database .
</h1>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Load driver class 
Connection con = DriverManager.getConnection("jdbc:odbc:zjyds1"); // Establish a database link 
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; // Establish ResultSet( Result set ) Object 
rs = stmt.executeQuery("SELECT * FROM tab02"); // Execute SQL Statement 
%>
<CENTER>
<TABLE bgcolor=pink>
<TR bgcolor=silver>
<TD><B> Numbering </B></TD><TD><B> Last name   Name  </B></TD><TD><B> Sex   Don't </B></TD><TD><B>  Year   Age </B></TD><TD><B> Ground   Address </B></TD>
</TR>
<%
// Utilization while Loop to list the records in the data table 
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(); // Shut down ResultSet Object 
stmt.close(); // Shut down Statement Object 
con.close(); // Shut down Connection Object 
%>
</TABLE>
</CENTER>
<h3><a href="jsp1.jsp"> Return </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>
 This is the first 1 Secondary use JSP Technology  !!!----2004/3/1-- Nanchang University School of Software ----
</h1>
<h3><a href="tmp/page_1.htm"> Another 1 Page </a></h3>
<br>
<h3><a href="jsp2.jsp"> Under 1 Page </a></h3>
<br>
<h3><a href="DBBean.jsp"> Database </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"> Login </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> Sequential data acquisition </TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE = 5 COLOR = blue> Sequential data acquisition </FONT>
</CENTER>
<BR>
<HR>
<BR>
<CENTER>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Load driver class 
Connection con = DriverManager.getConnection("jdbc:odbc:zjyds1"); // Establish a database link 
Statement stmt = con.createStatement(); // Establish Statement Object 
ResultSet rs; // Establish ResultSet( Result set ) Object 
rs = stmt.executeQuery("SELECT * FROM tab01"); // Execute SQL Statement 
%>
<TABLE bgcolor=pink>
<TR bgcolor=silver>
<TD><B> Learning   No. </B></TD><TD><B> Last name   Name  </B></TD><TD><B> Sex   Don't  </B></TD><TD><B> Year   Age  </B></TD><TD><B> Ground   Address </B></TD>
</TR>
<%
// Utilization while Loop to list the records in the data table 
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(); // Shut down ResultSet Object 
stmt.close(); // Shut down Statement Object 
con.close(); // Shut down Connection Object 
%>
</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>
 Login 
</title>
</head>
<body bgcolor="#ffffc0">
<form method="POST" action="jsp6.jsp">
<p align="center">
 User name: <input type="text" name="username" size="20"></p>
<p align="center">
 Dense   Code: <input type="password" name="password" size="20"></p>
<p align="center">
<input type="radio" value="manage" checked name="select">
 Management   
<input type="radio" name="select" value="statistic"> Statistics </p>
<p align="center"><input type="submit" value=" Deng   Record " name="login">
<input type="reset" value=" Heavy   Write " name="reset"></p>
</form>
</body>
</html>

jsp6:


<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>
 Receive data 
</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>
 This is the admin page  !!!
</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>
 Input data 
</title>
</head>
<body bgcolor="#ffffff">
<h1>
 Please enter data :
</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> Learning   No. :
</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> Sex   Don't </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> Last name   Name :
</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> Year   Age : </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> Ground   Address :
</b></font><input type="text" name="addr" size="84"> </p>
<p> </p>
<p>
<input type="submit" value=" Submit " name="B1" style="font-size: 14pt; font-weight: bold">
<input type="reset" value=" Rewrite all " name="B2" style="font-size: 14pt; font-weight: bold">
</p>
</form>
</body>
</html>

0

input.html


<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>
 Input data 
</title>
</head>
<body bgcolor="#ffffff">
<h1>
 Please enter data :
</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> Learning   No. :
</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> Sex   Don't </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> Last name   Name :
</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> Year   Age : </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> Ground   Address :
</b></font><input type="text" name="addr" size="84"> </p>
<p> </p>
<p>
<input type="submit" value=" Submit " name="B1" style="font-size: 14pt; font-weight: bold">
<input type="reset" value=" Rewrite all " name="B2" style="font-size: 14pt; font-weight: bold">
</p>
</form>
</body>
</html>

1

sendRedirect.jsp:


<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>
 Input data 
</title>
</head>
<body bgcolor="#ffffff">
<h1>
 Please enter data :
</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> Learning   No. :
</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> Sex   Don't </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> Last name   Name :
</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> Year   Age : </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> Ground   Address :
</b></font><input type="text" name="addr" size="84"> </p>
<p> </p>
<p>
<input type="submit" value=" Submit " name="B1" style="font-size: 14pt; font-weight: bold">
<input type="reset" value=" Rewrite all " name="B2" style="font-size: 14pt; font-weight: bold">
</p>
</form>
</body>
</html>

2

sendRedirect.html:


<HTML>
<HEAD>
<TITLE> Web page guide </TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE = 5 COLOR = blue> Web page guide </FONT>
</CENTER>
<BR>
<HR>
<BR>
<FORM action="sendRedirect.jsp" method=post name=form1>
<font size=5 color=red>
 The information you entered is incomplete, please re-enter it! 
</font>
<br>
<P> Name  : <INPUT name=inputName ></P>
<P>E-Mail : <INPUT name=inputE_Mail ></P>
<INPUT name=submit type=submit value= Send out >
</FORM>
</BODY>
</HTML>

I hope this article is helpful to everyone's JSP programming.


Related articles: