Data Search from Database using Parameters selected.

Task Description. 

create a simple web page shown as above, 

There are three choices that we can choose from as Search criteria. 

1. First name. 

2. Last Name, 

3. All Data

 based on the Search type and Input value, 

the page should return the data selected from Database. 

For Example 

with First_name as "Tom", the database returns two records. 

The search on the Web Page shown below returns the same records as that in the Database. 

  

Detail Codes. 

1. Search_With_Parameter.jsp

This page will allow us to choose the search type and type in the value, and submit the request, 

the request will go to "NewFile.jsp"

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    <form name = "searchForm" method = "post" action = "NewFile.jsp">
        <table>
            <tr>
                <td>First Name</td>
            
                <td>Last Name </td>    
            </tr>
            <tr>
                <td>Search_Type
                
                <select name="subsql">
                    <option value = "first_name" > First Name</option>
                    <option value ="last_name" > Last Name</option>
                    <option value ="all">  All Data </option>
                </select>
                <input type ="text" name = "subsqlValue" size ="20"> 
                <input type ="submit" name = "searchpart" value = "Search" onclick = "return check()">
                </td>        
            </tr>
        </table>
    </form>
</body>
</html>

2. NewFile.jsp

in this jsp, 

the request is being handled by the class NewsDao and the method GetParts(String subsql, SubString subsqlValue) defined in it. 

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    
<%@ page import= "src.util.*"%>
<%@ page import= "java.sql.*"%>

<%@ page import = "com.yang.Bean.actor" %>
<%@ page import = "com.yang.dao.NewsDao" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%    ResultSet rs=null;
    String subsql =request.getParameter("subsql");
    String subsqlValue = request.getParameter("subsqlValue");
    
    System.out.println(subsql);
    System.out.println(subsqlValue);

%>    
<table>
    <tr>
        <td>first Name</td>
    
        <td> Last Name</td>
        
    </tr>
        <% 
             
            NewsDao my = new NewsDao();
              
             rs = my.GetParts(subsql,subsqlValue); 
               
                while(rs !=null &&rs.next()) {
        %>        
            
            <tr>
                <td><%=rs.getString("first_name") %> </td>
                <td><%=rs.getString("last_name") %> </td>
            </tr>    
        
        <% } 
        %>    
    
</table>        
     
</body>
</html>

3. NewsDao Class

package com.yang.dao;
//Step 1, import the needed packages
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Date;
import java.util.Properties;

import com.yang.Bean.actor;

import src.util.ConfigManager;
/*USE mysql;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '831015';
FLUSH PRIVILEGES;

*/

public class NewsDao {
    
 // JDBC driver name and database URL
      String JDBC_Driver = null;
      String url = null;
      String UserName = null;
      String Password = null;
      Connection conn = null;
      PreparedStatement stmt = null;
      
  public ResultSet GetParts(String subsql, String subsqlValue) {
        
        if (subsql ==null)
            subsql = "";
        if(subsqlValue ==null)
            subsqlValue ="";
        
        String SQL = "select * from actor where " + subsql + "= '" +subsqlValue+"'";
        
        System.out.println(SQL);
        this.getConnection();
        
        try {
            
            stmt = conn.prepareStatement(SQL);
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return rs;
        
    }
    
    public void getConnection()  {
        
        ConfigManager m = new ConfigManager();
        JDBC_Driver = m.getString("jdbc.driver");
        url = m.getString("jdbc.connection.url");
        UserName = m.getString("jdbc.connection.username");
        Password = m.getString("jdbc.connection.password");
    
        try {
            
            //System.out.println("Connecting to Database...");
            Class.forName(JDBC_Driver);
            
            conn = DriverManager.getConnection(url, UserName, Password);
            
            //System.out.println("Connected to Database...");
        }
        catch(SQLException se){
              //Handle errors for JDBC
              se.printStackTrace();
           }catch(Exception e){
              //Handle errors for Class.forName
              e.printStackTrace();
           }    
     }
    
}

猜你喜欢

转载自www.cnblogs.com/codingyangmao/p/10947351.html
今日推荐