我是使用带有tomcat的JDBC连接到MySQL的新手。我一直在关注一本优秀的教程here,但我正在努力。我确保我的EC2安全组配置为MYSQL端口3306 0.0.0.0/0和HTTP端口80 0.0.0.0/0。我目前收到错误:名称[jdbc / bbytesting]未绑定在此Context中。无法找到[jdbc]。
这是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$1.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)
以下是我迄今为止所执行的步骤:
下载MySQL JDBC驱动程序并放入lib文件夹
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>
返回数据库状态代码:
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;
}
这是连接类代码:
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;
}
}
答案 0 :(得分:1)
这个例外完美地解释了这一点:这就是为什么你永远不应该有空的捕获块。
您的JNDI数据源查找失败。您需要在前面添加“java:/ comp / env”,就像这些Tomcat文档告诉您的那样:
https://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html
答案 1 :(得分:-1)
您在连接网址中使用awsurl
,该网址需要用户的额外权限。如果您的应用程序和数据库在同一台服务器上运行,请参阅localhost
或127.0.0.1
。
如果您需要让应用程序通过awsurl
访问此服务器,请以mysql root用户身份转到mysql提示符并执行GRANT
:
全球访问。
GRANT ALL ON bbytesting.* TO '<user>'@'%' IDENTIFIED BY '<password>'
从特定IP地址(可能是您运行应用程序的服务器的IP)访问
GRANT ALL ON bbytesting.* TO '<user>'@'<ip_address>' IDENTIFIED BY '<password>'
另外,我建议您使用命令测试从运行应用程序的服务器访问数据库:
mysql -u user_name -ppassword -h db_server_ip
切换-p和密码之间没有空格
您可能需要安装mysql-client软件包才能运行mysql命令。