本人使用Tomcat6.0+MSSQL Server2000+MyEclipse7.0配置數據庫連接池,代碼應該沒寫錯!數據庫也能連接成功,不知道為什么還報錯:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]對象名 'MyDatabase' 無效
contexet.xml文件代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/MyDatabase" auth="Container" type="javax.sql.DataSource"
maxActive="20" maxIdle="10" maxWait="1000" logAbandoned="true"
username="sa" password="0702020128" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://localhost:1433;DatebaseName=MyDatabase" />
</Context>
web.xml文件代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<res-ref-name>jdbc/MyDatabase</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
測試文件index.jsp代碼如下:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import = "java.sql.*" %>
<%@ page import = "javax.sql.*"%>
<%@ page import = "javax.naming.*"%>
<%
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>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Context context = new InitialContext();
DataSource dataSource =(DataSource)context.lookup("java:/comp/env/jdbc/MyDatabase");
conn = dataSource.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from MyDatabase");
System.out.println("rs:" + rs);
while(rs.next()){
out.println("學號" + rs.getString("ID"));
out.println("性別" + rs.getString("Sex"));
out.println("姓名" + rs.getString("Name"));
out.println("學校" + rs.getString("School"));
out.println("專業" + rs.getString("PROINF"));
}
}catch(SQLException e){
e.printStackTrace();
}
%>
<%
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
%>
</body>
</html>
8 个解决方案
" DataSource dataSource =(DataSource)context.lookup("java:/comp/env/jdbc/MyDatabase"); "
樓主你的這句話有問題
lookup("java:comp/env/jdbc/MyDatabase");
"java:/comp" 這個地方沒有"/"
改一下試試吧,如果對請加分哦,小弟是菜鳥需要的,謝謝
非常感謝你的回答!問題還是解決不了...
2009-5-7 22:55:44 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.6.0\bin;D:\ApacheTomcat 6.0\bin
2009-5-7 22:55:45 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2009-5-7 22:55:45 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 2788 ms
2009-5-7 22:55:45 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2009-5-7 22:55:45 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.18
2009-5-7 22:55:48 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
2009-5-7 22:55:48 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
2009-5-7 22:55:49 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2009-5-7 22:55:50 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2009-5-7 22:55:50 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/78 config=null
2009-5-7 22:55:50 org.apache.catalina.startup.Catalina start
信息: Server startup in 4503 ms
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]對象名 'MyDatabase' 無效。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSExecuteRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQuery(Unknown Source)
at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeQuery(DelegatingStatement.java:208)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:105)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
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: javax.servlet.ServletException: javax.naming.NameNotFoundException: Name MyDatabase is not bound in this Context
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:398)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.servlet.ServletException: javax.naming.NameNotFoundException: Name MyDatabase is not bound in this Context
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:852)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.index_jsp._jspService(index_jsp.java:158)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.naming.NameNotFoundException: Name MyDatabase is not bound in this Context
org.apache.naming.NamingContext.lookup(NamingContext.java:770)
org.apache.naming.NamingContext.lookup(NamingContext.java:140)
org.apache.naming.NamingContext.lookup(NamingContext.java:781)
org.apache.naming.NamingContext.lookup(NamingContext.java:153)
org.apache.jsp.index_jsp._jspService(index_jsp.java:99)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.
哈哈~問題終於解決了!非常感謝大家,小弟在這里"謝謝"!
我靠 問題解決了 說下怎么解決的撒 google累死找到個跟我一樣的exception你卻不說