数据库配置时出现了问题
在自学java,用的是myeclipse6.5 服务器是jboss
按照教材写的的代码。
DB.properties 文件时是放在src目录下.从myeclipse里打开这个文件说com.microsoft.....这些拼写错误。下面画了红线。但我没在意。我觉得这个没什么问题。
className = com.microsoft.sqlserver.jdbc.SQLServerDriverurl = jdbc:sqlserver://127.0.0.1:1433;DatabaseName=STUDENTSname= sapassword=3401
package beans;import java.io.InputStream;import java.util.Properties;public class DBProperty { private String className; private String url; private String name; private String password; public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getProperty(String pro){ InputStream in; Properties p = new Properties(); try{ in = Class.forName("beans.DBProperty").getResourceAsStream("/DB.properties"); p.load(in); }catch(Exception ex){ ex.printStackTrace(); } return p.getProperty(pro); }}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ page import ="java.sql.*" %><%@ page import="javax.sql.*" %><%@ page import="javax.naming.*" %><%@ page import="beans.DBConst"%><%@ page import="beans.DBProperty"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body>“ This is my JSP page. <br> <% DBProperty pro = new DBProperty(); Connection conn= null; Class.forName(pro.getClassName()).newInstance(); String url=pro.getUrl(); String user=pro.getName(); String password=pro.getPassword(); conn = DriverManager.getConnection(url,user,password); out.print("get a connection of databases!\n"); out.print(conn.toString()); %> </body></html>
exception
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:535)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:429)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
root cause
java.lang.NullPointerException
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Unknown Source)
org.apache.jsp.index_jsp._jspService(index_jsp.java:129)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:387)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
note The full stack trace of the root cause is available in the JBossWeb/2.0.0.GA logs.
[解决办法]
把你的index.jsp修改成一下内容看看:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ page import ="java.sql.*" %><%@ page import="javax.sql.*" %><%@ page import="javax.naming.*" %><%@ page import="beans.DBConst"%><%@ page import="beans.DBProperty"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body>“ This is my JSP page. <br> <% DBProperty pro = new DBProperty(); Connection conn= null; String clsName = pro.getProperty("className") ; String url = pro.getProperty("url") ; String user = pro.getProperty("name") ; String password = pro.getProperty("password") ; System.out.println(clsName + " -- " + url + " -- " + user + " --- " + password) ; Class.forName(clsName) ; conn = DriverManager.getConnection(url,user,password); out.print("get a connection of databases!\n"); out.print(conn.toString()); %> </body></html>
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html