jsp与mysql数据库相连,怎么样使数据库中的表在页面中出现?

就是页面中出现的表格是,数据库中建立的一个表,怎么样把它读取出来

在后台将数据查出来存入List中,并将List传给jsp页面。在页面中使用jslt输出List中的内容。追问

请问具体的代码是怎么样的呢?可以教我一下吗?

追答

由于你没有给出你的表,我假设你的表如下:
student(stuno,stuname.stuclassid,stusex);
第一步:首先建一个student实体类,属性同表student,生产所有的getter和setter方法;
代码略;
第二步:然后创建一个针对student实体的数据库访问类,并实现查询方法;
public class studentDao{
private Stirng driver="com.mysql.jdbc.Driver";
private String url="jdbc:mysql://localhost/3306/test"; //test为数据库名,请修改为你的数据库名
private String user="username"; //用户名
private String pwd="userpwd"; //密码

Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;

public List findAll(){
class.forName(driver);
con=DriverManager.getConnection(url,user,pwd);
String sql="select * from student";
pstmt=con.PrepareStatement(sql);
rs=pstmt.executeQuery();
List list=new ArrayList();
if(null!=rs){
while(rs,next()){
student stu=new student(rs.getInt("stuno"),rs.getString("stuname"),rs.getInt("stuclassid"),rs.getString("stusex"));
list.add(stu);
}
}finally{
rs.close();
pstmt.close();
con.close();
}
return list;
}
}
//由于在网吧没有工具,所有代码中的运行时异常你自己捕获一下,导一下包就OK了。
第三步:在JSP页面中加入一下代码:
导包:

${student.stuno}|${student.stuname}|${student.stuclassid}|${student.stusex}

第四步:部署tomcat服务器,在浏览器查看结果

温馨提示:内容为网友见解,仅供参考
第1个回答  2012-02-29
看上面那么详细,应该是对的,试试!
第2个回答  2012-02-28
没有分。
相似回答