推荐:JSP的MYSQL数据库的代码最近研究了jsp的数据库操作问题。本来我是要拿oracle数据库来测试的。但是由于机子的内存限制,就换成了mysql数据库了。 第一次用jsp编数据库的连接,发现问题还真不少。刚开始是jdbc配置就有问题,后来数据库连接好了,中文设置也是个问题。后来还有sql语句
这个功能一共创建了两个javabean组件和一个jsp页面显示分页页面,第一个是处理以数据库连接的javabean,第一个javabean是处理分页查询结果的代码,第三个jsp是调用第二个javabean,显示分页查询结果!
//下面是连接mysql数据库的一个javabean的代码:
package data;
import java.sql.*;
public class LoginData{
Connection conn=null;
public LoginData(){
this.connect();
}
public Connection getConn(){
return this.conn;
}
public boolean connect(){
try{
//使用JDBC桥创建数据库连接
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//使用DriverManager类的getConnection()方法建立连接
//第一个参数定义用户名,第二个参数定义密码
this.conn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/logindemo?useUnicode=true&characterEncoding=gb2312","root","123456");
}catch(Exception ex){
ex.printStackTrace();
return false;
}
return true;
}
}
//下面是一个javabean的处理mysql数据库的分页显示查询的代码
package data;
import java.sql.*;
import java.util.*;
public class strongSplitPage
{
private Connection conn=null;
private Statement stmt=null;
private ResultSet rs=null;
private ResultSetMetaData rsmd=null;
//sql 查询语句
private String sqlStr;
//总纪录数目
private int rowCount;
//所分得逻辑页数
private int pageCount;
//每页显示的纪录数目
private int pageSize;
//定义表的列数目
private int columnCount;
private int irows;
public void initialize(String sqlStr,int pageSize,int showPage)
{
this.sqlStr=sqlStr;
this.irows=pageSize*(showPage-1);
this.pageSize=pageSize;
try
{
LoginData loginData=new data.LoginData();
this.conn=loginData.getConn();
this.stmt=this.conn.createStatement();
this.rs=this.stmt.executeQuery(this.sqlStr);
this.rsmd=this.rs.getMetaData();
if(this.rs!=null)
{
this.rs.last();
this.rowCount=this.rs.getRow();
this.rs.first();
this.columnCount=this.rsmd.getColumnCount();
this.pageCount=(this.rowCount-1)/this.pageSize+1;
this.rs.close();
this.stmt.close();
}
this.sqlStr=this.sqlStr+" limit "+this.irows+","+this.pageSize;
this.stmt=this.conn.createStatement();
this.rs=this.stmt.executeQuery(this.sqlStr);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
public Vector getPage()
{
Vector vData=new Vector();
try
{
if(this.rs!=null)
{
while(this.rs.next())
{
String[] sData=new String[this.columnCount];
for(int j=0;j
{
sData[j]=this.rs.getString(j+1);
}
vData.addElement(sData);
}
this.rs.close();
this.stmt.close();
this.conn.close();
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return vData;
}
//获得页面总数
public int getPageCount()
{
return this.pageCount;
}
//获得数据表中总纪录数
public int getRowCount()
{
return this.rowCount;
}
}
//下面是jsp显示分页页面
//显示每页的纪录数
int pageSize=10;
String sqlStr="";
//当前页
int showPage=1;
%>
sqlStr="select * from userinfo order by id ";
String strPage=null;
//获得跳转到的页面
strPage=request.getParameter("showPage");
if(strPage==null){
showPage=1;
pages.initialize(sqlStr,pageSize,showPage);
}else{
try{
showPage=Integer.parseInt(strPage);
pages.initialize(sqlStr,pageSize,showPage);
}catch(NumberFormatException ex){
showPage=1;
pages.initialize(sqlStr,pageSize,showPage);
}
if(showPage<1){
showPage=1;
pages.initialize(sqlStr,pageSize,showPage);
}
if(showPage>pages.getPageCount()){
showPage=pages.getPageCount();
pages.initialize(sqlStr,pageSize,showPage);
}
}
//取得要显示的数据集合
Vector vData=pages.getPage();
%>
分页显示个人基本信息
编号 | 学号 | 姓名 |
---|
for(int i=0;i
{
//显示数据数
String[] sData=(String[])vData.get(i);
%>
}
%>
共条 条/页 第页/共页 [首页]
//判断“上一页”链接是否要显示
if(showPage>1){
%>
[上一页]
}
else{
%>
[上一页]
}
//判断“下一页”链接是否显示
if(showPage
{
%>
[下一页]
}
else{
%>
[下一页]
}
%>
[尾页] 转到
for(int x=1;x<=pages.getPageCount();x++)
{
%>
if(showPage==x){
out.println("selected");
}
%> >
}
%>
页
分享:jsp验证码使用jsp验证码使用 yanzhe,jsp %@ page autoFlush=false import=java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*% %@ page import=org.apache.commons.lang.RandomStringUtils% % RandomStringUtils rs=new RandomStringUtils(); Str