日期:2014-05-18  浏览次数:20677 次

请教一个,数据库连接的问题。(在线等待)
我现在有以下的代码:

Connection   con;
Statement   stmt;
ResultSet   res;   //   add

Class.forName( "com.mysql.jdbc.Driver ").newInstance();
con   =   DriverManager.getConnection( "jdbc:mysql://localhost:3306/test ",   "root ",   " ");
stmt   =   con.createStatement();
res   =   stmt.executeQuery( "select   *   from   username   ");
...........

这段java代码,在一个单独的Java程序(也就是有mail函数的那种)中正常执行。但是如果把这个代码,放到web开发的Java程序中就会出错。
「con   =   DriverManager.getConnection( "jdbc:mysql://localhost:3306/test ",   "root ",   " ");」出错,出错的信息如下:


com.mysql.jdbc.CommunicationsException:   Communications   link   failure   due   to   underlying   exception:  

**   BEGIN   NESTED   EXCEPTION   **  

java.net.SocketException
MESSAGE:   java.security.AccessControlException:   access   denied   (java.net.SocketPermission   127.0.0.1:3306   connect,resolve)

STACKTRACE:

java.net.SocketException:   java.security.AccessControlException:   access   denied   (java.net.SocketPermission   127.0.0.1:3306   connect,resolve)
at   com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:151)
at   com.mysql.jdbc.MysqlIO. <init> (MysqlIO.java:280)
at   com.mysql.jdbc.Connection.createNewIO(Connection.java:1765)
at   com.mysql.jdbc.Connection. <init> (Connection.java:430)
at   com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:268)
at   java.sql.DriverManager.getConnection(Unknown   Source)
at   java.sql.DriverManager.getConnection(Unknown   Source)
at   Login.LoginJavaBean.strCheck(LoginJavaBean.java:16)
at   Login.LoginServlet.doGet(LoginServlet.java:16)
at   javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
at   javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:199)
at   org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:50)
at   org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:128)
at   java.security.AccessController.doPrivileged(Native   Method)
at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:124)
at   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:210)
at   org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at   org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:955)
at   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:139)
at   org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at   org.apache.catalina.core.StandardPipeline.i