第一次做项目,遇到一难题,bbs项目,ie访问时无法取数据库内容 在做bbs项目时,mysql已经连到myeclipse,Tomcat也部署上了,javabean的封装也没有报异常,但在用ie访问时,数据库里面的帖子却无法取出来,实在想不出是什么原因 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你逐个将变量参数的值输出来看看,尤其是判定sql语句是否被执行了 你先在JAVABEAN里面测试一下 看能不能拿出数据 一步一步的去看 在myeclipse中的DB Browser里面已经看到数据运行结果了 程序的问题!index页面能否打开? 只有静态页面的内容package com.java.bbs;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;public class DB { public static Connection getConn(){ Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root","root"); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static Statement createStmt(Connection conn){ Statement stmt = null; try { stmt = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } return stmt; } public static ResultSet executeQuery(Statement stmt,String sql){ ResultSet rs = null; try { rs = stmt.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } public static int executeUpdate(Connection conn,String sql){ int set = 0; Statement stmt = null; try { stmt = conn.createStatement(); set = stmt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); }finally{ DB.close(stmt); } return set; } public static PreparedStatement prepareStmt(Connection conn,String sql){ PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static PreparedStatement prepareStmt(Connection conn,String sql,int autoGeneratedKeys){ PreparedStatement pstmt = null; try { pstmt = conn.prepareStatement(sql,autoGeneratedKeys); } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static void close(Connection conn){ if(conn!=null) try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void close(Statement stmt){ if(stmt!=null) try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void close(ResultSet rs){ if(rs!=null) try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } }} 一定是程序或者页面有问题,不可能是ie的问题,虽然ie有点烂 数据库里面一直都有数据,而且在myeclipse里面的DB Browser里面也查到了,连接也重新连了几次,jsp文件,javabean都不报错,反正用ie访问就是取不到数据库里面的帖子,我是第一次做项目,j2se和j2ee都是自学的,平时课多,自学的时间不多,学得也不是很好,谢谢大家的建议,我一个个慢慢试 这就是jsp文件<%@ page language="java" import="java.util.*,java.sql.*,com.java.bbs.*,java.io.*" pageEncoding="GB18030"%><%!private void tree(List<Article> articles,Connection conn,int id,int grade){ String sql = "select * from articles where pid="+id; Statement stmt = DB.createStmt(conn); ResultSet rs = DB.executeQuery(stmt,sql); try{ while(rs.next()){ Article a = new Article(); a.initFormRS(rs); a.setGrade(grade); articles.add(a); if(a.isLeaf){ tree(articles,conn,a.getId(),grade+1); }System.out.println(a.getPdate()); } }catch(SQLException e){ e.printStackTrace(); }finally{ DB.close(stmt); DB.close(rs); }} %><%List<Article> articles = new ArrayList<Article>();Connection conn = DB.getConn();tree(articles,conn,0,0);DB.close(conn);%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><title>Java|Java世界_中文论坛|ChinaJavaWorld技术论坛 : Java语言*初级版</title><meta http-equiv="content-type" content="text/html; charset=GBK"><link rel="stylesheet" type="text/css" href="images/style.css" title="Integrated Styles"><script language="JavaScript" type="text/javascript" src="images/global.js"></script><link rel="alternate" type="application/rss+xml" title="RSS" href="http://bbs.chinajavaworld.com/rss/rssmessages.jspa?forumID=20"><script language="JavaScript" type="text/javascript" src="images/common.js"></script></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody> <tr> <td width="140"><a href="http://bbs.chinajavaworld.com/index.jspa"><img src="images/header-left.gif" alt="Java|Java世界_中文论坛|ChinaJavaWorld技术论坛" border="0"></a></td> <td><img src="images/header-stretch.gif" alt="" border="0" height="57" width="100%"></td> <td width="1%"><img src="images/header-right.gif" alt="" border="0"></td> </tr> </tbody></table><br><div id="jive-forumpage"> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody> <tr valign="top"> <td width="98%"><p class="jive-breadcrumbs">论坛: Java语言*初级版 (模仿)</p> <p class="jive-description"> 探讨Java语言基础知识,基本语法等 大家一起交流 共同提高!谢绝任何形式的广告 </p> </td> </tr> </tbody> </table> <div class="jive-buttons"> <table summary="Buttons" border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td class="jive-icon"><a href="post.jsp"><img src="images/post-16x16.gif" alt="发表新主题" border="0" height="16" width="16"></a></td> <td class="jive-icon-label"><a id="jive-post-thread" href="http://bbs.chinajavaworld.com/post%21default.jspa?forumID=20">发表新主题</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&isBest=1"></a></td> </tr> </tbody> </table> </div> <br> <table border="0" cellpadding="3" cellspacing="0" width="100%"> <tbody> <tr valign="top"> <td><span class="nobreak"> 页: 1,316 - <span class="jive-paginator"> [ <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=0&isBest=0">上一页</a> | <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=0&isBest=0" class="">1</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=25&isBest=0" class="jive-current">2</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=50&isBest=0" class="">3</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=75&isBest=0" class="">4</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=100&isBest=0" class="">5</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=125&isBest=0" class="">6</a> | <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=50&isBest=0">下一页</a> ] </span> </span> </td> </tr> </tbody> </table> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tbody> <tr valign="top"> <td width="99%"><div class="jive-thread-list"> <div class="jive-table"> <table summary="List of threads" cellpadding="0" cellspacing="0" width="100%"> <thead> <tr> <th class="jive-first" colspan="3"> 主题 </th> <th class="jive-author"> <nobr> 作者 </nobr> </th> <th class="jive-view-count"> <nobr> 浏览 </nobr> </th> <th class="jive-msg-count" nowrap="nowrap"> 回复 </th> <th class="jive-last" nowrap="nowrap"> 最新帖子 </th> </tr> </thead> <tbody> <% for(Iterator<Article> it = articles.iterator();it.hasNext();){ Article a = it.next(); String preStr = " "; for(int i=0;i<a.getGrade();i++){ preStr+="----"; } %> <tr class="jive-even"> <td class="jive-first" nowrap="nowrap" width="1%"><div class="jive-bullet"> <img src="images/read-16x16.gif" alt="已读" border="0" height="16" width="16"> <!-- div--> </div></td> <td nowrap="nowrap" width="1%"> <a href="delete.jsp?id=<%=a.getId() %>&isLeaf=<%=a.isLeaf %>&pid<%=a.getPid() %>">DEL </a> </td> <td class="jive-thread-name" width="95%"><a id="jive-thread-1" href="articleDetail.jsp?id=<%=a.getId() %>">初学java遇一难题!!望大家能帮忙一下 谢谢了</a></td> <td class="jive-author" nowrap="nowrap" width="1%"><span class=""> <a href="http://bbs.chinajavaworld.com/profile.jspa?userID=226030">fei870407</a> </span></td> <td class="jive-view-count" width="1%"> 100</td> <td class="jive-msg-count" width="1%"> 5</td> <td class="jive-last" nowrap="nowrap" width="1%"><div class="jive-last-post"> <%=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getPdate()) %> <br> by: <a href="http://bbs.chinajavaworld.com/thread.jspa?messageID=780182#780182" title="jingjiangjun" style="">Ella »</a> </div></td> </tr> <%-- <tr class="jive-odd"> <td class="jive-first" nowrap="nowrap" width="1%"><div class="jive-bullet"> <img src="images/read-16x16.gif" alt="已读" border="0" height="16" width="16"> <!-- div--> </div></td> <td nowrap="nowrap" width="1%"> </td> <td class="jive-thread-name" width="95%"><a id="jive-thread-2" href="http://bbs.chinajavaworld.com/thread.jspa?threadID=744234&tstart=25">请 兄弟们指点下那里 错误,,,</a></td> <td class="jive-author" nowrap="nowrap" width="1%"><span class=""> <a href="http://bbs.chinajavaworld.com/profile.jspa?userID=226028">403783154</a> </span></td> <td class="jive-view-count" width="1%"> 52</td> <td class="jive-msg-count" width="1%"> 2</td> <td class="jive-last" nowrap="nowrap" width="1%"><div class="jive-last-post"> 2007-9-13 上午8:40 <br> by: <a href="http://bbs.chinajavaworld.com/thread.jspa?messageID=780172#780172" title="downing114" style="">downing114 »</a> </div></td> </tr> --%> <% } %> </tbody> </table> </div> </div> <div class="jive-legend"></div></td> </tr> </tbody> </table> <br> <br></div></body></html> 确实是够多的啊,建议先在你的处理程序中写一个main函数,逐个测试一下,再放到页面里 if(做个测试类,测试数据库连接是不是有问题,是否可以查出所需要的值){ 检查页面调用是否会有问题}else{ 查询部分处错,与页面无关,断点查询数据库查询方法,察看那一步出错了} ssh事务不能回滚啊。救救救救救救救救救!!!!!!!!!!!!! 求教一个JAVA关于URL的正则表达式 继承关系下对象属性 @WebService? 在servlet里如何得知服务器启动和即将关闭的信息?急~ (100分)求 用 schema 验证 XML 的例子 请问Tomcat+MySQL的JSP中文显示问题 100分求应该如何配置EJB的数据库访问 小弟我硕士毕业论文想写Web Service, 请教这方面些什么应用比较有深度 List里面放的是Object类型的对象,应该怎么取? jsp 实例化javaBean 对象的问题 现在的我时候看ssh框架了么
package com.java.bbs;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;public class DB {
public static Connection getConn(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root","root");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement createStmt(Connection conn){
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet executeQuery(Statement stmt,String sql){
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public static int executeUpdate(Connection conn,String sql){
int set = 0;
Statement stmt = null;
try {
stmt = conn.createStatement();
set = stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}finally{
DB.close(stmt);
}
return set;
}
public static PreparedStatement prepareStmt(Connection conn,String sql){
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static PreparedStatement prepareStmt(Connection conn,String sql,int autoGeneratedKeys){
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql,autoGeneratedKeys);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static void close(Connection conn){
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement stmt){
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs){
if(rs!=null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}}
<%@ page language="java" import="java.util.*,java.sql.*,com.java.bbs.*,java.io.*" pageEncoding="GB18030"%><%!
private void tree(List<Article> articles,Connection conn,int id,int grade){
String sql = "select * from articles where pid="+id;
Statement stmt = DB.createStmt(conn);
ResultSet rs = DB.executeQuery(stmt,sql);
try{
while(rs.next()){
Article a = new Article();
a.initFormRS(rs);
a.setGrade(grade);
articles.add(a);
if(a.isLeaf){
tree(articles,conn,a.getId(),grade+1);
}
System.out.println(a.getPdate());
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DB.close(stmt);
DB.close(rs);
}
}
%>
<%
List<Article> articles = new ArrayList<Article>();
Connection conn = DB.getConn();
tree(articles,conn,0,0);
DB.close(conn);%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Java|Java世界_中文论坛|ChinaJavaWorld技术论坛 : Java语言*初级版</title>
<meta http-equiv="content-type" content="text/html; charset=GBK">
<link rel="stylesheet" type="text/css" href="images/style.css" title="Integrated Styles">
<script language="JavaScript" type="text/javascript" src="images/global.js"></script>
<link rel="alternate" type="application/rss+xml" title="RSS" href="http://bbs.chinajavaworld.com/rss/rssmessages.jspa?forumID=20">
<script language="JavaScript" type="text/javascript" src="images/common.js"></script>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td width="140"><a href="http://bbs.chinajavaworld.com/index.jspa"><img src="images/header-left.gif" alt="Java|Java世界_中文论坛|ChinaJavaWorld技术论坛" border="0"></a></td>
<td><img src="images/header-stretch.gif" alt="" border="0" height="57" width="100%"></td>
<td width="1%"><img src="images/header-right.gif" alt="" border="0"></td>
</tr>
</tbody>
</table>
<br>
<div id="jive-forumpage">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="98%"><p class="jive-breadcrumbs">论坛: Java语言*初级版
(模仿)</p>
<p class="jive-description"> 探讨Java语言基础知识,基本语法等 大家一起交流 共同提高!谢绝任何形式的广告 </p>
</td>
</tr>
</tbody>
</table>
<div class="jive-buttons">
<table summary="Buttons" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="jive-icon"><a href="post.jsp"><img src="images/post-16x16.gif" alt="发表新主题" border="0" height="16" width="16"></a></td>
<td class="jive-icon-label"><a id="jive-post-thread" href="http://bbs.chinajavaworld.com/post%21default.jspa?forumID=20">发表新主题</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&isBest=1"></a></td>
</tr>
</tbody>
</table>
</div>
<br>
<table border="0" cellpadding="3" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td><span class="nobreak"> 页:
1,316 - <span class="jive-paginator"> [ <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=0&isBest=0">上一页</a> | <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=0&isBest=0" class="">1</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=25&isBest=0" class="jive-current">2</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=50&isBest=0" class="">3</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=75&isBest=0" class="">4</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=100&isBest=0" class="">5</a> <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=125&isBest=0" class="">6</a> | <a href="http://bbs.chinajavaworld.com/forum.jspa?forumID=20&start=50&isBest=0">下一页</a> ] </span> </span> </td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr valign="top">
<td width="99%"><div class="jive-thread-list">
<div class="jive-table">
<table summary="List of threads" cellpadding="0" cellspacing="0" width="100%">
<thead>
<tr>
<th class="jive-first" colspan="3"> 主题 </th>
<th class="jive-author"> <nobr> 作者
</nobr> </th>
<th class="jive-view-count"> <nobr> 浏览
</nobr> </th>
<th class="jive-msg-count" nowrap="nowrap"> 回复 </th>
<th class="jive-last" nowrap="nowrap"> 最新帖子 </th>
</tr>
</thead>
<tbody>
<%
for(Iterator<Article> it = articles.iterator();it.hasNext();){
Article a = it.next();
String preStr = " ";
for(int i=0;i<a.getGrade();i++){
preStr+="----";
}
%>
<tr class="jive-even">
<td class="jive-first" nowrap="nowrap" width="1%"><div class="jive-bullet"> <img src="images/read-16x16.gif" alt="已读" border="0" height="16" width="16">
<!-- div-->
</div></td>
<td nowrap="nowrap" width="1%">
<a href="delete.jsp?id=<%=a.getId() %>&isLeaf=<%=a.isLeaf %>&pid<%=a.getPid() %>">DEL </a>
</td>
<td class="jive-thread-name" width="95%"><a id="jive-thread-1" href="articleDetail.jsp?id=<%=a.getId() %>">初学java遇一难题!!望大家能帮忙一下 谢谢了</a></td>
<td class="jive-author" nowrap="nowrap" width="1%"><span class=""> <a href="http://bbs.chinajavaworld.com/profile.jspa?userID=226030">fei870407</a> </span></td>
<td class="jive-view-count" width="1%"> 100</td>
<td class="jive-msg-count" width="1%"> 5</td>
<td class="jive-last" nowrap="nowrap" width="1%"><div class="jive-last-post"> <%=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getPdate()) %> <br>
by: <a href="http://bbs.chinajavaworld.com/thread.jspa?messageID=780182#780182" title="jingjiangjun" style="">Ella »</a> </div></td>
</tr>
<%--
<tr class="jive-odd">
<td class="jive-first" nowrap="nowrap" width="1%"><div class="jive-bullet"> <img src="images/read-16x16.gif" alt="已读" border="0" height="16" width="16">
<!-- div-->
</div></td>
<td nowrap="nowrap" width="1%">
</td>
<td class="jive-thread-name" width="95%"><a id="jive-thread-2" href="http://bbs.chinajavaworld.com/thread.jspa?threadID=744234&tstart=25">请 兄弟们指点下那里 错误,,,</a></td>
<td class="jive-author" nowrap="nowrap" width="1%"><span class=""> <a href="http://bbs.chinajavaworld.com/profile.jspa?userID=226028">403783154</a> </span></td>
<td class="jive-view-count" width="1%"> 52</td>
<td class="jive-msg-count" width="1%"> 2</td>
<td class="jive-last" nowrap="nowrap" width="1%"><div class="jive-last-post"> 2007-9-13 上午8:40 <br>
by: <a href="http://bbs.chinajavaworld.com/thread.jspa?messageID=780172#780172" title="downing114" style="">downing114 »</a> </div></td>
</tr>
--%>
<%
}
%>
</tbody>
</table>
</div>
</div>
<div class="jive-legend"></div></td>
</tr>
</tbody>
</table>
<br>
<br>
</div>
</body>
</html>
if(做个测试类,测试数据库连接是不是有问题,是否可以查出所需要的值){
检查页面调用是否会有问题
}else{
查询部分处错,与页面无关,断点查询数据库查询方法,察看那一步出错了
}