在一个系统页面中需要查询另外一个数据库中的数据,所以在原页面 include了一个java ,内容是这样的<%@ page contentType="text/html; charset=utf-8" language="java"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.util.Date" %><%
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = java.sql.DriverManager.getConnection("jdbc:mysql://*.*.*.*:3306/discuz", "root", "*");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// 执行SQL查询语句方法,返回结果集对象
String sql = "select * from cdb_posts order by cdb_posts.dateline desc";
Statement stmt = null;
ResultSet rs = null;
try {
stmt= conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}%>
<%
while(rs.next()){
%>
<tr vAlign=top style="cursor:hand" onclick="showDetail(this, '', '');">
<td height=10 width="10" align="right" valign="bottom">
<image width="10" height="10" src="/images/arrow.gif" />
</td>
<td height=10 align="left" valign="bottom" class="gray-dashed">
<font style="font-size:12px;color:#353535">
<%=rs.getString("subject") %>
</font>
</td>
<td height=10 align="right" valign="bottom" class="gray-dashed">
<font style="font-size:12px;color:#353535">
<% Date d = new Date(rs.getTimestamp("dateline").getTime()*1000) ; %>
[<%= d %>
</font>]
</td>
<td width="5"> </td>
</tr>
<%
}
%>这是原始的代码,放在apache-tomcat-6.0.24\webapps\ROOT 下测试 ,页面中显示出了数据库中的所有数据 说明代码是没有问题,但是放到测试机上面去就是不行,调试发现 conn为null,但是在测试机中的该webproject下的lib我已经放置了 jar,不知道还是哪里的原因 conn对象总是获取不到呢
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.util.Date" %><%
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = java.sql.DriverManager.getConnection("jdbc:mysql://*.*.*.*:3306/discuz", "root", "*");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// 执行SQL查询语句方法,返回结果集对象
String sql = "select * from cdb_posts order by cdb_posts.dateline desc";
Statement stmt = null;
ResultSet rs = null;
try {
stmt= conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}%>
<%
while(rs.next()){
%>
<tr vAlign=top style="cursor:hand" onclick="showDetail(this, '', '');">
<td height=10 width="10" align="right" valign="bottom">
<image width="10" height="10" src="/images/arrow.gif" />
</td>
<td height=10 align="left" valign="bottom" class="gray-dashed">
<font style="font-size:12px;color:#353535">
<%=rs.getString("subject") %>
</font>
</td>
<td height=10 align="right" valign="bottom" class="gray-dashed">
<font style="font-size:12px;color:#353535">
<% Date d = new Date(rs.getTimestamp("dateline").getTime()*1000) ; %>
[<%= d %>
</font>]
</td>
<td width="5"> </td>
</tr>
<%
}
%>这是原始的代码,放在apache-tomcat-6.0.24\webapps\ROOT 下测试 ,页面中显示出了数据库中的所有数据 说明代码是没有问题,但是放到测试机上面去就是不行,调试发现 conn为null,但是在测试机中的该webproject下的lib我已经放置了 jar,不知道还是哪里的原因 conn对象总是获取不到呢
解决方案 »
- jboss升级报错
- 整合SSH 出现了配置的问题
- 服务器的jsp里取不到图片。本机没问题
- XML 无效字符
- 求教:一个字段输入值受到正则表达式约束,该在什么时候触发呢?
- Tomcat用得用得就布署不上去了! 请教!
- Struts 的Message Resources资源文件配置的问题
- 谁能告诉我数据库连接池!急急急!!!!!!!!!!!!!!
- 请教一个关于spring mvc hbiernate 的annotation定义事务的配置 位置的问题
- maven pom配置junit4.10 cmd命令报错、求解决啊
- JSP static变量问题 求解
- 手动添加structs2 支持报的错... 我选用的是2.1.6的lib
catch (ClassNotFoundException e) {
e.printStackTrace();
}
加上看看到底是什么
catch (Exception e) {
e.printStackTrace();
}
这是为嘛呢?
包 com.mysql.jdbc 不存在import com.mysql.jdbc.Driver;websphere.....
好像可以指定它采用哪个jdbc,你需要在was的控制台去配一下?
你的mysql的jar一定要添加到你项目的lib目录下..如果你不添加到你的项目lib目录下的话.拷贝你的项目的时候,你又没有把mysql的jar拷贝到其他机器,所以,你的项目在其他机器上运行就会有问题..
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.util.Date" %><%
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://10.222.1.104:3306/discuz", "root", "passw0rd");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String sql = "select t.* from (select * from cdb_posts ORDER by dateline desc ) t limit 5";
Statement stmt = null;
ResultSet rs = null;
try {
stmt= conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}%>
<% if(rs!=null){
while(rs.next()){
%>
<tr vAlign=top style="cursor:hand" onclick="showDetail(this, '', '');">
<td height=10 width="10" align="right" valign="bottom">
<image width="10" height="10" src="/images/arrow.gif" />
</td>
<td height=10 align="left" valign="bottom" class="gray-dashed">
<font style="font-size:12px;color:#353535">
<%=rs.getString("subject") %>
</font>
</td>
<td height=10 align="right" valign="bottom" class="gray-dashed">
<font style="font-size:12px;color:#353535">
<% Date d = new Date(rs.getTimestamp("dateline").getTime()) ; %>
[<%= d %>
</font>]
</td>
<td width="5"> </td>
</tr>
<%
}
rs.close();
}
%>这个jsp放到discuz中去,然后再java中的一个jsp 通过iframe 去引用它,不过可能要把jsp改成php的 ,有高手能“翻译”下么?
比如引入的一下.jar文件的路径变化了等等。
//这里少了东西了吧---- conn = db.getConnection();//你上面只是声明了一个//Connection conn = null; stmt= conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}