Hi
package javacode;
import java.io.*;
import javax.servlet.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Enumeration;
import javax.servlet.http.*;
import javax.sql.*;
import javax.net.*;
public class ConnectToDatabase extends HttpServlet {
String url = "jdbc:
mysql://192.168.10.211:3306/";;
String db = "amar";
String driver = "com.mysql.jdbc.Driver";
String userName ="amar";
String password="amar123";
Connection con = null;
private ServletConfig config;
public void init(ServletConfig config) throws ServletException{
this.config=config;
}
public void service (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HttpSession session = req.getSession(true);
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><HEAD><TITLE>Display list From Database.</TITLE>");
out.println("</HEAD>");
out.println("<BODY bgColor=blanchedalmond text=#008000 topMargin=0>");
out.println("<P align=center><FONT face=Helvetica><FONT color=fuchsia style=\"BACKGROUND-COLOR: white\"><BIG><BIG>List of Student details.</BIG></BIG></FONT></P>");
out.println("<P align=center>");
out.println("<TABLE align=center border=1 cellPadding=1 cellSpacing=1 width=\"75%\">");
out.println("<tr>");
out.println("<td>Id</td>");
out.println("<td>Course Name</td>");
out.println("<td>Course Fee</td>");
out.println("<td>Faculty Name</td></tr>");
try{
Class.forName(driver);
con=DriverManager.getConnection(url+db,userName,password);
Statement st = con.createStatement();
String query = "select * from student";
ResultSet theResult = st.executeQuery(query);
while(theResult.next()){
out.println();
out.println("<tr>");
out.println("<td>" + theResult.getInt(1) + "</td>");
out.println("<td>" + theResult.getString(2) + "</td>");
out.println("<td>" + theResult.getString(3) + "</td>");
out.println("<td>" + theResult.getString(4) + "</td>");
out.println("</tr>");
}
theResult.close();//Close the result set
st.close();//Close statement
con.close(); //Close database Connection
}
catch(Exception e){
out.println(e.getMessage());//Print trapped error.
}
out.println("</TABLE></P>");
out.println("<P> </P></FONT></BODY></HTML>");
}
public void destroy(){
}
}