如何显示数据库中的搜索记录

时间:2022-08-16 19:30:16

i want to search record from database in jsp using id. if id exists in database then display edit.jsp otherwise display any error message like record not found in db.

我想使用id在jsp中搜索数据库中的记录。如果id存在于数据库中,则显示edit.jsp,否则显示任何错误消息,如db中未找到的记录。

Thanks

2 个解决方案

#1


0  

I guess it would be better if you use a servlet too . else you can go with the JSP itself , but you need to use scriplets , which is considered as a bad practice refer here.

我想如果你也使用servlet会更好。否则你可以使用JSP本身,但是你需要使用scriplets,这被认为是一种不好的做法。

In your servlet, do something like this ,

在你的servlet中,做这样的事情,

public void doPost(HttpServletRequest request, HttpServletResponse response)
{
  1. get the parameters using

    获取参数使用

    request.getParameter

  2. Connect to DB, Select id from table , iterate and if Id exists return a boolean variable with value true else return it as false

    连接到DB,从表中选择id,迭代,如果Id存在,则返回值为true的布尔变量,否则返回false

  3. Check the value of the boolean variable if true , redirect it to edit.jsp else display an error msg

    如果为true,则检查布尔变量的值,将其重定向到edit.jsp否则显示错误消息

Note: For redirecting either use sendRedirect or RequestDispatcher . Refer this links could be more useful

注意:对于重定向,请使用sendRedirect或RequestDispatcher。请参阅此链接可能更有用

Creating simple JSP app

创建简单的JSP应用

Start from here

从这里开始

Hope it Helps !!

希望能帮助到你 !!

#2


0  

Try this : Selecting data from DB using MYSQL:

试试这个:使用MYSQL从DB中选择数据:

<%
    Connection conn = null; 
    PreparedStatement ps1 = null;
    ResultSet rs1 = null; 

    try {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/<DATABASE_NAME>?user=<USER NAME>&password=<PASSWORD>");

          if(!conn.isClosed())
          out.println("MySql Connection Success..");

          ps1 = conn.prepareStatement("SELECT *, count(*) as totalRecords FROM TABLE_NAME WHERE id = 1");
          rs1 = ps1.executeQuery();

          while (rs1.next()) {
             if(rs1.getInt("totalRecords") > 0) {
                 out.println("Record Found");
             } else {
                 out.println("Record Not Found");
             }
          }
    } catch (Exception e) {
                out.println("Error:"+e);
    } finally {
         if (rs1!= null) rs1.close(); 
         if (ps1!= null) ps1.close();
         if (conn!= null) conn.close();
    }

%>

#1


0  

I guess it would be better if you use a servlet too . else you can go with the JSP itself , but you need to use scriplets , which is considered as a bad practice refer here.

我想如果你也使用servlet会更好。否则你可以使用JSP本身,但是你需要使用scriplets,这被认为是一种不好的做法。

In your servlet, do something like this ,

在你的servlet中,做这样的事情,

public void doPost(HttpServletRequest request, HttpServletResponse response)
{
  1. get the parameters using

    获取参数使用

    request.getParameter

  2. Connect to DB, Select id from table , iterate and if Id exists return a boolean variable with value true else return it as false

    连接到DB,从表中选择id,迭代,如果Id存在,则返回值为true的布尔变量,否则返回false

  3. Check the value of the boolean variable if true , redirect it to edit.jsp else display an error msg

    如果为true,则检查布尔变量的值,将其重定向到edit.jsp否则显示错误消息

Note: For redirecting either use sendRedirect or RequestDispatcher . Refer this links could be more useful

注意:对于重定向,请使用sendRedirect或RequestDispatcher。请参阅此链接可能更有用

Creating simple JSP app

创建简单的JSP应用

Start from here

从这里开始

Hope it Helps !!

希望能帮助到你 !!

#2


0  

Try this : Selecting data from DB using MYSQL:

试试这个:使用MYSQL从DB中选择数据:

<%
    Connection conn = null; 
    PreparedStatement ps1 = null;
    ResultSet rs1 = null; 

    try {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/<DATABASE_NAME>?user=<USER NAME>&password=<PASSWORD>");

          if(!conn.isClosed())
          out.println("MySql Connection Success..");

          ps1 = conn.prepareStatement("SELECT *, count(*) as totalRecords FROM TABLE_NAME WHERE id = 1");
          rs1 = ps1.executeQuery();

          while (rs1.next()) {
             if(rs1.getInt("totalRecords") > 0) {
                 out.println("Record Found");
             } else {
                 out.println("Record Not Found");
             }
          }
    } catch (Exception e) {
                out.println("Error:"+e);
    } finally {
         if (rs1!= null) rs1.close(); 
         if (ps1!= null) ps1.close();
         if (conn!= null) conn.close();
    }

%>