我是数据库新手,找不到解决此错误的方法。我正在尝试使用Eclipse在我的jsp文件和sql服务器之间建立连接。我将ms sql驱动程序复制到WEB-INF / lib并配置了“生成路径”,因此它指向该驱动程序,但它给出了错误。我的错误是什么,如何纠正?
在此错误消息:
Message java.lang.NullPointerException
Description The server encountered an unexpected condition that prevented it
from fulfilling the request.
Exception
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrap per.java:572)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:48
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.NullPointerException
org.apache.jsp.Datab_jsp$Actor.getActors(Datab_jsp.java:54)
org.apache.jsp.Datab_jsp._jspService(Datab_jsp.java:193)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:44
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
我在jsp文件中的代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<%@ page import=" javax.sql.DataSource" %>
<% Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
public class Actor {
String URL = "jdbc:sqlserver://localhost:1433/demo";
String username = "root";
String password = "7170256aziz";
Connection connection = null;
PreparedStatement selectActors = null;
ResultSet resultSet = null;
public Actor(){
try {
connection = DriverManager.getConnection(URL, username, password);
selectActors = connection.prepareStatement("SELECT name, id, address FROM demo");
} catch(SQLException e){
e.printStackTrace();
}
}
public ResultSet getActors(){
try{
resultSet = selectActors.executeQuery();
} catch (SQLException e){
e.printStackTrace();
}
return resultSet;
}
}
%>
<%
Actor actor = new Actor();
ResultSet actors = actor.getActors();
%>
答案 0 :(得分:0)
尝试在Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
中添加Actor()
public Actor(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager.getConnection(URL, username, password);
selectActors = connection.prepareStatement("SELECT name, id, address FROM demo");
} catch(SQLException e){
e.printStackTrace();
}
}
答案 1 :(得分:0)
我解决了这个问题。我所做的是:
将驱动程序6.4更改为4.0
将URL语法更改为“ jdbc:sqlserver:// localhost; databaseName = demo; integratedSecurity = true;”
将“ SELECT名称,ID,地址FROM演示”更改为“ SELECT名称,ID,地址FROM人”,其中人是我的表名
将sqljdbc_auth.dll复制到Windows / system32
此后,错误消息消失了。