把数据库的数据保存在response、session、application,然后在页面使用struts标签、jsp标签等显示,或者用<%。
对于你的这个问题,建议你学一下系统的javaee体系,应为把数据读到页面不是那么简单的,需要jdbc链接数据库,需要一个tomcat服务器,需要数据库jar包、、、、、、
显示mssqlserver所有表:
use mssqlserver
select from sysobjects
这个sysobjects是系统表,保存的是所有表的信息
取单个表所有字段信息的Sql语句:
declare @objid int,@objname char(40)
set @objname = 'Talbename'
select @objid = id from sysobjects where id = object_id(@objname)
select 'Column_name' = name from syscolumns where id = @objid order by colid
注意:Tablename是你要选择的表名
如果你要显示所有的表,并且每个表的结构的话,那需要写一个综合的存储过程,用一个循环就可以了
把数据库得到的数据封装在javabean中,再在jsp页面中创建这个javabean的实例,然后用这个实例调用javabean里面的getXXX(属性名)方法得到数据显示出来就行了;也可以直接在jsp页面中用
<useBean id="" class="">这种jsp标准动作来引用javabean从而得到数据并显示。
我这里用的是JSP+JavaBean+Servlet+MSSQLServer2000实现分页查询。希望能够办到你!
====================================================================
数据库 *** 作Bean:DBManagejava
package comstudentdb;
import javasqlConnection;
import javasqlDriverManager;
import javasqlStatement;
import javasqlResultSet;
import javasqlSQLException;
public class DBManage
{
public Connection getCon()throws ClassNotFoundException,SQLException{
ClassforName("sunjdbcodbcJdbcOdbcDriver");
Connection con = DriverManagergetConnection("jdbc:odbc:sun","sa","");
return con;
}
public Statement getSt()throws ClassNotFoundException,SQLException{
Connection con = thisgetCon();
Statement st = concreateStatement(1004,1007);
return st;
}
public ResultSet getResult(String sql)throws ClassNotFoundException,SQLException{
Statement st = thisgetSt();
ResultSet rs = stexecuteQuery(sql);
return rs;
}
public int getUpdate(String sql)throws ClassNotFoundException,SQLException{
Statement st = thisgetSt();
int result = stexecuteUpdate(sql);
return result;
}
}
分页Bean:PageBean
package comcommon;
public class PageBean
{
public int getPageCount(int totalCount,int pageSize)
{
int pageCount = totalCount % pageSize == 0 totalCount / pageSize : totalCount / pageSize + 1;
return pageCount;
}
public int getCurrentPage(String sp,int pageCount)
{
int currentPage = 1;
if(sp!=null)
{
try{
currentPage = IntegerparseInt(sp);
}catch(NumberFormatException nfe){
currentPage = 1;
}
}
if(currentPage < 1)
{
currentPage = 1;
}
if(currentPage > pageCount)
{
currentPage = pageCount;
}
return currentPage;
}
}
服务类:StudentServicejava
package comstudentstuservice;
import javasqlResultSet;
import javasqlSQLException;
import javautilList;
import javautilArrayList;
import comstudentdbDBManage;
import comstudentvoStuVO;
public class StudentService
{
comstudentdbDBManage dbm = new comstudentdbDBManage();
public int getTotalCount()throws ClassNotFoundException,SQLException
{
int totalCount = 0;
String sql = "select count() from stu";
ResultSet rs = dbmgetResult(sql);
boolean b = rsnext();
if(b)
{
totalCount = rsgetInt(1);
}
return totalCount;
}
public List getStudentList(int currentPage,int pageSize)throws ClassNotFoundException,SQLException
{
List<StuVO> stuList = new ArrayList<StuVO>();
String sql = "select from stu";
ResultSet rs = dbmgetResult(sql);
int position = (currentPage-1)pageSize+1;
rsabsolute(position);
for(int i = 0 ; i < pageSize ; i ++)
{
if(rsisAfterLast()==false)
{
int id = rsgetInt(1);
String name = rsgetString(2);
String sex = rsgetString(3);
int age = rsgetInt(4);
StuVO stu = new StuVO();
stusetId(id);
stusetName(name);
stusetSex(sex);
stusetAge(age);
stuListadd(stu);
rsnext();
}
}
return stuList;
}
}
数据表对应的实体类VO:StuVOjava
package comstudentvo;
public class StuVO
{
int id;
String name;
String sex;
int age;
public void setId(int id)
{
thisid = id;
}
public int getId()
{
return thisid;
}
public void setName(String name)
{
thisname = name;
}
public String getName()
{
return thisname;
}
public void setSex(String sex)
{
thissex = sex;
}
public String getSex()
{
return thissex;
}
public void setAge(int age)
{
thisage = age;
}
public int getAge()
{
return thisage;
}
}
查询的Servlet:StudentServletjava
package comstudentservlet;
import javaxservletRequestDispatcher;
import javaxservletServletException;
import javaxservlet>
以上就是关于如何从数据库里把数据显示在JSP页面上全部的内容,包括:如何从数据库里把数据显示在JSP页面上、在jsp(java)中如何显示数据库中所有的表、怎么用java在表单上把数据库的数据显示出来等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)