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

Programming Microsoft Visual Basic 2012/2015 - Create an SQL or Access database

ID: 3816169 • Letter: P

Question

Programming Microsoft Visual Basic 2012/2015 - 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 your friend’s record off the database by name and 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: UNABLE TO ATTACH SAMPLE JPG's

Explanation / Answer

<%@ 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.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);
   }

}