连接java.sql.SQLNonTransientConnectionException:无法创建与数据库服务器的连接

时间:2018-09-10 06:43:16

标签: java mysql jdbc database-connection

我正在尝试在java中的MySQL数据库usign jdbc中添加一些记录。该程序对100条记录绝对有效。但是在100条记录之后,我得到以下错误。

Exception occured while obtaining 

connectionjava.sql.SQLNonTransientConnectionException: Could not create connection to database server.
java.lang.NullPointerException
    at com.rest.EmployeeRest.insertDesigMySQLData(EmployeeRest.java:446)
    at com.rest.EmployeeRest.syncDesignation(EmployeeRest.java:188)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
    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:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1082)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:623)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

在google上出现此错误后,有人告诉要在localhost 3306(MySQL端口)上使用telnet命令。并显示“连接过多”错误。 谁能帮我解决这个问题。

获取数据并添加到MySQL的API代码:

ArrayList<Employee> arrDesig = getAllDesig();
setTotRecords(arrDesig.size());
System.out.println("Oracle TotRecords = "+getTotRecords());
if(arrDesig.size() > 0){
    for(int iCnt=0; iCnt<arrDesig.size(); iCnt++){
        if(insertDesigMySQLData(arrDesig.get(iCnt)) == 1){
            mysqlDat++;
        }
        System.out.println("MySQL inserted data = "+mysqlDat);
    }
    if(getTotRecords() == mysqlDat){
        retval = 1;
    }else{
        retval = 0;
    }
}else{
    retval = 0;
}

要添加的MySQL代码:

int retval = 0;
    OracleDataSource objMySQLDataSource = new OracleDataSource("mysql");
    try{
        Connection connection = objMySQLDataSource.getDbConnection();
        CallableStatement pstmt=connection.prepareCall("{call addDesig(?,?,?)}");
        pstmt.setString(1,objEmployee.getDesig_id());
        System.out.println("getDesig_id() = "+objEmployee.getDesig_id());
        pstmt.setString(2,objEmployee.getDesig_nm());
        System.out.println("getDesig_nm() = "+objEmployee.getDesig_nm());
        pstmt.registerOutParameter(3, Types.NUMERIC);
        pstmt.executeUpdate();
        retval = pstmt.getInt(3);
        pstmt.close();
    }catch (Exception e) {
        retval = 0;
        e.printStackTrace();
    }finally{
        objMySQLDataSource.connectionClose();
    }
    return retval;

Oracle连接

OracleDataSource(){
    setUrl("jdbc:oracle:thin:@dburl");
    setDriver("oracle.jdbc.OracleDriver");
    setUserName("admin");
    setPassword("**********");
}

MySQL连接

OracleDataSource(String mysql){
    setUrl("jdbc:mysql://localhost:3306/academicdb?verifyServerCertificate=false&useSSL=true&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC");
    setDriver("com.mysql.cj.jdbc.Driver");//com.mysql.jdbc.Driver
    setUserName("root");
    setPassword("");
}

public Connection getDbConnection() {
        Connection con = null;
        try{
            Class.forName(getDriver());
            con = DriverManager.getConnection(getUrl(), getUserName(), getPassword());
        } catch (Exception se) {
            System.out.println("Exception occured while obtaining connection" + se.toString());
        }
        return con;
    }

0 个答案:

没有答案