无法通过servlet连接mysql;访问被拒绝

时间:2017-08-28 09:29:55

标签: java mysql eclipse tomcat servlets

我是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

1 个答案:

答案 0 :(得分:0)

检查java安全管理器选项。

您可以尝试在没有安全管理器的情况下运行tomcat以找出问题。见how to disable Security Manager