manish thakur
manish thakur

Reputation: 820

How to use a variable value declared in servlet into a new Java class?

I have a login servlet where I have a login query in my post method from the query I am getting username, password, company name and ID

I am storing all this values in a variable like

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    String companyDB,nameDB,idDB;

        try {
                    con = DBConnection.createConnection();
                    statement = con.createStatement();
                    String sql = "  SELECT MT_USERS.MT_USERS_VCLOGINCODE AS USERID, MT_USERS.MT_USERS_VCUSERPASSWORD AS PASSWORDID, MT_USERS.MT_USERS_VCUSERNAME AS NAME, (SELECT MT_DISTRIBUTR_VCDISTRIBUTRNAME FROM MT_DISTRIBUTR WHERE MT_DISTRIBUTR_VCDISTRIBUTRCODE = MT_USERS.MT_DISTRIBUTR_VCDISTRIBUTRCODE) AS COMPANYNAME ,(SELECT mt_distributr_vcdistributrcode FROM mt_distributr WHERE MT_DISTRIBUTR_VCDISTRIBUTRCODE = MT_USERS.MT_DISTRIBUTR_VCDISTRIBUTRCODE) AS ID FROM MT_USERS WHERE MT_USERS_VCLOGINCODE='admin' AND MT_USERS_VCUSERPASSWORD ='admin'";
                    resultSet = statement.executeQuery(sql);
                    if (resultSet.next()) {

                        companyDB = resultSet.getString("COMPANYNAME");
                        nameDB = resultSet.getString("name");
                        idDB = resultset.getString("ID");  


                    }

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

}
}

Now I have an another class where I am writing a query and in that query I want to use idDB like

My new class is

 public class Outlet {
    Connection con = null;
    Statement statement = null;
    ResultSet resultSet = null;

    public List<String> getoutlet() throws ClassNotFoundException, SQLException {
        List<String> list = new ArrayList<String>();
        con = DBConnection.createConnection();
        statement = con.createStatement();

        try {

            ResultSet resultSet = statement.executeQuery("select * from ecustomer where CUSTOMERIDENTIFIER in(select CUSTOMERIDENTIFIER from mt_distributrol where mt_distributr_vcdistributrcode = 'AAAA'");
            while (resultSet.next()) {
                list.add(resultSet.getString("CUSTOMERDESCRIPTOR"));

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

        return list;
    }

}

Where mt_distributr_vcdistributrcode = 'AAAA'" at the place of 'AAAA' I have to pass a variable which has the value of idDB

Upvotes: 0

Views: 106

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522817

You may use a prepared statement here:

String sql = "SELECT CUSTOMERDESCRIPTOR FROM ecustomer WHERE CUSTOMERIDENTIFIER IN (";
sql += "SELECT CUSTOMERIDENTIFIER FROM mt_distributrol ";
sql += "WHERE mt_distributr_vcdistributrcode = ?)");
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, "AAAA");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
    list.add(resultSet.getString("CUSTOMERDESCRIPTOR"));
}

I actually find that MkYong does a good job of explaining prepared statements in Java, see here, but any good documentation is a good place to start looking. And see Oracle Tutorial.

Upvotes: 2

Related Questions