java 名称 [resource] 在此上下文中未绑定。找不到 [jdbc]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32808231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Name [resource] is not bound in this Context. Unable to find [jdbc]
提问by jKraut
I'm new to connecting to MySQL using JDBC with tomcat. I've been following an excellent tutorial herebut am struggling. I have ensured my EC2 security group is configured to MYSQL port 3306 0.0.0.0/0 and HTTP port 80 0.0.0.0/0. I'm currently getting the error: Name [jdbc/bbytesting] is not bound in this Context. Unable to find [jdbc].
我是使用 JDBC 和 tomcat 连接到 MySQL 的新手。我一直在这里学习一个很好的教程,但我很挣扎。我确保我的 EC2 安全组配置为 MYSQL 端口 3306 0.0.0.0/0 和 HTTP 端口 80 0.0.0.0/0。我目前收到错误:名称 [jdbc/bbytesting] 在此上下文中未绑定。无法找到 [jdbc]。
Here is stacktrace:
这是堆栈跟踪:
javax.naming.NameNotFoundException: Name [jdbc/bbytesting] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:818)
at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:157)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at com.youtube.dao.MySQL308tube.MySQL308tubeConn(MySQL308tube.java:31)
at com.youtube.rest.status.V1_status.returnDatabaseStatus(V1_status.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Here are the steps I've performed up to this point:
以下是我到目前为止执行的步骤:
Downloaded MySQL JDBC Driver and put in lib folder
下载 MySQL JDBC Driver 并放入 lib 文件夹
JDBC Resource Configuration
JDBC 资源配置
<Context>
<Resource name="jdbc/bbytesting" auth="Container" type="javax.sql.DataSource"
maxActive="50" maxIdle="30" maxWait="10000"
username="username" password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://awsurl:3306/bbytesting">
</Context>
Return database status code:
返回数据库状态码:
public String returnDatabaseStatus() throws Exception {
PreparedStatement query = null;
String myString = null;
String returnString = null;
Connection conn = null;
try {
conn = MySQL308tube.MySQL308tubeConn().getConnection();
query = conn.prepareStatement("select to_char(sysdate, 'YYYY-MM-DD HH24:MI:SS') DATETIME " + "from sys.dual");
ResultSet rs = query.executeQuery();
while (rs.next()) {
myString = rs.getString("DATETIME");
}
query.close();
returnString = "<p>Database Status</p> "
+ "<Database Date/Time return: " + myString + "</p>";
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (conn != null) conn.close();
}
return returnString;
}
Here is connection class code:
这是连接类代码:
public class MySQL308tube {
private static DataSource MySQL308tube = null;
private static Context context = null;
public static DataSource MySQL308tubeConn() throws Exception {
if(MySQL308tube != null) {
return MySQL308tube;
}
try {
if (context == null) {
context = new InitialContext();
}
MySQL308tube = (DataSource) context.lookup("jdbc/bbytesting");
}
catch (Exception e) {
e.printStackTrace();
}
return MySQL308tube;
}
}
回答by duffymo
The exception explains it perfectly: That's why you should never, ever have empty catch blocks.
异常完美地解释了它:这就是为什么你永远不应该有空的 catch 块。
Your JNDI data source lookup is failing. You need to prepend it with "java:/comp/env", like these Tomcat docs tell you to:
您的 JNDI 数据源查找失败。您需要在它前面加上“java:/comp/env”,就像这些 Tomcat 文档告诉您的那样:
https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html
https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html
回答by James Jithin
You are using awsurl
in the connection URL which needs extra permissions for the user. If your application and database runs on the same server, refer by localhost
or 127.0.0.1
.
您awsurl
在需要用户额外权限的连接 URL 中使用。如果您的应用程序和数据库在同一台服务器上运行,请通过localhost
或引用127.0.0.1
。
If you would need to make your application access this server by awsurl
, go to mysql prompt as mysql root user and execute GRANT
:
如果您需要让您的应用程序通过 访问此服务器awsurl
,请以 mysql root 用户身份转到 mysql 提示符并执行GRANT
:
For global access.
用于全球访问。
GRANT ALL ON bbytesting.* TO '<user>'@'%' IDENTIFIED BY '<password>'
For access from particular IP address (possibly IP of server where you run your application)
用于从特定 IP 地址访问(可能是您运行应用程序的服务器的 IP)
GRANT ALL ON bbytesting.* TO '<user>'@'<ip_address>' IDENTIFIED BY '<password>'
Also, I recommend you to test accessing database from the server you run your application using command:
此外,我建议您使用以下命令测试从运行应用程序的服务器访问数据库:
mysql -u user_name -ppassword -h db_server_ip
No space between switch -p and password
开关 -p 和密码之间没有空格
You may need to install mysql-client package to run mysql command.
您可能需要安装 mysql-client 软件包才能运行 mysql 命令。