Pergunta

So I am working on a project wherein I need to connect to a MySQL using a web service. I will eventually write a client on Android but I haven't gotten that far yet.

What I have done so far:

  • Created a MySQL database
  • Created a "separate" (for testing connection) working class (as a simple java program, not web service) to try out some simple SQL queries with my connection. This all worked like expected.
  • Next I followed THIS tutorial on setting up a web service for my project.

This is where my problems start. So I already created the service, what I am doing now is RIGHT-CLICKING on my project and selecting RUN AS > Run On Server... I am using Eclipse Kepler by the way.

This is what I see in the console:

[INFO] The DBHelper service, which is not valid, caused java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at org.apache.axis2.description.java2wsdl.DefaultSchemaGenerator.<init>(DefaultSchemaGenerator.java:140)
at org.apache.axis2.deployment.util.Utils.fillAxisService(Utils.java:453)
at org.apache.axis2.deployment.ServiceBuilder.populateService(ServiceBuilder.java:388)
at org.apache.axis2.deployment.repository.util.ArchiveReader.buildServiceGroup(ArchiveReader.java:101)
at org.apache.axis2.deployment.repository.util.ArchiveReader.processServiceGroup(ArchiveReader.java:178)
at org.apache.axis2.deployment.ServiceDeployer.deploy(ServiceDeployer.java:82)
at org.apache.axis2.deployment.repository.util.DeploymentFileData.deploy(DeploymentFileData.java:136)
at org.apache.axis2.deployment.DeploymentEngine.doDeploy(DeploymentEngine.java:813)
at org.apache.axis2.deployment.repository.util.WSInfoList.update(WSInfoList.java:144)
at org.apache.axis2.deployment.RepositoryListener.update(RepositoryListener.java:377)
at org.apache.axis2.deployment.RepositoryListener.checkServices(RepositoryListener.java:254)
at org.apache.axis2.deployment.DeploymentEngine.loadServices(DeploymentEngine.java:142)
at org.apache.axis2.deployment.WarBasedAxisConfigurator.loadServices(WarBasedAxisConfigurator.java:283)
at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContext(ConfigurationContextFactory.java:95)
at org.apache.axis2.transport.http.AxisServlet.initConfigContext(AxisServlet.java:584)
at org.apache.axis2.transport.http.AxisServlet.init(AxisServlet.java:454)
at org.apache.axis2.webapp.AxisAdminServlet.init(AxisAdminServlet.java:60)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at com.dylanlegendre.notepal1.PropLoader.<init>(PropLoader.java:35)
at com.dylanlegendre.notepal1.DBHelper.<clinit>(DBHelper.java:17)
... 31 more

I'm not exactly sure what I may or may not be doing correctly. The server itself starts and I can go to the SERVICES link and it just shows as a faulty service with that message.

I have researched and tried a few different things. One of which was to make sure my MySQL Connector JAR was in the WEB-INF folder and changed my Build Path accordingly, but to no avail--same errors.

My project currently has TWO classes in it. The actual Service Class is the DBHelper class which I will use to interact with the database. The PropLoader class simply loads in my properties files that has the database connection information in it. The next logical step I will take is to attempt to get it all working WITHOUT the properties file and just hard code the connection information into the actual service class. But from what I THINK is the issue is that my JDBC isn't getting read? Am I wrong in this?

EDIT So I tried my above solution and it worked. Now I just need to know WHY and/or how to remedy this situation. I removed my PropLoader instance and just put actual strings into the variables and it worked with no errors. So maybe you can't have an instance of another class in a web service is what I'm thinking? If so, how can I load from my properties file without creating an instance of it?

Anywho, here are the source codes for my two classes:

public class DBHelper {

    private static PropLoader loader = new PropLoader("/WEB-INF/lib/info.cfg");

    private final static String DRIVER_PATH = "com.mysql.jdbc.Driver";
    private final static String DB_URL = loader.getDbConnection();
    private final static String USER = loader.getDbUser();
    private final static String PASSWORD = loader.getDbPass();

    private static Connection conn; 
    private static Statement stmt;
    private static String query;
    private static ResultSet rs;
    private static String msg; // the result of the query as a string

    public static String getMsg() {
        return msg;
    }

    public static void setMsg(String MESS) {
        msg = MESS;
    }

    public static void openConnection() {
        try {
            Class.forName( DRIVER_PATH ).newInstance();
            conn = DriverManager.getConnection( DB_URL, USER, PASSWORD );
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void closeConnection() {
        try {
            conn.close();
        } catch (SQLException e) {

            e.printStackTrace();
        }
    }

    public static String executeSingleQuery(String QUERY1, String COLUMN_NAME) {
        try {
            stmt = conn.createStatement();
            query = QUERY1;
            rs = stmt.executeQuery(query);

            while (rs.next()) {
                msg = rs.getString(COLUMN_NAME);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return msg;

    }

    public static void executeQuery(String QUERY2, String COLUMN_NAME1) {
        try {
            stmt = conn.createStatement();
            query = QUERY2;
            rs = stmt.executeQuery(query);

            while (rs.next()) {
                msg = rs.getString(COLUMN_NAME1);
                System.out.println(msg);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }



}

public class PropLoader {

    private Properties prop;
    private String path;

    private String dbConnection;
    private String dbUser;
    private String dbPass;

    public PropLoader(String PATH) {
        this.path = PATH;
        this.prop = new Properties();

        try {
            InputStream fs = getClass().getResourceAsStream(path);
            prop.load(fs);

            // retrieve properties
            this.dbConnection = prop.getProperty("dbConnection");
            this.dbUser = prop.getProperty("dbUser");
            this.dbPass = prop.getProperty("dbPass");

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

    public String getDbConnection() {
        return dbConnection;
    }

    public void setDbConnection(String dbConnection) {
        this.dbConnection = dbConnection;
    }

    public String getDbUser() {
        return dbUser;
    }

    public void setDbUser(String dbUser) {
        this.dbUser = dbUser;
    }

    public String getDbPass() {
        return dbPass;
    }

    public void setDbPass(String dbPass) {
        this.dbPass = dbPass;
    }

}
Foi útil?

Solução

ExceptionInInitializerError occurs if an error or exception occurs when the class is getting loaded in its ClassLoader.

This might occur if anything wrong happens while initializing any of the static variables that are present within a class or if there are any static blocks something like:

In your class you could check if any exception occurs at the line:

private static PropLoader loader = new PropLoader("/WEB-INF/lib/info.cfg");

In addition to catching the IOException, you could also catch the Exception to check if your PropLoader constructor is completed successfully without any errors. If there is an error or exception there, then it could lead to whether the file "/WEB-INF/lib/info.cfg" is present inside the war in the said location or not.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top