救命!!数据库连接问题

keranbing 2005-05-10 01:23:03
我做了一个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.

我怎么看也想不明白到底哪地方错了,请大虾指教啊!!!

...全文
81 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
keranbing 2005-05-10
  • 打赏
  • 举报
回复
按照楼上所说,是可以,但在tomcat中为什么回有这么一条语句
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对
象名 'application' 无效。
这是什么意思呢???
曼绕木杉 2005-05-10
  • 打赏
  • 举报
回复
("jdbc:microsoft:sqlserver://localhost:1443;fight;","sa","");

改为

("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=fight;","sa","");
曼绕木杉 2005-05-10
  • 打赏
  • 举报
回复
("jdbc:microsoft:sqlserver://localhost:1443;fight;","sa","");

改为

("jdbc:microsoft:sqlserver://localhost:1433;fight;","sa","");
keranbing 2005-05-10
  • 打赏
  • 举报
回复
我数据库用户名是sa, 密码为空啊!
YLENTER 2005-05-10
  • 打赏
  • 举报
回复
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1443;DatabaseName=fight;","sa","sa");这里有问题.

81,116

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧