How to retrieve all tables from database dynamically using servlets and jsp...please help me

Recommended Answers

All 4 Replies

two points:
you may be a bit more specific (about what it is you want and what it is you've tried so far)
you may also consider posting this question in the jsp forum.

JSP PROGRAM

<%@page language="java" %>
<%@page import="java.sql.*;" %>


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td>table name</td>
<td>${display.tablename}</td>
</tr>
</table>
</body>
</html>

SERVLET PROGRAM

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DisplayTable
 */
@WebServlet("/DisplayTable")
public class DisplayTable extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public DisplayTable() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        Connection conn;
        PreparedStatement ps;
        ResultSet rs;
        String tablename=request.getParameter("tname");
        try{

            Class.forName("org.postgresql.Driver");
            System.out.println("Connecting to selected database....");
            conn=DriverManager.getConnection("jdbc:postgresql://172.16.5.222:5432/TESTDB","infotech","infotech");
            System.out.println("Connected to database Successfully");

            String query="select * from ?";
            ps=conn.prepareStatement(query);

            ps.setString(1, tablename);


            rs=ps.executeQuery();
            while(rs.next())
            {
                Display display=new Display (rs);   

            display.setTablename(rs.getString("tablename"));


             request.setAttribute("display",display);
             RequestDispatcher view = request.getRequestDispatcher("display.jsp");
             view.forward(request, response);
            }
            rs.close();
            ps.close();
            conn.close();

            }catch(Exception e){

        }

    }
}

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */

JAVA CLASS

import java.sql.ResultSet;
import java.sql.SQLException;


public class Display {
    private String tablename;

    public String getTablename() {
        return tablename;
    }

    public void setTablename(String tablename) {
        this.tablename = tablename;
    }

   public Display(ResultSet resultset) throws SQLException
   {
       this.tablename=resultset.getString("tablename");
   }
}

HTML CODE

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="display" method="get">
Table name<input type="text">
<input type="submit" value="GetDetails">
</form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>

<servlet-name>DisplayTable</servlet-name>
<servlet-class>DisplayTable</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayTable</servlet-name>
<url-pattern>/display</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

/////I am not getting output..please try to help me

It is not retrieving any table..please help me

you write code like this:

catch(Exception e){
        }

no wonder you have no idea what's going wrong. if an exception is thrown, you merely hide it. print the stacktraces and check your logs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.