简体   繁体   中英

Retrieve and Display Data in JSP from Mysql

I have two table in my database 1. users 2. applicant_detail

I have created a form that takes the input from user for the applicant_detail table and submit it into DB. Now I want to create a JSP that allows user to login and if login is valid display the detail of specific user (applicant) if it exists in the applicant_detail table. the code of HallTicket.jsp

<div class="boxdis">
        <div id="container">
            <h1>&bull; LOGIN &bull;</h1>
            <div class="underline"></div>
            <form action="HallTicket1.jsp" method="post">

                <div class="username">
                    <label for="username"></label> <input type="text"
                        placeholder="My username is" name="username" required>
                </div>
                <div class="pwd">
                    <label for="pwd"></label> <input type="password"
                        placeholder="My password is" name="pwd" required>
                </div>
                <div class="submit">
                    <input type="submit" value="Access Form" id="form_button" />
                </div>
            </form>
            <!-- // End form -->
        </div>
    <div class="warning">
        <p>${errorMessage}</p>
        <c:remove var="errorMessage" scope="session" />
    </div>

The code for the "HallTicket1.jsp"


    <%@ page import="java.sql.*"%>
    <%@ page import="javax.sql.*"%>
    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.sql.Statement"%>
    <%@page import="java.sql.Connection"%>
    <%
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/cinushi_university", "CinthiyaSingh",
                "Cinthiya@098");
        PreparedStatement ps = con.prepareStatement("select *from users where username=?");
        ps.setString(1, username);
        ResultSet rs = ps.executeQuery();
        Statement st = con.createStatement();
        rs = st.executeQuery("select * from users where username='" + username + "' and pwd='" + pwd + "'");
        if (rs.next()) {
            PreparedStatement ps2 = con.prepareStatement("select * from applicant_detail where username=?");
            ps2.setString(1, username);
            ResultSet rs1= ps2.executeQuery();




        }
        else {
            request.getSession().setAttribute("errorMessage",
                    "Invalid/ Unmatched Username or Password, or you might not have Registered. Please Try Again or Register through Apply Now -> Application Guidelines tab.");
            request.getRequestDispatcher("HallTicket.jsp").forward(request, response);
        }
    %>
</body>
</html>

I need to put the code for the part where the login credential is validated from users table however the details are in the applicant_detail table.

The following code might help to understand the applicant_detail table as well

<%@page import="java.sql.*"%>
<%@page import="javax.sql.*"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String username=request.getParameter("username");
String firstname=request.getParameter("firstname");
String lastname=request.getParameter("lastname");
String email=request.getParameter("email");
String phone=request.getParameter("phone");
String gender=request.getParameter("gender");
String category=request.getParameter("category");
String DOB=request.getParameter("DOB");
String PH=request.getParameter("PH");
String Fathername=request.getParameter("Fathername");
String Mothername=request.getParameter("Mothername");
String marital=request.getParameter("marital");
String Nationality=request.getParameter("Nationality");
String Religion=request.getParameter("Religion");
String Addline1=request.getParameter("Addline1");
String Addline2=request.getParameter("Addline2");
String AddState=request.getParameter("AddState");
String AddCountry=request.getParameter("AddCountry");
String Pincode=request.getParameter("Pincode");
String Add2line1=request.getParameter("Add2line1");
String Add2line2=request.getParameter("Add2line2");
String Add2State=request.getParameter("AddState");
String Add2Country=request.getParameter("Add2Country");
String Pin2code=request.getParameter("Pin2code");
String Mat_Board=request.getParameter("Mat_Board");
String Mat_School=request.getParameter("Mat_School");
String Mat_Year=request.getParameter("Mat_Year");
String Mat_Percent=request.getParameter("Mat_Percent");
String Int_Board=request.getParameter("Int_Board");
String Int_School=request.getParameter("Int_School");
String Int_Year=request.getParameter("Int_Year");
String Int_Percent=request.getParameter("Int_Percent");
String prog_group=request.getParameter("select1");
String prog_name=request.getParameter("select2");
String prog_code=request.getParameter("select3");
String pass_state=request.getParameter("pass_state");
String center_choice1name=request.getParameter("centerchoice1");
String center_choice1code=request.getParameter("center1code");
String center_choice1zone=request.getParameter("zone1");
String center_choice2name=request.getParameter("centerchoice2");
String center_choice2code=request.getParameter("center2code");
String center_choice2zone=request.getParameter("zone2");
String Aadhaar=request.getParameter("Aadhar");


Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/cinushi_university", "CinthiyaSingh", "Cinthiya@098");
PreparedStatement ps = con.prepareStatement("select *from applicant_detail where username=?");
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
     request.getSession().setAttribute("errorMessage", "The Application for the given username is already submitted you can proceed to PAYMENT DETAILS under APPLY NOW tab for completing the payment process for the application, you can check the application form under the HALL TICKET tab and print it for the further reference.");
     request.getRequestDispatcher("ApplicationStatus.jsp").forward(request, response);

}
else{
     PreparedStatement ps2 = con.prepareStatement("insert into applicant_detail(username, firstname, lastname, email, contact, gender, category, DOB, PH, fathername, mothername, marital_status, nationality, religion, addressline1, addressline2, state, country, pincode, address2line1, address2line2, state2, country2, pin2code, m_board, m_school, m_p_year, m_percentage, i_board, i_school, i_p_year, i_percentage, program_group, program_name, P_code, state_last_exam, center_choice1name, center_choice1code, center_choice1zone, center_choice2name, center_choice2code, center_choice2zone, aadhaar )" + "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
     //setting value for "?"
     ps2.setString(1, username);
     ps2.setString(2, firstname);
     ps2.setString(3, lastname);
     ps2.setString(4, email);
     ps2.setString(5, phone);
     ps2.setString(6, gender);
     ps2.setString(7, category);
     ps2.setString(8, DOB);
     ps2.setString(9, PH);
     ps2.setString(10, Fathername);
     ps2.setString(11, Mothername);
     ps2.setString(12, marital);
     ps2.setString(13, Nationality);
     ps2.setString(14, Religion);
     ps2.setString(15, Addline1);
     ps2.setString(16, Addline2);
     ps2.setString(17, AddState);
     ps2.setString(18, AddCountry);
     ps2.setString(19, Pincode);
     ps2.setString(20, Add2line1);
     ps2.setString(21, Add2line2);
     ps2.setString(22, Add2State);
     ps2.setString(23, Add2Country);
     ps2.setString(24, Pin2code);
     ps2.setString(25, Mat_Board);
     ps2.setString(26, Mat_School);
     ps2.setString(27, Mat_Year);
     ps2.setString(28, Mat_Percent);
     ps2.setString(29, Int_Board);
     ps2.setString(30, Int_School);
     ps2.setString(31, Int_Year);
     ps2.setString(32, Int_Percent);
     ps2.setString(33, prog_group);
     ps2.setString(34, prog_name);
     ps2.setString(35, prog_code);
     ps2.setString(36, pass_state);
     ps2.setString(37, center_choice1name);
     ps2.setString(38, center_choice1code);
     ps2.setString(39, center_choice1zone);
     ps2.setString(40, center_choice2name);
     ps2.setString(41, center_choice2code);
     ps2.setString(42, center_choice2zone);
     ps2.setString(43, Aadhaar);

     int i = ps2.executeUpdate();
     if (i > 0) {
          //redirect
         request.getSession().setAttribute("errorMessage", "The Application is successfully submitted and will be considered complete post the payment is made, please proceed to PAYMENT DETAILS tab under APPLY NOW tab for completing the payment process for the application. You can check the application form under the HALL TICKET tab and print it for the further reference.");
         request.getRequestDispatcher("ApplicationStatus.jsp").forward(request, response);

     }  
}
con.close();

%>

I found the solution to this, we can retrieve that data and post it in table format using the following set

<%
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/cinushi_university", "CinthiyaSingh",
                "Cinthiya@098");
        PreparedStatement ps = con.prepareStatement("select *from users where username=?");
        ps.setString(1, username);
        ResultSet rs = ps.executeQuery();
        Statement st = con.createStatement();
        rs = st.executeQuery("select * from users where username='" + username + "' and pwd='" + pwd + "'");
        if (rs.next()) {
            PreparedStatement ps2 = con.prepareStatement("select * from applicant_detail where username=?");
            ps2.setString(1, username);
            ResultSet rs1 = ps2.executeQuery();
                while (rs1.next()) {
%>
<%=rs1.getString("field_name_from_database")%>

    %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM