TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
View Data Table in Java
Pintoo Yadav
Dec 04
2015
Code
1.6
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
jsp Page
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package servlet;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import logic.logic;
/**
*
* @author Pintoo
*/
public
class
dummy extends HttpServlet
{
logic logic =
new
logic();
@Override
protected
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String name = request.getParameter(
"name"
);
String pass = request.getParameter(
"pass"
);
String value = request.getParameter(
"submit"
);
// System.out.println(name);
if
(value.equals(
"Submit"
))
{
logic.insert(name, pass);
}
else
if
(value.equals(
"Delete"
))
{
logic.Delete(name);
}
else
if
(value.equals(
"Select"
))
{
List < HashMap > datarow = logic.select();
request.setAttribute(
"resultset"
, datarow);
RequestDispatcher rd = request.getRequestDispatcher(
"/getdata.jsp"
);
rd.forward(request, response);
}
}
@Override
protected
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
Logic Class
public
List < HashMap > select()
{
List < HashMap > datarow =
new
ArrayList < > ();
try
{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
HashMap < String, String > data =
null
;
//JOptionPane.showMessageDialog(this, "Saved");
rs = stmt.executeQuery(
"select * from Texttbl"
);
while
(rs.next())
{
data =
new
HashMap < > ();
data.put(
"name"
, rs.getString(
"name"
));
data.put(
"password"
, rs.getString(
"pass"
));
datarow.add(data);
}
}
catch
(Exception e)
{
e.printStackTrace();
System.
out
.println(
"Error connecting databsse"
+ e.getStackTrace());
}
finally
{
try
{
if
(conn !=
null
)
{
conn.close();
}
if
(stmt !=
null
)
{
stmt.close();
}
}
catch
(Exception e)
{
System.
out
.println(e.getStackTrace());
}
}
return
datarow;
}
Servlet
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package servlet;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import logic.logic;
/**
*
* @author Pintoo
*/
public
class
dummy extends HttpServlet
{
logic logic =
new
logic();
@Override
protected
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String name = request.getParameter(
"name"
);
String pass = request.getParameter(
"pass"
);
String value = request.getParameter(
"submit"
);
// System.out.println(name);
if
(value.equals(
"Submit"
))
{
logic.insert(name, pass);
}
else
if
(value.equals(
"Delete"
))
{
logic.Delete(name);
}
else
if
(value.equals(
"Select"
))
{
List < HashMap > datarow = logic.select();
request.setAttribute(
"resultset"
, datarow);
RequestDispatcher rd = request.getRequestDispatcher(
"/getdata.jsp"
);
rd.forward(request, response);
}
}
@Override
protected
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
java
data table
view data table in java