我是eclipse和servlet的新手。我试图连接数据库(MySQL)。当我在.java应用程序中运行代码时,它起作用,但是当我将代码放入一个servlet时,它不起作用。我已经查看了类似于我的问题,但答案无法解决我的问题。请给我任何想法如何使我的代码工作。谢谢。我的代码如下:无法通过servlet连接mysql;访问被拒绝
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
\t \t // TODO Auto-generated method stub
\t \t response.getWriter().append("Served at: ").append(request.getContextPath());
\t }
\t /**
\t * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
\t */
\t protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
\t \t Connection conn = null;
\t \t \t try{
\t \t \t Class.forName("com.mysql.jdbc.Driver");
\t \t \t conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1","root","jeff800205");
\t \t \t }
\t \t \t catch(SQLException e){
\t \t System.out.println("@@@Error occured at barDAO->select_all()");
\t \t e.printStackTrace();
\t \t } catch (ClassNotFoundException e) {
\t \t \t e.printStackTrace();
\t \t \t System.out.println("%%%Error occured at barDAO->select_all()");
\t \t
\t \t \t \t //} catch (ClassNotFoundException e) {
\t \t \t \t // TODO Auto-generated catch block
\t \t \t \t e.printStackTrace();
\t \t \t }
\t }
问题如下:
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")
\t at java.security.AccessControlContext.checkPermission(Unknown Source)
\t at java.security.AccessController.checkPermission(Unknown Source)
\t at java.lang.SecurityManager.checkPermission(Unknown Source)
\t at java.lang.Thread.setContextClassLoader(Unknown Source)
\t at com.mysql.jdbc.AbandonedConnectionCleanupThread$1.newThread(AbandonedConnectionCleanupThread.java:50)
\t at java.util.concurrent.ThreadPoolExecutor$Worker.<init>(Unknown Source)
\t at java.util.concurrent.ThreadPoolExecutor.addWorker(Unknown Source)
\t at java.util.concurrent.ThreadPoolExecutor.execute(Unknown Source)
\t at java.util.concurrent.Executors$DelegatedExecutorService.execute(Unknown Source)
\t at com.mysql.jdbc.AbandonedConnectionCleanupThread.<clinit>(AbandonedConnectionCleanupThread.java:54)
\t 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
这里是我的端口imformation: enter image description here
将代码和堆栈跟踪添加为文本而不是图像。 – Jens
请看看你的tomcat日志文件并发布整个异常 – Jens
谢谢。这是文本中的代码。 – jeff