数据库未连接Tomcat,Servlet,MYSQL

时间:2018-09-01 09:49:03

标签: java html mysql tomcat servlets

第一篇文章

大家好,我正在尝试运行由另一个开发人员开发的Java Web项目。目前,我正在尝试运行该项目,但该项目未连接到数据库。在登录屏幕中,我正在从数据库中检索一些数据并将它们显示在下拉列表中,但是当用户输入名称和密码时,它不会显示任何错误消息或重定向到下一页,也不会在控制台中显示任何错误。 这是我的连接代码:

public final class MySqlConnect {
Logger logger = Logger.getLogger(MySqlConnect.class.getName());
public Connection conn;
private Statement statement;
public static MySqlConnect db;
private MySqlConnect() {

    String url= "jdbc:mysql://localhost:3306/";
     String dbName = "anish";
     String driver = "com.mysql.jdbc.Driver";
     String userName = "root";
     String password = "root";




    try {
        Class.forName(driver).newInstance();
        this.conn = (Connection)DriverManager.getConnection(url+dbName,userName,password);
    }
    catch (Exception sqle) {
        logger.info(sqle.getMessage());
    }
}

public static synchronized MySqlConnect getDbCon() {
    if ( db == null ) {
        db = new MySqlConnect();
    }
    return db;

}

public ResultSet query(String query) throws SQLException{
    statement = db.conn.createStatement();
    System.out.println(query);
    ResultSet res = statement.executeQuery(query);
    return res;
}

public int insert(String insertQuery) throws SQLException {
    statement = db.conn.createStatement();
    System.out.println(insertQuery);
    int result = statement.executeUpdate(insertQuery);
    return result;

}

public int insertAndgeneratedRowId(String insertQuery) throws SQLException {
    statement = db.conn.createStatement();
    System.out.println(insertQuery);

    int result = statement.executeUpdate(insertQuery,Statement.RETURN_GENERATED_KEYS);
    int  auto_id = 0;
    if(result > 0) {
       ResultSet rs = statement.getGeneratedKeys();
        rs.next();
        auto_id = rs.getInt(1);
    }
    return auto_id;

}

}

似乎连接正常,因为我正在获取下拉列表的数据,但是 当我查询某些东西时,它什么也没显示。 例如用户输入名称和密码后,应重定向到下一页。

这是HTML文件

 <!DOCTYPE html>
<html>
<head>
<title>Login here</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Tangerine">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Allerta+Stencil">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Oxygen:400,700">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="css/style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
 <script type="text/javascript" src="js/LoginPage.js"></script>
</head>
<body onload="loadLoginPageData()">
<div class="row">
  <div class="extra_column" style="background-color:#fff;">
<form action="LoginController" method="post">

    <h2> Get Started </h2>
    <label style="color:#8e44ad;margin-right:10px;"><b>SELECT ASSEMBLY: </b></label>
    <select id="slloginAssembly" name="Assembly">
    <option>Select Assembly</option>
  </select><br><br>

    <input type="text" class="inp" id="loginUserID" placeholder="Enter Userno." name="uname" required>
    <br><br>
    <input type="radio" id="v1" name="Verifiactiontype" onclick="chooseType(this.value)" value="password" checked> Password
    <input type="radio" id="v2" name="Verifiactiontype" onclick="chooseType(this.value)" value="OTP"> OTP<br><br>

    <input type="password" id="loginPassword" placeholder="Enter Password" name="psw" required>
    <br><br>
    <p>
    <input type="submit" id="btnLogin" value="SIGN IN"  class="w3-button w3-green w3-round-xlarge">
    <input type="button" id="btnGenerate" value="VERIFY" class="w3-button w3-green w3-round-xlarge" style="display: none;" onclick="generateOTP()">
    </p>

    <!-- <span>Forgot<a href="#">password?</a></span> -->

</form>
</div>
  <div class="column" style="background:linear-gradient(220deg,  #9933ff, #a64dff);color:white">
    <h3>WELCOME TO</h3>
    <p style="text-align:center;"><img src="image/logo.PNG" alt="LOGO" style="width:244px;height:164px;"></p>
    <p style="margin-left: 2px;">Creating data driven solutions for</p>
    <p style="margin-left: -1px;">election campaigns</p>
  </div>
</div>
<script>

     $('#v1').focus(function() {
        $("#loginPassword").attr('placeholder', 'Enter Password')
        $("#btnLogin").attr('value', 'SIGN IN')
        document.getElementById("btnLogin").setAttribute("type","submit");
    })

    $('#v2').focus(function() {
        $("#loginPassword").attr('placeholder', 'Enter OTP')
        $("#btnLogin").attr('value', 'Generate')




    })

    $('#btnLogin').click(function() {

        $("#btnLogin").attr('value', 'Verify')

    })

</script> 
</body>
</html>

这是Java文件

 @WebServlet("/LoginController")
public class LoginController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    Logger logger = Logger.getLogger(LoginController.class.getName());

    AdminService adminServiceImpl ;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginController() {

        super();

        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(request.getParameter("caller").equals("generateOTP"))
        {
            String assembly=request.getParameter("AssemblyId");  
            String userid=request.getParameter("userId");  

            System.out.println(assembly+"  &  "+userid); 
            try {

                adminServiceImpl  = new AdminServiceImpl(); 
                adminServiceImpl.getGenerateOTP(assembly,userid); 
                response.getWriter().append("");
            } catch (Exception e) {
                logger.info(e.getMessage());
            }
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



            String assembly=request.getParameter("Assembly");  
            //String assembly=request.getParameter("LoginAssembly"); 
            String userid=request.getParameter("uname");  
            String logintype=request.getParameter("Verifiactiontype");  
            String password=request.getParameter("psw");  
            String otp=request.getParameter("LoginOTP");  


            adminServiceImpl  = new AdminServiceImpl();
            if(logintype.equalsIgnoreCase("password"))
            {
            LoginPojo loginPojo =   adminServiceImpl.validateWithPassword(assembly,userid,password);

                if(loginPojo.getStatus().equalsIgnoreCase("true"))
                {
                    HttpSession session = request.getSession(true); 
                    session.setAttribute("user", userid);
                    session.setAttribute("AssemblyID",assembly);
                    session.setAttribute("AssemblyName", loginPojo.getAssemblyName());
                    //session.setMaxInactiveInterval(30);

                    /* RequestDispatcher rd=request.getRequestDispatcher("jsp/MainPage.jsp");  
                        rd.forward(request,response); */
                    RequestDispatcher rd=request.getRequestDispatcher("Home.html");  
                    rd.forward(request,response); 
                }
                else {
                    // RequestDispatcher rd=request.getRequestDispatcher("jsp/LoginPage.html");  
                     RequestDispatcher rd=request.getRequestDispatcher("index.html");
                        rd.forward(request,response);
                }




            }else if(logintype.equalsIgnoreCase("OTP")){
                if(adminServiceImpl.loginWithOTP(assembly,userid,password))
                {
                    HttpSession session = request.getSession(true); 
                    session.setAttribute("user", userid);
                    session.setAttribute("AssemblyID",assembly);
                    session.setAttribute("AssemblyName", "Udaipur");
                    RequestDispatcher rd=request.getRequestDispatcher("Home.html");  
                    rd.forward(request,response);
                }
                else {
                    // RequestDispatcher rd=request.getRequestDispatcher("jsp/LoginPage.html");
                     RequestDispatcher rd=request.getRequestDispatcher("index.html");
                        rd.forward(request,response);
                } 
            }




          /*  if(LoginDao.validate(n, p)){  
                RequestDispatcher rd=request.getRequestDispatcher("servlet2");  
                rd.forward(request,response);  
            }  
            else{  
                out.print("Sorry username or password error");  
                RequestDispatcher rd=request.getRequestDispatcher("index.html");  
                rd.include(request,response);  
            }  */

    }

}

AdminServiceImpl.java

public class AdminServiceImpl implements AdminService {

Logger logger = Logger.getLogger(AdminServiceImpl.class.getName());
CommonUtil commonUtil = new CommonUtil();
Properties properties;

public String CreateAssembly(String data) {

    JSONObject jsonObj;
    try {
        jsonObj = new JSONObject(data);
        String AssemblyName = (String) jsonObj.get("AssemblyName");

        MySqlConnect con = MySqlConnect.getDbCon();

        int i = con.insert("INSERT INTO `tbl_assembly` (`assembly_name`, `assembly_status`) VALUES ('"
                + AssemblyName + "', '1');");

        if (i > 0)
            return "success";
        else
            return "Failure";
    } catch (JSONException e) {
        logger.info(e.getMessage()); 
    } catch (SQLException e) {
        logger.info(e.getMessage()); 
    }
    return null;
}

public JSONArray getAssembly() {
    JSONArray assemblyArray = new JSONArray();
    try {
        MySqlConnect con = MySqlConnect.getDbCon();

        ResultSet assemblyList = con.query("SELECT assembly_id, assembly_name,assembly_status FROM tbl_assembly ;");

        while (assemblyList.next()) {
            JSONObject jO = new JSONObject();
            jO.put("AssemblyID", assemblyList.getInt(1));
            jO.put("AssemblyName", assemblyList.getString(2));
            jO.put("AssemblyStatus", assemblyList.getInt(3));
            assemblyArray.put(jO);
        }

        if (assemblyArray != null) {
            return assemblyArray;
        }

    } catch (JSONException e) {
        logger.info(e.getMessage()); 
    } catch (SQLException e) {
        logger.info(e.getMessage()); 
    }

    return new JSONArray();
}

public int toggleAssemblyStatus(String data) {
    JSONObject jsonObj;
    try {
        jsonObj = new JSONObject(data);
        int recordId = (Integer) jsonObj.get("recordId"); 
        MySqlConnect con = MySqlConnect.getDbCon();
        int i = con
                .insert("UPDATE `tbl_assembly` SET `assembly_status`=  case when assembly_status = 0 then 1 else 0 end WHERE `assembly_id`='"
                        + recordId + "'");
        int currentstatus = 0;
        if (i > 0) {
            ResultSet rs = con
                    .query("SELECT assembly_status FROM tbl_assembly WHERE `assembly_id`='" + recordId + "';");
            if (rs.next()) {
                currentstatus = rs.getInt(1);
            }

            return currentstatus;
        } else {
            return currentstatus;
        }
    } catch (JSONException e) {
        logger.info(e.getMessage()); 
    } catch (SQLException e) {
        logger.info(e.getMessage()); 
    }
    return 0;
}

public JSONArray getPSAssembly() {
    JSONArray assemblyArray = new JSONArray();
    try {
        MySqlConnect con = MySqlConnect.getDbCon();

        ResultSet assemblyList = con
                .query("SELECT assembly_id, assembly_name FROM tbl_assembly where assembly_status = 1  ;");

        while (assemblyList.next()) {
            JSONObject jO = new JSONObject();
            jO.put("AssemblyID", assemblyList.getInt(1));
            jO.put("AssemblyName", assemblyList.getString(2));

            assemblyArray.put(jO);
        }

        if (assemblyArray != null) {
            return assemblyArray;
        }

    } catch (JSONException e) {
        logger.info(e.getMessage()); 
    } catch (SQLException e) {
        logger.info(e.getMessage()); 
    }

    return new JSONArray();
}

public String CreatePS(String data) {
    JSONObject jsonObj;
    try {
        jsonObj = new JSONObject(data);
        String AssemblyID = (String) jsonObj.get("AssemblyID");
        String PSName = (String) jsonObj.get("PolingStationName");

        MySqlConnect con = MySqlConnect.getDbCon();

        int i = con.insert("INSERT INTO `tbl_poling_stations` (`location_name`, `assembly_id`) VALUES ('" + PSName
                + "', '" + AssemblyID + "');");

        if (i > 0)
            return "success";
        else
            return "Failure";
    } catch (JSONException e) {
        logger.info(e.getMessage()); 
    } catch (SQLException e) {
        logger.info(e.getMessage()); 
    }
    return null;
}

public JSONArray getPSList(String data) {
    JSONArray PSArray = new JSONArray();
    JSONObject jsonObj;
    try {

        jsonObj = new JSONObject(data);
        String AssemblyID = (String) jsonObj.get("AssemblyID");

        MySqlConnect con = MySqlConnect.getDbCon();

        ResultSet PSList = con.query(
                "SELECT location_id,location_name FROM tbl_poling_stations where assembly_id = " + AssemblyID);

        while (PSList.next()) {
            JSONObject jO = new JSONObject();
            jO.put("PSID", PSList.getInt(1));
            jO.put("PSName", PSList.getString(2));

            PSArray.put(jO);
        }

        if (PSArray != null) {
            return PSArray;
        }

    } catch (JSONException e) {
        logger.info(e.getMessage()); 
    } catch (SQLException e) {
        logger.info(e.getMessage()); 
    }

    return new JSONArray();
}

public String CreateBooth(String data) {
    JSONObject jsonObj;
    try {
        jsonObj = new JSONObject(data);
        String AssemblyID = (String) jsonObj.get("AssemblyID");
        String PolingStationID = (String) jsonObj.get("PolingStationID");
        String BoothName = (String) jsonObj.get("BoothName");

        MySqlConnect con = MySqlConnect.getDbCon();

        int i = con.insert("INSERT INTO `tbl_booth` (`booth_name`, `booth_status`, `polingstation_id`) VALUES ('"
                + BoothName + "', '1', '" + PolingStationID + "');");

        if (i > 0)
            return "success";
        else
            return "Failure";
    } catch (JSONException e) {
        logger.info(e.getMessage()); 
    } catch (SQLException e) {
        logger.info(e.getMessage()); 
    }
    return null;
}

public JSONArray getBoothList(String data) {
    JSONArray assemblyArray = new JSONArray();
    JSONObject jsonObj;
    try {

        jsonObj = new JSONObject(data);
        String PolingStation = (String) jsonObj.get("PolingStation");
        MySqlConnect con = MySqlConnect.getDbCon();

        ResultSet boothList = con
                .query("SELECT booth_id,booth_name,booth_status FROM tbl_booth where polingstation_id = "
                        + PolingStation + ";");

        while (boothList.next()) {
            JSONObject jO = new JSONObject();
            jO.put("booth_id", boothList.getInt(1));
            jO.put("booth_name", boothList.getString(2));
            jO.put("booth_status", boothList.getInt(3));
            assemblyArray.put(jO);
        }

        if (assemblyArray != null) {
            return assemblyArray;
        }

    } catch (JSONException e) {
        logger.info(e.getMessage()); 
    } catch (SQLException e) {
        logger.info(e.getMessage()); 
    }

    return new JSONArray();
}

public String CreateColony(String data) {
    JSONObject jsonObj;
    try {
        jsonObj = new JSONObject(data);
        String AssemblyID = (String) jsonObj.get("AssemblyID");
        String PolingStationID = (String) jsonObj.get("PolingStationID");
        String boothID = (String) jsonObj.get("BoothID");
        String Colonyname = (String) jsonObj.get("Colonyname");

        MySqlConnect con = MySqlConnect.getDbCon();

        int i = con
                .insertAndgeneratedRowId("INSERT INTO `tbl_colony` (`colony_name`) VALUES ('" + Colonyname + "');");

        if (i > 0) {

            int j = con.insertAndgeneratedRowId("INSERT INTO `txn_booth_colony` (`booth_id`, `colony_id`) VALUES ('"
                    + boothID + "', '" + i + "');");
            if (j > 0)
                return "success";
            else
                return "Failure";

        } else {

        }

    } catch (JSONException e) {
        logger.info(e.getMessage()); 
    } catch (SQLException e) {
        logger.info(e.getMessage()); 
    }
    return null;
}

public JSONArray getColonyList(String data) {

    JSONArray assemblyArray = new JSONArray();
    JSONObject jsonObj;
    try {

        jsonObj = new JSONObject(data);
        String boothID = (String) jsonObj.get("boothID");
        MySqlConnect con = MySqlConnect.getDbCon();

        ResultSet boothList = con
                .query("SELECT colony_id , colony_name FROM tbl_colony where colony_id in (SELECT colony_id FROM txn_booth_colony where booth_id = "
                        + boothID + ")");

        while (boothList.next()) {
            JSONObject jO = new JSONObject();
            jO.put("colony_id", boothList.getInt(1));
            jO.put("colony_name", boothList.getString(2));

            assemblyArray.put(jO);
        }

        if (assemblyArray != null) {
            return assemblyArray;
        }

    } catch (JSONException e) {
        logger.info(e.getMessage()); 
    } catch (SQLException e) {
        logger.info(e.getMessage()); 
    }

    return new JSONArray();

}

public JSONArray getLoginAssembly() {
    JSONArray assemblyArray = new JSONArray();
    try {
        MySqlConnect con = MySqlConnect.getDbCon();

        ResultSet assemblyList = con
                .query("SELECT assembly_id, assembly_name FROM tbl_assembly  where assembly_status = 1 ;");

        while (assemblyList.next()) {
            JSONObject jO = new JSONObject();
            jO.put("AssemblyID", assemblyList.getInt(1));
            jO.put("AssemblyName", assemblyList.getString(2));

            assemblyArray.put(jO);
        }

        if (assemblyArray != null) {
            return assemblyArray;
        }

    } catch (SQLException e) {
        logger.info(e.getMessage());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        logger.info(e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }

    return new JSONArray();
}

public void getGenerateOTP(String assembly, String userid) {

    try {
        properties = commonUtil.getProperty();

        URL obj = new URL(properties.getProperty(VplEndPoints.SendOTP.URL));

        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");

        String POST_PARAMS = "AssemblyID=" + assembly + "&UserID=" + userid;
        System.out.println(POST_PARAMS);
        // For POST only - START
        con.setDoOutput(true);
        OutputStream os = con.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();
        // For POST only - END

        int responseCode = con.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) { // success
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result

        } else {
            logger.info("POST request not worked");
        }
    } catch (IOException e) {
        logger.info(e.getMessage());
    }

}

public LoginPojo validateWithPassword(String assembly, String userid, String password) {

    LoginPojo LoginPojo = new LoginPojo();
    try {
        properties = commonUtil.getProperty();

        URL obj = new URL(properties.getProperty(VplEndPoints.LoginWithPassword.URL));

        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");

        String POST_PARAMS = "AssemblyID=" + assembly + "&UserID=" + userid + "&Password=" + password;
        // For POST only - START
        con.setDoOutput(true);
        OutputStream os = con.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();
        // For POST only - END

        int responseCode = con.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) { // success
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result

            ObjectMapper mapper = new ObjectMapper();
            LoginPojo = mapper.readValue(response.toString(), LoginPojo.class);

            return LoginPojo;

        } else {
            logger.info("POST request not worked");
        }
    } catch (IOException e) {
        logger.info(e.getMessage());
    }
    return null;
}

public boolean loginWithOTP(String assembly, String userid, String otp)  {

    LoginPojo LoginPojo = new LoginPojo();
    try {
        properties = commonUtil.getProperty();

        URL obj = new URL(properties.getProperty(VplEndPoints.LoginWithOTP.URL));

        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");

        String POST_PARAMS = "AssemblyID=" + assembly + "&UserID=" + userid + "&OTP=" + otp;
        System.out.println(POST_PARAMS);
        // For POST only - START
        con.setDoOutput(true);
        OutputStream os = con.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();
        // For POST only - END

        int responseCode = con.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) { // success
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result

            ObjectMapper mapper = new ObjectMapper();
            LoginPojo = mapper.readValue(response.toString(), LoginPojo.class);

            if (LoginPojo.getStatus().equalsIgnoreCase("true")) {
                return true;
            } else {
                return false;
            }

        } else {
            logger.info("POST request not worked");
        }
    } catch (IOException e) {
        logger.info(e.getMessage());
    }
    return false;
}

public boolean validateWithPassword_SU(String userid, String password) {
    LoginPojo LoginPojo = new LoginPojo();
    try {
        properties = commonUtil.getProperty();

        URL obj = new URL(properties.getProperty(VplEndPoints.SuperUserLoginWithPassword.URL));

        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");

        String POST_PARAMS = "UserID=" + userid + "&Password=" + password;
        // For POST only - START
        con.setDoOutput(true);
        OutputStream os = con.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();
        // For POST only - END

        int responseCode = con.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) { // success
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result
            System.out.println(response.toString());

            ObjectMapper mapper = new ObjectMapper();
            LoginPojo = mapper.readValue(response.toString(), LoginPojo.class);

            if (LoginPojo.getStatus().equalsIgnoreCase("true")) {
                return true;
            } else {
                return false;
            }

        } else {
            logger.info("POST request not worked");
        }
    } catch (IOException e) {
        logger.info(e.getMessage());
    }
    return false;
}

public boolean loginWithOTP_SU(String userid, String otp) {

    LoginPojo LoginPojo = new LoginPojo();
    try {
        properties = commonUtil.getProperty();

        URL obj = new URL(properties.getProperty(VplEndPoints.SuperUserLoginWithOTP.URL));

        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");

        String POST_PARAMS = "UserID=" + userid + "&OTP=" + otp;
        // For POST only - START
        con.setDoOutput(true);
        OutputStream os = con.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();
        // For POST only - END

        int responseCode = con.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) { // success
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result

            ObjectMapper mapper = new ObjectMapper();
            LoginPojo = mapper.readValue(response.toString(), LoginPojo.class);

            if (LoginPojo.getStatus().equalsIgnoreCase("true")) {
                return true;
            } else {
                return false;
            }

        } else {
            logger.info("POST request not worked");
        }
    } catch (IOException e) {
        logger.info(e.getMessage());
    }
    return false;
}

public void getGenerateOTP_SU(String userid) {
    try {
        properties = commonUtil.getProperty();

        URL obj = new URL(properties.getProperty(VplEndPoints.SuperUserSendOTP.URL));

        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");

        String POST_PARAMS = "UserID=" + userid;
        // For POST only - START
        con.setDoOutput(true);
        OutputStream os = con.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();
        // For POST only - END

        int responseCode = con.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) { // success
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result

        } else {
            logger.info("POST request not worked");
        }
    } catch (IOException e) {
        logger.info(e.getMessage());
    }

}

public void setAllDeactivate(int boothID, int colonyID) {
    try {

        MySqlConnect con = MySqlConnect.getDbCon();

        int i = con.insert("UPDATE `tbl_voter` SET `voter_status`='0' WHERE colony_id = " + colonyID
                + " and booth_id = " + boothID + ";");

    } catch (SQLException e) {
        logger.info(e.getMessage());
    }

}

public boolean isExist(String voterId) {
    // TODO Auto-generated method stub
    try {

        MySqlConnect con = MySqlConnect.getDbCon();

        ResultSet voterIdRS = con.query("SELECT voter_id FROM tbl_voter where voter_uid = '" + voterId + "'");

        while (voterIdRS.next()) {
            return true;
        }

    } catch (SQLException e) {
        logger.info(e.getMessage());
    }
    return false;
}

public void updateExisting(String voterId, String name, String cname, int houseNo, int age, String gender) {
    try {

        MySqlConnect con = MySqlConnect.getDbCon();
        String gend = "";
        if (gender == "male") {
            gend = "M";
        } else {
            gend = "F";
        }
        int UE = con.insert("UPDATE `tbl_voter` SET `voter_name`='" + name + "', `gender`='" + gend
                + "', `care_of_name`='" + cname + "', `age`=" + age + ", `plot_no`=" + houseNo
                + ", `voter_status`='1' WHERE voter_uid = '" + voterId + "';");

        if (UE > 0) {

        }

    } catch (SQLException e) {
        logger.info(e.getMessage());
    }

}

public void insertNew(String voterId, String name, String cname, int houseNo, int age, String gender) {
    try {

        MySqlConnect con = MySqlConnect.getDbCon();
        String gend = "";
        if (gender == "male") {
            gend = "M";
        } else {
            gend = "F";
        }
        int UE = con
                .insert("INSERT INTO `tbl_voter` (`voter_name`, `voter_uid`, `gender`, `care_of_name`, `age`, `plot_no`, `colony_id`, `booth_id`, `current_status`, `caste_id`, `is_voted`, `is_voter_parchi_send`, `question_id`, `option_id`, `voter_status`)"
                        + " VALUES ('" + name + "', '" + voterId + "', '" + gend + "', '" + cname + "', " + age
                        + ", " + houseNo + ", '1', '1', '1', '1', '0', '0', '0', '0', '1');");

        if (UE > 0) {

        }

    } catch (SQLException e) {
        logger.info(e.getMessage());
    }

}

}

0 个答案:

没有答案