我是 eclipse 和 servlet 的新手。我正在尝试连接数据库(mysql)。当我在 .java 应用程序中运行代码时它起作用,但当我将代码放入 servlet 时它不起作用。我已经查看了与我类似的问题,但答案无法解决我的问题。请告诉我如何使我的代码工作。谢谢。我的代码如下:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","jeff800205");
}
catch(SQLException e){
System.out.println("@@@Error occured at barDAO->select_all()");
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("%%%Error occured at barDAO->select_all()");
//} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
问题如下:
SEVERE: Servlet.service() for servlet [helloBar2] in context with path [/echarts2] threw exception [null] with root cause
java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "setContextClassLoader")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.Thread.setContextClassLoader(Unknown Source)
at com.mysql.jdbc.AbandonedConnectionCleanupThread$1.newThread(AbandonedConnectionCleanupThread.java:50)
at java.util.concurrent.ThreadPoolExecutor$Worker.<init>(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.addWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.execute(Unknown Source)
at java.util.concurrent.Executors$DelegatedExecutorService.execute(Unknown Source)
at com.mysql.jdbc.AbandonedConnectionCleanupThread.<clinit>(AbandonedConnectionCleanupThread.java:54)
at java.lang.Class.forName0(Native Method)
我用的是tomcat 8.5,mysql5.7,mysql-connector-java 5.1.43-bin.jar和mysql-connector-java 5.0.8.jar都试过了。我把mysql-connector.jar放到了WEB_INF下的lib文件夹里,我也把mysql-connector.jar放到了Tomcat的lib里。但问题仍然存在。
这是我在 tomcat 中的日志文件:
127.0.0.1 - - [28/Aug/2017:09:12:01 +0800] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [28/Aug/2017:09:12:10 +0800] "GET /echarts/test.jsp HTTP/1.1" 200 604
0:0:0:0:0:0:0:1 - - [28/Aug/2017:09:12:16 +0800] "POST /echarts/bar.do HTTP/1.1" 500 2834
127.0.0.1 - - [28/Aug/2017:12:35:06 +0800] "GET / HTTP/1.1" 200 11452
0:0:0:0:0:0:0:1 - - [28/Aug/2017:12:35:16 +0800] "GET /echarts/test.jsp HTTP/1.1" 200 604
0:0:0:0:0:0:0:1 - - [28/Aug/2017:12:35:25 +0800] "POST /echarts/bar.do HTTP/1.1" 500 2834
这是我的端口信息: enter image description here
最佳答案
检查 java 安全管理器选项。
您可以尝试在没有Security Manager 的情况下运行tomcat 来找出问题所在。看这里how to disable Security Manager
关于java - 无法通过 servlet 连接 mysql;访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45915850/