-1

" Unresolved compilation problems: The local variable input may not have been initialized The local variable success may not have been initialized The local variable input may not have been initialized" Ive been trying to populate my Database with 3 inputs, I'm very new to struts2 framework at MySQl so any type of insight or input would be very helpful. Have i included enough code information or?This is my ActionClass.

<pre> package com.tutorialspoint.struts2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport{

    private String osName;
    private String version;
    private String notes;



    public String execute() throws Exception {

        String input;
        String success;
        String ret = input;
        Connection conn = null;

        try{
            String URL = "jdbc:mysql://localhost/HelloWorld";
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(URL, "root", "");
            String sql = "SELECT osName FROM entry WHERE";
            sql+=" osName = ? AND version = ?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, osName);
            ps.setString(2, version);
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                notes = rs.getString(1);
                ret = success;
            }
        }catch (Exception e) {
            ret = input;
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                }
            }
        }

        return ret;
    }


        public String getOsName() {
        return osName;
    }
    public void setOsName(String osName) {
        this.osName = osName;
    }
    public String getVersion() {
        return version;
    }
    public void setVersion(String version) {
        this.version = version;
    }
    public String getNotes() {
        return notes;
    }
    public void setNotes(String notes) {
        this.notes = notes;
    }

    public void validate()
    {
        if (osName == null || osName.trim().equals(""))
        { 
            addFieldError("osName","The OS name is required");
        }
        if (version == null || version.trim().equals(""))
        {
            addFieldError("version","The OS version is required");
        }
    }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
Kyas
  • 39
  • 2
  • 7
  • As a note, just merge your `sql` string into a single constant. There's no reason to split it up like that. And you probably shouldn't be opening and closing database connections for single requests; they're extremely expensive. – chrylis -cautiouslyoptimistic- Jan 18 '15 at 01:38
  • You are doing absolutely different thing with database and what is your actual problem? – Roman C Jan 18 '15 at 10:18

2 Answers2

1

Your input and success are local variables, which don't have default values. When you hit the line ret = input, you haven't put anything in input, and so that assignment makes no sense. Same thing for ret = success; you never assign any value to success anywhere in the method.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
1

SUCCESS, ERROR, INPUT, NONE and LOGIN are predefined framework constants defined in the Action interface, implemented by the ActionSupport class, that your action extends.

So, leaving apart the fact that you are not initializing your variables (what you should have done would be :

private String success = "success";
private String input   = "input";

), they are absolutely not needed because you can return the constant value:

ret = SUCCESS;
...
ret = INPUT;

or, when not extending the ActionSupport, the literal value:

ret = "success";
...
ret = "input";

The constant are preferred to avoid typos.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Can someone take a look at my github file and tell me whats going on? https://github.com/kyas123/JavaOsLogger/commits/master – Kyas Jan 20 '15 at 03:33
  • CREATE TABLE `oslog`.`entry` ( `osName` VARCHAR( 10 ) NOT NULL , `version` VARCHAR( 10 ) NOT NULL , `notes` VARCHAR( 20 ) NOT NULL , PRIMARY KEY ( `osName` ) ) ENGINE = InnoDB; is my DB – Kyas Jan 20 '15 at 03:56
  • Have you at least READ the answer provided ? Your error has nothing to do with the database, and it is explained extensively along with the right way to do it in the answer above, so please read it, try it, then when it work, accept the answer by marking the white V in top left corner. Then, if you still have problems, they are other problems, and need to be posted as separate, atomic questions – Andrea Ligios Jan 20 '15 at 09:27