Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Creating an Application with JSP, JavaBeans and MySQL

Developing an application with JSP and JavaBeans using MySQL Database.

First, you need to choose the database and as an example, we will use MySQL. If you do not have it, download it from the http://dev.mysql.com/downloads site. It is very simple to install and use. Once installed, to run the MySQL use the executable winmysqladmin.exe located in the C:\mysql\bin, then the icon of a semaphore will appear on the taskbar near the clock.

Once up and running, you need to configure it, so enter to Host Name “Localhost” and for User Name and Password use “Root”, to run on the local machine.

Then you must create the database and table, you can use any manipulation program, we will use the MySQL Control Center, also available on the MySQL website: http://dev.mysql.com/downloads/other/mysqlcc site.html.

For the example, we will create a database with the name “Test” and then we will create a table called person with the columns name, age and gender. Now we are ready to use our database.

Configuring the server to run the JSP

JSP (Java Server Pages) are codes that when combined with HTML tags produce dynamic pages, which are processed by the server. As a server, we can use the Jakarta-Tomcat that can be found on the site http://jakarta.apache.org/tomcat/ or JBoss with Tomcat found in http://www.JBoss.org/downloads/index site.

So first step is to configure the server to be able to perform these dynamic pages. If you choose Tomcat, you must start it by running the file C:\Tomcat 5.0\bin\startup.bat, which will open a window showing the boot. It takes a while, but then you can now run your JSP files to it record your JSP files in the path C:\Tomcat 5.0\webapps\ROOT\, and run them through the browser by typing the path http://localhost:8080/. For example, if you create a file test.jsp the same would be recorded in the path C:\Tomcat 5.0\webapps\ROOT\test.jsp and then would run in the browser using the address http://localhost:8080/test.jsp, and when you want to shut down Tomcat run the file C:\Tomcat 5.0\bin\shutdown.bat.

If you choose the JBoss is much like Tomcat, JBoss has since integrated Tomcat and the same way you must boot the server through the file C:\JBoss-3.0.8_tomcat-4.1.24\bin\run.bat then you create a folder in the path C:\JBoss-3.0.8_tomcat-4.1.24\server\default\deploy\ with the .war extension. For example, if you want to create a test folder it would be as follows: C:\JBoss-3.0.8_tomcat-4.1.24\server\default\deploy\teste.war and inside will be our JSP files.

We will use the JBoss for our implementation and we will do a simple example to test the server, create the test.war folder, and then create a simple page and save into the test.war folder.

Listing 1. Simple HTML page.

<%@ page contentType="text/html"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
  
<head>  
<title>Test JSP</title>  
</head>  
  
<body>  
   <%out.println("Successfully conducted test");%>  
</body>  
</html>  

Then start the server and enter the address http://localhost:8080/test/test.jsp in the browser. Here the test folder does not need to come together with the .war extension.

Note that the JSP code is between <% and %>, and if all goes well you will see the JSP code message on the screen.

To end the JBoss run the C:\JBoss-3.0.8_tomcat-4.1.24\bin\shutdown.bat. If you are having trouble try to close the JBoss or Tomcat and initializes it again.

Accessing MySQL through the JSP using JBoss server with integrated Tomcat

Now let's test the connection to the MySQL Database using JSP. Initially we need to download the MySQL driver, which is a .jar file available on http://dev.mysql.com/downloads/ site and copy it to the C:\JBoss-3.0.8_tomcat-4.1.24\server\default\lib.

We will develop a page to test the connection to the MySQL database and save with testConnection.jsp name in the folder C:\JBoss-3.0.8_tomcat-4.1.24\server\default\deploy\test.war created earlier.

Listing 2. Testing the connection.

<%@ page contentType="text/html" language="java" import="java.sql.*"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  
<head>  
   <title>Connection Test</title>  
</head>  
  
<body>  
  
<%  
      Connection con;  
   Statement stm;  
     
   try {  
  
      Class.forName("org.gjt.mm.mysql.Driver");  
      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
      stm = con.createStatement();   
      out.println("Connection successfully executed");  
           
   } catch (Exception e) {  
      out.println("Could not connect to the database" + e.getMessage());  
   }  
%>  
  
</body>  

Then start MySQL, the JBoss server and runs in the browser the address: http://localhost:8080/test/testConnection.jsp. If it works, will appear on the screen the message “Connection successfully executed”.

Using JSP to access JavaBeans

JavaBeans is nothing more than Java files accessed through dynamic pages, or through the JSP. Our purpose will be to access a JavaBeans to do all the handling of the database, but before we test a simple example. Initially we will make a java applet following some conventions “get” to obtain data and “set” to assign data. For this, we can use any editor, including Notepad.

Listing 3. TestBean.

package testBean;  
  
public class TestBean {  
     
   String txt = "";  
     
   public void setText(String text){  
      txt = text;  
   }  
     
   public String getText(){  
      return txt;  
   }     
}

So within the test.war folder create a folder called WEB-INF, which is where you will be searched all JavaBenas and within the WEB-INF folder is created the class folder and the lib folder. In the folder classes can burn the files with .class extension and the lib folder can burn the files with .jar extension. Always save the JavaBeans in packages to avoid inconvenience. Let's create a .jar file for this example and save it as testBean.jar in the lib folder, leaving the way next C:\JBoss-3.0.8_tomcat-4.1.24\server\default\deploy\test.war\WEB-INF\lib\testBean.jar or if you prefer you can record all testBean package within the classes folder C:\JBoss-3.0.8_tomcat-4.1.24\server\default\deploy\teste.war\WEB-INF\classes\testBean\TestBean.class.

Now within the test.war folder will create the JSP file that will call the JavaBeans. Save it as testBean.jsp.

Listing 4. Using the bean.

<jsp:useBean id="testObj" class="testBean.TestBean">  
<jsp:setProperty name="testObj" property="text" value="Testing..."/>  
</jsp:useBean>  
  
<html>  
   <head>  
      <title>Test Jsp</title>  
   </head>  
     
   <body>  
      <h1>Test JSP</h1>  
      <p>The message test is:  
         <jsp:getProperty name="testObj" property="text"/>  
      </p>  
   </body>  
</html>

The command creates an object TestObj of the TestBean class. The command through the object TestObj uses the setProperty to call the setText method combines the property=“text” to setProperty and calls the setText method JavaBeans passing as parameter value “Testing...”. The same is true for the command that calls the getText method by printing the variable txt JavaBeans of the screen.

You can also use the object created to call the methods of JavaBeans without using getProperty, placing the JSP code between <% and %>, for example might call the getText method as follows <%=TestObj.getText()%> using the sign = to print on the screen, or have printed using the <%out.print(TestObj.getText())%> as the getText() method returns a string.

Then start the JBoss server and runs in the browser address http://localhost:8080/test/testBean.jsp. If printing the message “Testing...” on the screen, the call to JavaBeans occurred correctly.

Using JSP and JavaBeans to access MySQL

Now let's improve our database with connection example, causing the JavaBeans make the connection and the JSP access the results through it. First, we will create a JavaBeans to make the connection, much like the JSP that we did earlier.

Listing 5. Connection class.

package connection;  
  
import java.sql.*;  
  
public class Connection{  
     
   public Connection con;  
   public Statement stm;  
   private String sit = "";  
   
   public Connection() {  
  
      try {  
  
         Class.forName("org.gjt.mm.mysql.Driver");  
         con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
         stm = con.createStatement();   
  
         sit = "Connection successfully executed";  
  
           
      } catch (Exception e) {  
         sit = "Could not connect to the database" + e.getMessage();  
      }  
   }  
     
   public String getSituation(){  
      return sit;     
   }  
}  

So just as previously shown save the JavaBeans as Connection.jar within the test.war\WEB-INF\lib and create a JSP file and save as connection.jsp within the test.war folder.

Listing 6. Connection test.

<%@page contentType="text/html"%>  
<jsp:useBean id="con" class="connection.Connection"/>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  
<html>  
   <head>  
      <title>Connection test</title>  
   </head>  
     
   <body>  
      <h1>Connection test</h1>  
      <p>The database situation o situation is:  
         <jsp:getProperty name="con" property="situation"/>  
      </p>  
   </body>  
</html>  

Then start the JBoss and runs in the browser address http://localhost:8080/test/connection.jsp. If this works, we are ready to start developing the application itself.

Application Development

Now you can implement the following code. Let's take a brief explanation of each, leaving you to analyze the code to understand their roles.

Connection.java

We will further enhance the previous JavaBeans causing it to be responsible for all handling in the database and saves it as Connection.jar in the lib folder.

Listing 7. Connecting.

package connection;  
  
import java.sql.*;  
  
public class Connection {  
     
   public Connection con;  
   public Statement stm;  
   public ResultSet res = null;  
   private String name = null;  
   private int idade = 0;  
   private String sex = null;  
  
   public Connection() {  
  
      try {  
  
         Class.forName("org.gjt.mm.mysql.Driver");  
         con =  
            DriverManager.getConnection(  
              "jdbc:mysql://localhost:3306/test",  
              "root",  
              "root");  
         stm = con.createStatement();   
           
      } catch (Exception e) {  
         System.out.println("could not connect to database" + e.getMessage());  
      }  
   }  
     
   public void setName(String name){  
      this.nome = name;  
   }  
        
   public void setAge(int age){  
      this.age = age;  
   }  
  
   public void setSex(String sex){  
      this.sexo = sex;  
   }  
  
   public String getName(){  
      return name;  
   }  
  
   public int getAge(){  
      return age;  
   }  
        
   public String getSex(){  
      return sex;  
   }  
  
   public void insertData(){  
     
      try {  
         String query = "insert into person(name,age,sex) values(\""+name+"\","+age+",\""+sex+"\")";  
         stm.executeUpdate(query);  
  
      }catch (SQLException e){System.out.println("Error in inserting:" + e.getMessage());}  
        
   }  
  
   public boolean changeData(){  
        
      boolean test = false;  
     
      try {  
         String query = "update person "+  
                     "set age = " + age + ", "+  
                     "sex = \"" + sex + "\" " +  
                     "where name = \"" + name + "\"";  
  
         int lines = stm.executeUpdate(query);  
           
         if (lines > 0)  
            test = true;  
         else  
            test = false;  
  
      }catch (SQLException e){System.out.println("Error in inserting:" + e.getMessage());}  
        
      return test;  
   }  
     
   public boolean deleteData(){  
     
    boolean test = false;  
     
      try {  
         String query = "delete from person where name='" + name+"'";  
         int lines = stm.executeUpdate(query);  
           
         if (lines > 0)  
            test = true;  
         else  
            test = false;  
  
      }catch (SQLException e){System.out.println("Error exclusion:" + e.getMessage());}  
        
      return test;  
   }  
     
   public boolean searchData(){  
        
      boolean test = false;  
           
      try {  
         String query = "select * from person where name='" + name+"'";  
         res = stm.executeQuery(query);  
           
         if (res.next()){test = true;}  
         else{test = false;}  
           
      }catch (SQLException e){System.out.println("Error in inserting:" + e.getMessage());}  
        
      return test;  
   }     
  
   public void setSearch() {  
        
      try {  
         res = stm.executeQuery("select * from person");  
      }   
      catch (SQLException e){  
         e.printStackTrace();  
      }  
  
   }  
  
   public ResultSet getResult() {  
      return res;  
   }  
}  

Where the constructor is responsible for connection to the database, that is, every time it is instantiated an object of this class, the constructor is called first and the database will be connected. Were created three private variables name, age and sex, which will be accessed through the get and set methods.

The insertData() method is responsible for entering the database, which will be passed as the three private variables values. The changeData() method is responsible for updating the data taking into account the variable name, testing if any records were found, thus returning true or false. The deleteData() method, as the changeData() deletes the data taking into account the variable name, testing found any record. The searchData() method only query the database, also testing found any record. The setSearch() method performs a common query. And getResult() method returns the ResultSet object containing all the records of the database.

Person.jsp

Now we will create a main JSP file containing the fields and buttons to insert, query, modification and deletion. We call the person.jsp.

Listing 8. person.jsp

<%@ page contentType="text/html"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  
<html>  
<head>  
<title> Person Table</title>  
  
  
<script>  
  
function send(pair){  
     
   if (par == 'register'){  
     
      if (document.register.nameField.value == ''){  
         document.register.statusField.value = 'Fill in the name field';  
      }else if (document.register.ageField.value == ''){  
         document.register.statusField.value = ' Fill in the age field';  
      }else if (document.register.sexField.value == ''){  
         document.register.statusField.value = ' Choose the sex of the person';  
      }else {  
         document.register.action='register.jsp';  
         document.register.submit();  
      }  
  
   } else if (pair == 'search'){  
  
      if (document.register.nameField.value == ''){  
         document.register.statusField.value = 'Fill in the name field';  
      }else {  
         document.register.action='search.jsp';  
         document.register.submit();  
      }  
  
   } else if (par == 'change'){  
  
      if (document.register.nameField.value == ''){  
         document.register.statusField.value = 'Fill in the name field';  
      } else if (document.register.ageField.value == ''){  
         document.register.statusField.value = ' Fill in the age field';  
      }else if (document.register.sexField.value == ''){  
         document.register.statusField.value = 'Choose the sex of the person';  
      }else {  
         document.register.action='search.jsp';  
         document.register.submit();  
      }  
   } else if (pair == 'delete'){  
  
      if (document.register.nameField.value == ''){  
         document.register.statusField.value = 'Fill in the name field';  
      }else {  
         document.register.action='delete.jsp';  
         document.register.submit();  
      }  
   } else if (pair == 'show'){  
      document.register.action='show.jsp';  
      document.register.submit();  
   }  
}  
  
</script>  
  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
<style type="text/css">  
<!--  
.style3 {   font-size: 28px;  
   font-weight: bold;  
}  
-->  
</style>  
</head>  
  
<%  
String name = request.getParameter("name");  
String age = request.getParameter("age");  
String sex = request.getParameter("sex");  
String status = request.getParameter("status");  
%>  
  
<body>  
<form name="register" method="get">  
  
<table width="472" border="0">  
  <tr>  
    <td width="458" height="70" bgcolor="#6699FF"><p align="left" class="style3"> Register Person </p>  
    </td>  
  </tr>  
  <tr>  
    <td height="177" bgcolor="#6699FF"><p> <strong>Name :</strong>  
       <input name="nameField" type="text" id="nameField" size="46" value="<%=(name==null)?"":name%>"> </p>  
         <p> <strong>Age :</strong>  
       <input name="ageField" type="text" id="ageField" size="10" value="<%=(age==null)?"":age%>">  
               <strong>Sex:</strong>   
  
        <input name="sexField" type="text" id="sexField" size="6" value="<%=(sex==null)?"":sex%>">  
          
  
         </p>      
   </td>  
  </tr>  
  <tr>  
    <td height="101" bgcolor="#6699FF"><p>   
        <input type="button" onClick="send('register')" name="bt1" value="register">  
        <input type="button" onClick="send('search')" name="bt2" value="search">  
        <input type="button" onClick="send('change')" name="bt3" value="change">  
        <input type="button" onClick="send('delete')" name="bt4" value="delete">  
        <input type="button" onClick="send('show')" name="bt5" value="show">  
      </p>  
      <p> <strong>Status:  
   <input name="statusField" disabled type="text" id="statusField" size="46" value="<%=(status==null)?"":status%>">  
      </strong></p>  
   </td>  
  </tr>  
</table>  
</form>  
</body>  
</html>  

This page is a simple screen with some buttons and textfields. The buttons use the onclick method to call the function send(pair) written in javascript, where according to each button pressed, is called a different page, and also took the opportunity to test whether the fields are empty according to each request.

Note that there is a JSP snippet <% String name = request.getParameter("name"); %>, Which will be used to retrieve the parameters of other pages where they will be best visible later on.

Register.jsp

When you press the register button, it is necessary that all person.jsp in fields are filled, then it is called the register.jsp page.

Listing 9. register.jsp

<%@ page contentType="text/html" language="java" import="java.sql.*"%>  
<jsp:useBean id="con" class="connection2.Connection"/>  
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<title>Register</title>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
</head>  
  
<body>  
<form name="form1" method="post" action="person.jsp">  
  
<p><strong>Person Table </strong></p>  
<table border=1 cellspacing=0 cellpadding=0 >  
  
   <tr>  
    <td width=200><strong>  
      Name  
   </strong></td>      
   <td width=50><strong>  
      Age 
   </strong></td>      
   <td width=30><strong>  
      Sex  
   </strong></td>  
   </tr>  
  
  
<%      
   try {  
  
      con.setName(request.getParameter("nameField"));  
      con.setAge(Integer.parseInt(request.getParameter("ageField")));  
      con.setSex(request.getParameter("sexField"));  
      con.insertData();  
                 
      con.setSearch();  
      ResultSet temp = con.getResult ();  
      temp.next();  
        
      if(request.getParameter("nameField").equals(temp.getString("name")))  
         response.sendRedirect("http://localhost:8080/test/person.jsp?status= Register existing");  
        
      do{  
      %>  
      <tr>  
          <td width=200>  
               <%out.print(temp.getString("name"));%>  
         </td>  
          <td width=50>  
               <%out.print(temp.getString("age"));%>  
         </td>  
          <td width=30>  
               <%out.print(temp.getString("sex"));%>  
         </td>  
      </tr>  
      <%}while (temp.next());  
     
   }catch (Exception e) {  
      e.printStackTrace();  
   }  
     
%>  
</table>  
  <p> </p>  
  <p>  
    <input type="submit" name="Submit" value="back">  
</p>    
<p><strong>Status:  
    <input name="statusField" disabled type="text" id="statusField" size="36" value="<%=request.getParameter("nomeField")%> it was successfully registered ">  
      </strong></p>  
</form>  
</body>  
</html>  

Here is retrieved the contents of person.jsp fields, already assigning those values to variables JavaBeans and then is called the insertData() method, which will be responsible for making the insertion in the bank with the variables assigned above.

After a test is made if the name variable is equal to any in the bank, if so returns to person.jsp page with the status variable that will be used in person.jsp to display the message “Register existing” in the field statusField.

If not equal to none, then calls the getResult() method and assigns the variable temp ResultSet type, which will contain all the database records and through temp.getString() method shown in a table all the records of the bank, and can view the registration made. On this page there is also a button to return to the main page and a field to show the status of the register. Note that at the beginning of this page are being importing the java.sql package through the code <%import="java.sql.*"%>, because it will be necessary to create a variable of type ResultSet.

Search.jsp

The query page is used to return the person.jsp screen all the data of a record for a subsequent amendment or deletion.

Then when you press the button to consult, will be called the search.jsp page where you will need the name field is filled.

Listing 10. search.jsp

<title>Search</title>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
</head>  
  
<body>  
<form method="get">  
<%  
      con.setName(request.getParameter("nameField"));  
      boolean test = con.searchData();  
                 
      if (testa)  
      {           
         ResultSet temp = con.getResult();  
         response.sendRedirect("http://localhost:8080/test/person.jsp?status=Was successful consultation&name="+  
            temp.getString("name")+"&age="+temp.getString("age")+"&sex="+temp.getString("sex"));  
      }else{  
         response.sendRedirect("http://localhost:8080/test/person.jsp?status=Register not found");  
      }  
           
              
%>  
</form>  
</body>  
</html>  

Here is retrieved the contents of the name field and assigned to the variable name through the setName() method in JavaBeans. So it is called the searchData() method, which is of type boolean, referring to the record that contains the name and check if it exists, returning true or false.

If there is, then it is called the getResult() method to retrieve the registry information and passes them as parameters to person.jsp through the variable name, age, sex and status with the message “Was successful consultation”, by response .sendRedirect to be displayed on the screen person.jsp. If not, will only last the variable status with the message “Register not found”.

Change.jsp

The change.jsp page is usually called after the query made any name, where all fields need to be filled.

Listing 11. change.jsp

<%@ page contentType="text/html" language="java" import="java.sql.*"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<jsp:useBean id="con" class="connection2.Connection"/>  
<html>  
<head>  
<title>Change</title>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
</head>  
  
<body>  
<form name="form1" method="post" action="person.jsp">  
  
<jsp:setProperty name="con" property="name" value="<%=request.getParameter("nameField")%>"/>  
<jsp:setProperty name="con" property="age" value="<%=Integer.parseInt(request.getParameter("idadeField"))%>"/>  
<jsp:setProperty name="con" property="sex" value="<%=request.getParameter("sexField")%>"/>  
<p><strong>Person Table </strong></p>  
<table border=1 cellspacing=0 cellpadding=0 >  
  
   <tr>  
    <td width=200><strong>  
      Name  
   </strong></td>      
   <td width=50><strong>  
      Age  
   </strong></td>      
   <td width=30><strong>  
      Sex 
   </strong></td>  
   </tr>  
  
  
<%      
   try {  
  
      boolean test = con.changeData();  
                 
      if (con.changeData())  
      {           
         con.setSearch();  
         ResultSet temp = con.getResult();  
        
         while (temp.next()){  
         %>  
         <tr>  
             <td width=200>  
               <%out.print(temp.getString("name"));%>  
            </td>  
             <td width=50>  
               <%out.print(temp.getString("age"));%>  
            </td>  
             <td width=30>  
               <%out.print(temp.getString("sex"));%>  
            </td>  
         </tr>  
         <%}  
      }else{  
         response.sendRedirect("http://localhost:8080/test/person.jsp?status=Register not found");  
      }     
   }catch (Exception e) {  
      e.printStackTrace();  
   }  
     
%>  
</table>  
  <p>  
    <input type="submit" name="Submit" value="back">  
</p>    
<p><strong>Status:  
    <input name="statusField" disabled type="text" id="statusField" size="36" value="<%=request.getParameter("nameField")%> it was changed successfully">  
      </strong></p>  
</form>  
</body>  
</html>  

Just as in register.jsp here are retrieved person.jsp all fields and their values assigned to variables JavaBeans then called changeData() method, which is a Boolean method that makes changes to the database and returns true or false.

If returns true, the change was made successfully and then is shown in a table all the records of the bank, can view the changed record. If returns false, it will be called the page person.jsp passing the variable status to show in statusField field person.jsp the message “Register not found”.

Delete.jsp

This page is also called after a query and will require all the fields are filled.

Listing 12. delete.jsp

<%@ page contentType="text/html" language="java" import="java.sql.*"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<jsp:useBean id="con" class="connection2.Connection"/>  
<html>  
<head>  
<title>Excluir</title>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
</head>  
  
<body>  
<form name="form1" method="post" action="person.jsp">  
  
<p><strong>Person Table </strong></p>  
<table border=1 cellspacing=0 cellpadding=0 >  
  
   <tr>  
    <td width=200><strong>  
      Name  
   </strong></td>      
   <td width=50><strong>  
      Age  
   </strong></td>      
   <td width=30><strong>  
      Sex  
   </strong></td>  
   </tr>  
  
  
<%      
   try {  
  
      con.setNome(request.getParameter("nomeField"));  
  
      if (con.deleteData())  
      {  
         con.setSearch();  
         ResultSet temp = con.getResult();  
        
         while (temp.next()){  
         %>  
            <tr>  
                <td width=200>  
                  <%out.print(temp.getString("name"));%>  
               </td>  
                <td width=50>  
                  <%out.print(temp.getString("age"));%>  
               </td>  
                <td width=30>  
                  <%out.print(temp.getString("sex"));%>  
               </td>  
            </tr>  
         <%}  
      }  
      else{  
         response.sendRedirect("http://localhost:8080/test/person.jsp?status=Register not found");  
      }     
   }catch (Exception e) {  
      e.printStackTrace();  
   }  
  
           
%>  
</table>  
  <p> </p>  
  <p>  
    <input type="submit" name="Submit" value="back">  
    </p>  
  <p><strong>Status:  
    <input name="statusField" disabled type="text" id="statusField" size="36" value="<%=request.getParameter("nameField")%> has been deleted successfully">  
      </strong></p>  
</form>  
</body>  
</html>  

The functionality of this page is identical to change.jsp page, changing only that is invoked deleteData() method instead of changeData().

If deletion is successfully executed, the same way the data will be shown in a table being able to see the absence of excluded record.

Show.jsp

When you press the button list, is called the show.jsp page.

Listing 13. show.jsp

<%@ page contentType="text/html" language="java" import="java.sql.*"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<jsp:useBean id="con" class="connection2.Connection"/>  
<html>  
<head>  
<title>Untitled Document</title>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
</head>  
  
<body>  
<form name="form1" method="post" action="person.jsp">  
  
<p><strong>Person Table </strong></p>  
<table border=1 cellspacing=0 cellpadding=0 >  
  
   <tr>  
    <td width=200><strong>  
      Name  
   </strong></td>      
   <td width=50><strong>  
      Age  
   </strong></td>      
   <td width=30><strong>  
      Sex  
   </strong></td>  
   </tr>  
  
  
<%      
   try {  
      con.setSearch();  
      ResultSet temp = con.getResult();  
        
      while (temp.next()){  
      %>  
      <tr>  
          <td width=200>  
               <%out.print(temp.getString("name"));%>  
         </td>  
          <td width=50>  
               <%out.print(temp.getString("age"));%>  
         </td>  
          <td width=30>  
               <%out.print(temp.getString("sex"));%>  
         </td>  
      </tr>  
      <%}  
     
   }catch (Exception e) {  
      e.printStackTrace();  
   }  
     
%>  
</table>  
  <p>  
    <input type="submit" name="Submit" value="back">  
</p>  
</form>  
</body>  
</html>  
 

Where its unique feature is to show in a table all record of the bank at the time, for a possible check of the data. Once this is done, our application is finished and ready to run, and you are prepared to create and practice more advanced examples.



Julio is a System analyst and enthusiast of Information Technology. He is currently a developer at iFactory Solutions company, working in the development of strategic systems, is also a JAVA instructor. He has knowledge and experi...

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login