无法使用Tomcat在servlet上创建连接whit Drivemanager

时间:2018-03-30 13:39:32

标签: java postgresql tomcat servlets jdbc

我正在将localhost迁移到localhost和远程服务器之间。在远程服务器上,任何东西看起来都很好,但是当我尝试通过jdbc连接器连接到远程数据库时,程序停止并且没有错误返回。

远程服务器的IP是正确的,它是另一台服务器(不是我加载webapp的服务器)。

public void accessRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException,  ClassNotFoundException {
    PreparedStatement prepStat=null;
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();


    Connection con=null;
    Class.forName("org.postgresql.Driver");
    out.println("Before create connection");
    try {
        con = (Connection) DriverManager.getConnection("jdbc:postgresql://192.168.0.130:5432/dbhydro2","user", "pwd");
    } catch (SQLException e) {

        e.printStackTrace();
        out.println("connection doesnt create");
    }
out.println("connection create");

我想:

  • jdk的版本错误(我使用的是最后一个版本)
  • 我必须在tomcat或jdk中配置一些东西

不可能: - jar文件的错误位置

你有什么想法吗?这看起来很简单但不是。

日志:

30-Mar-2018 07:14:28.801 SEVERE [http-nio-8080-exec-7] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Test] in context with path [/HydroGui] threw exception
 java.lang.ClassCastException: org.postgresql.jdbc.PgConnection cannot be cast to com.mysql.jdbc.Connection
    at Test.accessRequest(Test.java:52)
    at Test.doGet(Test.java:87)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:409)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

0 个答案:

没有答案