由于底层异常,无法加载连接类:'java.lang.NumberFormatException:对于输入字符串:"OPENSHIFT_MYSQL_DB_PORT" [英] Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "OPENSHIFT_MYSQL_DB_PORT"'

查看:374
本文介绍了由于底层异常,无法加载连接类:'java.lang.NumberFormatException:对于输入字符串:"OPENSHIFT_MYSQL_DB_PORT"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在openshift上部署Jersey项目.我实现了这个Apple类来测试另一个类中的错误,因为我猜问题出在建立数据库连接上.在Tails日志中,我发现此错误:

I am trying to deploy my Jersey project on openshift. I have implemented this apple class to test the error in the another class since I guess the problem is with the establishing the database connection. in the Tails log I found this error:

连接到数据库…… com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:由于底层异常而无法加载连接类:'java.lang.NumberFormatException:对于输入字符串:"OPENSHIFT_MYSQL_DB_PORT"'. 在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)

Connecting to database… com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "OPENSHIFT_MYSQL_DB_PORT"'. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

package org.busTracker.serverSide;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("myresource")
public class Apple {

   //I modified my credients.
    String host = "jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:OPENSHIFT_MYSQL_DB_PORT/serverside";
    String user = "adminBjv5a4k";
    String password = "7tvPb1Bx3v8j";

    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {

        Connection conn = null;  
        try {    
          Class.forName("com.mysql.jdbc.Driver");    
          System.out.println("Connecting to database…");    
          conn = DriverManager.getConnection(host,user,password);    
        } catch (Exception e) {    
          e.printStackTrace();    
        } finally {    
          if (conn != null) {    
            try {    
              conn.close();    
            } catch (SQLException e) {    
              // ignore    
            }    
          }    
        }   

        return "Hello, from apple class   14.05.15 11:35!";
    }

}

编辑:我在DriverManager.getConnection()之后的try块中添加了以下内容:

I added the following to the try block after DriverManager.getConnection():

  Map<String, String> env = System.getenv();
  for (String envName : env.keySet()) {
      System.out.format("%s=%s%n",
                        envName,
                        env.get(envName));
  }

我尝试了以下操作,但仍然遇到相同的错误:

I have tried the following but I am still getting the same error:

  • This solution: https://forums.openshift.com/mysql-51-jboss-app-numberformatexception-mysql-url And add the following jdbc:mysql://${env.OPENSHIFT_MYSQL_DB_HOST}:${env.OPENSHIFT_MYSQL_DB_PORT}/serv‌​erside but nothing changed.
  • "jdbc:mysql://127.10.310.130:3306 /serverside"; This values are from the phpmyadmin of the app.

推荐答案

问题是由于此行

String host = "jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:OPENSHIFT_MYSQL_DB_PORT/serverside";

要获取环境变量,您需要使用方法System.getEnv().get("[the variable name]").因此,在您的情况下,主机变量应如下所示:

to get the environment variable, you need to use the method System.getEnv().get("[the variable name]"). So, in your case, the host variable should looks like this

String host = "jdbc:mysql://" 
              + System.getenv().get("OPENSHIFT_MYSQL_DB_HOST") 
              + ":" 
              + System.getenv().get("OPENSHIFT_MYSQL_DB_PORT") 
              + "/serverside";

顺便说一句,您的编辑不起作用,因为应用程序在执行代码之前已经抛出了异常.因此,要使其正常工作,您需要将其放在 before DriverManager.getConnection()函数中.

by the way, your edit does not work because the application already throws an exception before it execute the code. so, to make it work, you need to put it before the DriverManager.getConnection() function.

这篇关于由于底层异常,无法加载连接类:'java.lang.NumberFormatException:对于输入字符串:"OPENSHIFT_MYSQL_DB_PORT"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆