救命!!数据库连接问题
我做了一个DBConnection.java用来连接mssql,可是就是有问题!求教!!
以下是DBConnection.java内容:
package db;
import java.sql.*;
import javax.sql.*;
import java.io.*;
public class DBConnection
{
private Connection con=null;
private Statement stm=null;
private ResultSet rs=null;
/**
*construction
*/
public DBConnection()
{
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
try
{
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1443;fight;","sa","");
}
catch(SQLException e)
{
System.out.println(e);
}
}
/**
*Close the DB Connection
*/
public void close() throws SQLException
{
con.close();
}
/**
*execute an sql statement,it include delete,updata or insert
*@param sql
*return either the row of the delete,insert,updata,or 0 for return nothing
*/
public int updataDB(String sql) throws SQLException
{
int i=0;
stm=con.createStatement();
i=stm.executeUpdate(sql);
return i;
}
/**
*execute an query sql statement
*@param sql
*return the all row of the ResultSet
*/
public ResultSet queryDB(String sql) throws SQLException
{
stm=con.createStatement();
rs=stm.executeQuery(sql);
return rs;
}
}
测试的jsp文件:
<%@ page contentType="text/html;charset=GB2312" language="java" %>
<!--db.DBConnection-->
<%@ page import="java.sql.*"%>
<%@ page import="db.DBConnection"%>
<html>
<head>
<title>test</title>
</head>
<body>
<jsp:useBean id="myCon" class="db.DBConnection" scope="application"/>
<%
ResultSet rs=null;
String sql="select *from application";
try
{
rs=myCon.queryDB(sql);
rs.next();
}
catch (SQLException e)
{
System.out.println(e);
}
myCon.close();
%>
hello
</body>
</html>
运行抱错:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
db.DBConnection.queryDB(DBConnection.java:59)
org.apache.jsp._1_jsp._jspService(_1_jsp.java:69)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.27 logs.
我怎么看也想不明白到底哪地方错了,请大虾指教啊!!!