hi friends m new to
servlet programming. am using eclipse
IDE
i followed a tutorial to fetch data from mysql. nothing error is showing.
It shows like server is working fine.But result is not displayed. Please help me out.
servlet coding
public class Databaseaccess extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
//JDBC driver name and database URL
final
String DB_URL="jdbc:mysql://localhost/dblogin";
java.sql.Connection conn = null;
Statement stmt = null;
// Database credentials
final String USER = "root";
final String PASS = "root";
//Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Database Result";
String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " +"transitional//en\">\n";
out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n");
try{
// Register
JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
conn = DriverManager.getConnection(DB_URL,USER,PASS);
// Execute SQL query
stmt = conn.createStatement();
String sql;
sql = "SELECT username,name FROM usertable";
ResultSet rs = stmt.executeQuery(sql);
// Extract data from result set
while(rs.next()){
//Retrieve by column name
String uname = rs.getString("username");
String name = rs.getString("name");
//Display values
out.println(", First: " + uname + "<br>");
out.println(", Last: " + name + "<br>");
}
out.println("</body></html>");
// Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
} //end try
}
}