Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Option -2: Create an SQL or Access database with all your friends\' information

ID: 3816146 • Letter: O

Question

Option -2: Create an SQL or Access database with all your friends' information and write a program to load the names in the combo box and also search the database and find yourfriend's record off the database by nameand retrieve all the information from the database to the form including your friend's picture. You should be able to add new friends, update an existing friends' information and delete a friend's record. See the sample below Matt Alimagham Sample of Final Project Delete Search Add Angelina Jolie Charlize Theron Nicole Kidman Hillary Clinton John McCain Hu Jintao Jennifer Aniston Jacob Zuma Anwar Ibraham Kevin Rudd Bartholomew I Ben Bernanke Moe Green Robert Gates Il Michelle Bachelet

Explanation / Answer

studentSearch.jsp
------------------------
<%@ page import="java.util.*" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>

<html>
<head>
</head>
<body>
   <table width="700px" align="center">
       <tr>
           <td colspan=4 align="center"><b>User
                   Record</b></td>
       </tr>
       <tr>
           <td><b>Student Name</b></td>
           <td><b>Student Id</b></td>
           <td><b>Email</b></td>
           <td><b>Registration Date</b></td>
       </tr>
       <%
int count = 0;
String color = "#F9EBB3";
if (request.getAttribute("piList") != null) {
ArrayList al = (ArrayList) request.getAttribute("student_list");
System.out.println(al);
Iterator itr = al.iterator();
while (itr.hasNext()) {

if ((count % 2) == 0) {
color = "#eeffee";
}
count++;
ArrayList student_list = (ArrayList) itr.next();
%>
       <tr>
           <td><%=student_list.get(0)%></td>
           <td><%=student_list.get(1)%></td>
           <td><%=student_list.get(2)%></td>
           <td><%=student_list.get(3)%></td>
       </tr>
       <%
}
}
if (count == 0) {
%>
       <tr>
           <td colspan=4 align="center"><b>No
                   Record Found..</b></td>
       </tr>
       <% }
%>
   </table>
</body>
</html>

student.jsp
--------------------

<%@ page language="java" import="java.util.*;"%>
<%
   List studentList;
   int nouser;
   int i = 1;
   List cityList;
   int nocity;
   int j = 1;
%>
<%
   studentList = (ArrayList) request.getAttribute("studentList");
   nouser = studentList.size();
%>


<html>
<head>
<title>dynamic combobox in servlet</title>
</head>
<body>
   <br>
   <br>
   <center>
       <table border="1" width="300px" bgcolor="bluelight" cellspacing="0"
           cellpadding="0">
           <tr>
               <td width="100%">
                   <form method="GET" action="jsp/studentList">
                       <font color="red"><h3 align="center">Dynamic Combobox
                               List</font>
                       </h3>
                       <table border="1" width="300px" cellspacing="0" cellpadding="0">
                           <tr>
                               <td width="50%"><b>Student Name:</b></td>
                               <td width="50%"><select name="studentId" value="">
                                       <option value="0">---Select Name---</option>
                                       <%
                                           Iterator userit = studentList.iterator();
                                           while (userit.hasNext()) {
                                               while (i <= nouser) {
                                       %>
                                       <option value="<%=i%>"><%=userit.next()%></option>
                                       <%
                                           i++;
                                               }
                                           }
                                       %>
                               </select></td>
                           </tr>
                          
                       </table>

                   </form>
               </td>
           </tr>
       </table>
   </center>
</body>

</html>


package com.napier.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StudentSearch extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "dbname";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "dbpass";

Statement st;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, userName, password);
System.out.println("Connected!");
String sid = request.getParameter("sid");

ArrayList arrayList = null;
ArrayList student_list = new ArrayList();
String query = "select * from students where sid='" + sid + "' ";

System.out.println("query " + query);
st = conn.createStatement();
ResultSet rs = st.executeQuery(query);

while (rs.next()) {
arrayList = new ArrayList();

arrayList.add(rs.getString(1));
arrayList.add(rs.getString(2));
arrayList.add(rs.getString(3));
arrayList.add(rs.getString(4));

System.out.println("al :: " + arrayList);
student_list.add(arrayList);
}

request.setAttribute("student_list", student_list);
RequestDispatcher view = request.getRequestDispatcher("/studentSearch.jsp");
view.forward(request, response);
conn.close();
System.out.println("Disconnected!");
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

package com.napier.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StudentServlet extends HttpServlet {
   public void doGet(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       response.setContentType("text/html");
       PrintWriter pw = response.getWriter();
       Connection con = null;
       String url = "jdbc:mysql://10.5.3.121:3306/";
       ;
       String db = "hr";
       String driver = "com.mysql.jdbc.Driver";
       String userName = "hr";
       String password = "hr";
       try {
           Class.forName(driver);
           con = DriverManager.getConnection(url + db, userName, password);
           Statement st = con.createStatement();
           ResultSet rs = st.executeQuery("select * from student");
           List studentList = new ArrayList();
           List clist = new ArrayList();
           while (rs.next()) {
               //request.setAttribute("studentId", rs.getString(2));
               request.setAttribute("studentName", rs.getString(3));
               pw.println("studentId" + " " + "studentName" + "<br>");
               pw.println(rs.getString(2) + " " + rs.getString(3) + "<br>");
               //studentList.add(rs.getString(2));
               studentList.add(rs.getString(3));

           }
           request.setAttribute("studentList", studentList);

       } catch (Exception e) {
           pw.println(e);
       }
       RequestDispatcher dispatcher = getServletContext()
               .getRequestDispatcher("/jsp/Combobox.jsp");
       dispatcher.forward(request, response);
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote