3. Write a servlet program that accepts a table name and to display all the records in the table.
Prerequisite :
- MySQL Server.
- MySQL-connector-java.jar
- An IDE (Netbeans, eclipse or your favorite one )
- A Database with at least one table in it.
All of them or anyone can be downloaded from the mentioned link or from the official websites.
You will have to create two files namely TableData.html, TableInfo.java (Servlet), and will be required one database table, let's name it students.
TableData.html
You will have to create two files namely TableData.html, TableInfo.java (Servlet), and will be required one database table, let's name it students.
TableData.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!DOCTYPE html> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <form method="get" action="TblInfo"> Enter Table Name : <input type="text" name="tblname"/> <br> <br> <input type="submit" value="Get Data"/> </form> </body> </html> |
TableInfo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author Shubham */ @WebServlet(urlPatterns = {"/TblInfo"}) public class TblInfo extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { out.println("<html>"); out.println("<title>Servlet TblInfo</title>"); out.println("<body>"); Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/information_schema","root",""); Statement st = con.createStatement(); String tblname = request.getParameter("tblname"); ResultSet rs = st.executeQuery("Select * From " + tblname + ";"); ResultSetMetaData rsmd = rs.getMetaData(); int num = rsmd.getColumnCount(); out.println("<table border=3 <tr>"); for(int i=1; i<=num; i++) { out.println("<th>" + rsmd.getColumnName(i) + "</th>"); } out.println("</tr>"); while(rs.next()==true) { out.println("<tr><td>"+ rs.getString(1)+"</td>"); out.println("<td>"+ rs.getString(2)+ "</td>"); out.println("<td>" + rs.getString(3)+ "</td>"); out.println("<td>" + rs.getString(4)+ "</td>"); out.println("<td>" + rs.getString(5)+ "</td></tr>"); } out.println("</table>"); out.println("</body>"); out.println("</html>"); } catch(ClassNotFoundException MySQLSyntaxErrorException) { out.println("<th>Error : Enter Valid Table Name </th>"); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { processRequest(request, response); } catch (SQLException ex) { Logger.getLogger(TblInfo.class.getName()).log(Level.SEVERE, null, ex); } } } |
Create a Student Table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | CREATE TABLE Students ( StudentID int NOT NULL, LastName varchar(255), FirstName varchar(255), Address varchar(255), Optedcourse varchar(255), PRIMARY KEY (StudentID) ); INSERT INTO Students (StudentID, LastName, FirstName, Address, Optedcourse) VALUES ('101', 'Singh', 'Shubham', 'Mumbai', 'Computer Science'); INSERT INTO Students (StudentID, LastName, FirstName, Address, Optedcourse) VALUES ('102', 'Shaikh', 'Salim', 'Dubai', 'Information Technology'); |
Before Running the program please check if you have placed MySQL-connector-java.jar in Libraries folder.
Output
Fig 1. Final Output
No comments:
Post a Comment