JSP database Action routines (use Bean)

Source: Internet
Author: User
Tags getmessage odbc rowcount split stmt
js| Data | database


-Data pagination Display-JDBC 2.0:ODBC
Through the JDBC:ODBC can realize the JSP to the database operation, in this example I wrote the connection of the database in a JavaBean, can realize to reuse
pagetest.jsp file:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<% @page contenttype= "text/html;charset=gb2312"%>
<jsp:usebean id= "Htool" scope= "session" class= "Zbean". Htmltool "/>
<jsp:usebean id= "Jodb" scope= "session" class= "Zbean". Jodb "/>
<title> Database Paging Test </title>
<%
Defining the ResultSet Class
Java.sql.ResultSet rst;

Set ODBC data source
Jodb.setconnstr ("Jdbc:odbc:jtest", "", "");

Set JDBC Driver
Jodb.setdbdriver ("Sun.jdbc.odbc.JdbcOdbcDriver");

Executes the SQL statement, calling the Jodb class's Execute method
Rst=jodb.execute ("SELECT * from Gbook");
%>

<%

Int startrownum;
int pagesize=10;
Rst.last ();
int Rowcount=rst.getrow ();
int pagecount= (rowcount+pagesize-1)/pagesize;
int intpage;
String strpage=request.getparameter ("page");
if (strpage==null)
{
intpage=1;
}
Else
{
Intpage=java.lang.integer.parseint (strpage);
if (intpage<1) intpage=1;
if (intpage>pagecount) Intpage=pagecount;
}
startrownum= (intPage-1) *pagesize+1;
%>
<body>

<div align= "center" >
<center>
<p> Database Paging Test </p>
<p><%= htool.getstr (Jodb.pagestr (Intpage,pagecount, "pagetest.jsp?", "en")%></p>
<table border= "1" width= "bordercolorlight=" #000000 "cellspacing=" 0 "cellpadding=" 2 "bordercolordark=" #FFFFFF ">
<tr>
<TD width= "><font" size= "2" > Number </font></td>
<TD width= "><font" size= "2" > Name </font></td>
<TD width= "174" ><font size= "2" > E-mail </font></td>
<TD width= "><font" size= "2" > Message </font></td>
</tr>

<%
for (int i=0;i<pagesize;i++) {

Rst.absolute (Startrownum+i);

if (Rst.isafterlast ())
{
Break
}

%>
<tr>
<TD width= "><%= Rst.getlong" ("id")%> </td>
<TD width= "><%=" rst.getstring ("name")%> </td>
<TD width= "174" ><%= rst.getstring ("email")%> </td>
<TD width= "><%=" rst.getstring ("Pnote")%> <%= (Rst.getrow)%></td>
</tr>
<%
}
%>
</table>
</center>
</div>

</body>

Jodb.java files are as follows:

Package Zbean;
Import java.sql.*;
Import zbean.*;

public class Jodb
{
Public String sdbdriver= "Sun.jdbc.odbc.JdbcOdbcDriver";
Public String sConnStr;
public long Count;
String uid;
String pwd;
Connection Conn=null;
ResultSet Rs=null;

Public Jodb ()
{
Try
{
Class.forName (Sdbdriver);
}
catch (Java.lang.ClassNotFoundException e)
{
System.err.println ("Jodb ():" +e.getmessage ());
}
}

public void Setdbdriver (String y)
{
Sdbdriver=y;
}

public void Setconnstr (String x,string z,string a)
{
Sconnstr=x;
Uid=z;
Pwd=a;
}

Public ResultSet execute (String sql)
{
Rs=null;

Try
{
Conn=drivermanager.getconnection (SCONNSTR,UID,PWD);
Statement Stmt=conn.createstatement (Java.sql.resultset.type_scroll_sensitive,java.sql.resultset.concur_read_ only);
Rs=stmt.executequery (SQL);
}
catch (SQLException ex)
{
System.err.println ("Jodb.execute ():" +ex.getmessage ());
}
Return RS;
}

Public long Update (String SQL)
{
Long x=0;
Try
{
Conn=drivermanager.getconnection (SCONNSTR);
Statement stmt=conn.createstatement ();
X=stmt.executeupdate (SQL);
}
catch (SQLException ey)
{
System.err.println ("Jodb.update ():" +ey.getmessage ());
}
return x;
}

Public String pagestr (int page,int pagecount,string url,string showstr)
{
String str= "Page: (" +page+ "/" +pagecount+ ")";
String str= "";
String Fstr;
String pstr;
String Nstr;
String Lstr;
int page=currpage;
int pagecount=pagect;

if (showstr== "cn")
{
fstr= "First page";
Pstr= "Previous page";
nstr= "Next page";
lstr= "last Page";
}
else if (showstr== "en")
{
Fstr= "a";
Pstr= "Previous";
Nstr= "Next";
Lstr= "Last";
}
Else
{
String[] Temp_array=split (Showstr, ",");
if (temp_array==null)
{
str= "Please input String like:" First,previous,next,last ";
return str;
}
FSTR=TEMP_ARRAY[0];
PSTR=TEMP_ARRAY[1];
NSTR=TEMP_ARRAY[2];
LSTR=TEMP_ARRAY[3];
}
/*
int npage;
npgae=page+1;
int ppage;
ppage=page-1;
if (Npage>pagecount)
{Npae=pagecount;}
if (ppgae<1)
{ppage=1;}
*/
if (page==1) {
str=str+ "<a href=" +url+ "page=" + (page+1) + ">" +nstr+ "</a>";
str=str+ "<a href=" +url+ "page=" +pagecount+ ">" +lstr+ "</a>";
}
if (Page==pagecount) {
str=str+ "<a href=" +url+ "page=1>" +fstr+ "</a>";
str=str+ "<a href=" +url+ "page=" + (page-1) + ">" +pstr+ "</a>";
}
if (Page>1&&page<pagecount) {
str=str+ "<a href=" +url+ "page=1>" +fstr+ "</a>";
str=str+ "<a href=" +url+ "page=" + (page-1) + ">" +pstr+ "</a>";
str=str+ "<a href=" +url+ "page=" + (page+1) + ">" +nstr+ "</a>";
str=str+ "<a href=" +url+ "page=" +pagecount+ ">" +lstr+ "</a>";
}

return str;
}

Public string[] Split (String str,string Strin)
{
Char[] Temp_array;
Temp_array=str.tochararray ();
int Strlength=str.length ();
int Strinlength=strin.length ();
int strintimes=0;
int strindex[]=new Int[strlength];

int i=0;
int ii=0;
while (I<=strlength-strinlength)
{
String temp_str= "";
for (int j=i;j<i+strinlength;j++)
{
TEMP_STR=TEMP_STR+TEMP_ARRAY[J];
}
if (Temp_str.equals (Strin))
{
strintimes++;
Strindex[ii]=i;
I=i+strinlength;
ii++;
}
Else
{
i++;
}

}

if (strintimes<1)
{
String[] Back_str=null;
return back_str;
}
Else
{
String Back_str[]=new string[strintimes+1];
Back_str[0]=str.substring (0,strindex[0]);
for (int k=1;k<strintimes;k++)
{
Back_str[k]=str.substring (Strindex[k-1]+strinlength,strindex[k]);
}
Back_str[strintimes]=str.substring (Strindex[strintimes-1]+strinlength,str.length ());
return back_str;
}

}

}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.