Question: how to fetch data from servlet ????

Ads

 
 
 

Share on Google+Share on Google+

preety
how to fetch data from servlet ????
3 Answer(s)      3 years and 11 months ago
Posted in : JDBC

how to fetch data from servlet ????

Ads
View Answers

May 22, 2013 at 10:36 AM


hi friend,

Please explain what do you want to do exactly. Specify, from where you want to get data using servlet for example, whether from an html form, database, or from else where.

  1. getParameter(String name): A simple method of HttpServletRequest to get the value from an html form.

  2. How to get data from database : To get data from database you would have to first make a connection with database. To know more in detail how to get data from database using servlet you may go through the following links below, may this will be helpful for you

http://www.roseindia.net/jdbc/jdbcconnectivity.shtml

http://www.roseindia.net/servlets/databaseconnectionservlet.shtml

Thanks.



May 22, 2013 at 11:48 AM


view.jsp

<[email protected] import="java.sql.Statement"%> 
<[email protected] import="java.sql.DriverManager"%> 
<[email protected] import="java.sql.Connection"%>
<[email protected] import="java.sql.ResultSet"%> 
<[email protected] contentType="text/html" pageEncoding="UTF-8"%> 

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>View</title>
    </head>
    <body>
      <center> 


}
          <h1>Welcome Madhab</h1>
          <form action="NewServlet" method="POST">
              <input type="submit" value="Click!">
          <table border="2">
  <tr>
    <th>S.No</th>
    <th>State</th>
    <th>Capital</th>
  </tr>
  <tr>
      <% try {ResultSet rs = (ResultSet) request.getAttribute("data"); 
     while (rs.next()) {%>             

  <td><input type="text" value="<%=rs.getString("state_id")%>"</td> 
<td><input type="text" value="<%=rs.getString("state_name")%>"</td>
<td><input type="text" value="<%=rs.getString("capital")%>"</td>
</tr> 
<% }} 
catch (Exception e) { 
                  } %>        
          </table> 
      </form>
</center> 
</body>


May 22, 2013 at 11:49 AM


view.serverlet

 try {       
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/state_capital_db", "root", "root");

        String query = "select * from state_details"; 
        Statement s = null; 
        s = (Statement) con.createStatement(); 
        ResultSet rs = null;  
        rs = s.executeQuery(query);  
        request.setAttribute("data", rs);  
        RequestDispatcher rd = request.getRequestDispatcher("view.jsp");  
        rd.forward(request, response);  

    } catch (Exception e) {       
        out.print(e.toString());   
    }
    finally {       
        out.close();
    }
    }
    }

Ads









Related Tutorials/Questions & Answers:
Tutorials   
Java Spring Hibernate Struts Training how to retrieve text and images from mysql database and show on html page using jsp servlet How to install Neo4j on Ubuntu 15.10? please help me in these prog MYSQL - mysql copy table to another table example by creating new table solution for mapping hibernate and pojos from net beans through database Connect GWT application with Oracle inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store inventory System of Medical Store This code have created jTable that connected with a database.every cell of last column have a button.But this button doesn't work properly.Where is the error java program Java Database Program jdbc Database connection class in a live project ..?? What is JDBC architecture? Unable to Create DSN for excel to ms access importing data and edit that data Database Scrolling Buttons how to use foreign key in java How to delete content of a log file from linux terminal? JSP-Oracle connectivity Student Management System Java code for Saving Marathi (Indian local language ) in Mysql and retrieving the same crud operations with foreign key constraint. I really need help with this assignment question Please help me out Please Backup Failed Mysqldump Got Errno 28 On Write Upgrade MySQL 5.1 to 5.5 in cpanel mysqldump all databases com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'jpa.sequence' doesn't exist persistence xml class element persistence.xml for MySQL MySQL JDBC Driver pom dependency code Blob column jdk6 and oracle 8i connexion oracle 8i et jdk6 java [ cannot retrive date from sql ] why?? Order Entry System How to threow timeout exception when DB is not responding How to threow timeout exception when DB is not responding This is the employee form to add data to mysql database but I got error of NullPointerException, Is this write code please give me feedback as soon as possible make a method which takes user name & pwd as parameter & validate them against the database. If the corresponding user exist return true otherwise false. prabhakar validating credentials and displaying error message in login .jsp if not valid JDBC jdbc jdbc code register servlet vibhu connectiondetails from vibhu

Ads

 
Advertisement null

Ads