- 论坛徽章:
- 0
|
下面是我连接数据库的文件link.java:
package data;
import java.io.*;
import java.sql.*;
public class link
{
String user="sa";
String url="jdbc:microsoft:sqlserver://IP:1433;DatabaseName=test";
String pwd="shujuku";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public ResultSet link(String sql)
{
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
conn= DriverManager.getConnection(url,user,pwd); stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
}
catch(Exception e) {
System.err.println("link(): " + e.getMessage());
}
return rs;
}
public void close() throws SQLException
{
stmt.close();
conn.close();
}
}
下面是jsp页面:index.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.text.*"%>
<%@ page language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>漂亮的企业网站</title>
<link href="image/css.css" rel="stylesheet" type="text/css">
</head>
<jsp:useBean id="link" scope="page" class="data.link" />
<body>
<marquee style='BOTTOM: 0px; FONT-WEIGHT: 120px; HEIGHT:130px; TEXT-ALIGN: left; TOP: 0px' id='news' scrollamount='1' scrolldelay='10' behavior='loop' direction='up' border='0' onMouseOver='this.stop()' onMouseOut='this.start()'>
<%
String sql="Select * from product where pro_name='gg'";
ResultSet rst = link.link(sql);
while (rst.next()) {
String dd = rst.getString("id");
String title = rst.getString("title");
%>
<a href=affiche.jsp?id=<%=dd%> class="2"><%=title%></a><br><br>
<%}
rst.close();
link.close();
%>
</marquee>
<table width="100%" border="0" cellspacing="8" cellpadding="0">
<%
String sql2="Select * from product where pro_name='news' order by id desc";
int i;
ResultSet rs_news = link.link(sql2);
i=0;
while((i<7) && (rs_news.next())){
String id = rs_news.getString("id");
String title = rs_news.getString("title");
String time = rs_news.getString("pub_date");
%>
<tr>
<td width="5%"><div align="center"><img src="image/20.jpg" width="6" height="9"></div></td>
<td width="70%"><a href="news_more.jsp?id=<%=id%>" class="2"><%=title%></a></td>
<td width="25%" style="font-size=12px">[<%=time%>]</td>
</tr>
<%
i=i+1;
}
rs_news.close();
link.close();
%>
</table>
</body>
</html>
显示的错误如下:
java.lang.NullPointerException
at _index__jsp._jspService(/index.jsp:21)
at com.caucho.jsp.JavaPage.service(JavaPage.java:75)
at com.caucho.jsp.Page.pageservice(Page.java:549)
at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:184)
at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:149)
at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:221)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:257)
at com.caucho.server.port.WorkerThread.run(WorkerThread.java:123)
at java.lang.Thread.run(Thread.java:534) |
|