Implement JDBC-Eclipse & Mysql & Servlet and configure jdbc in eclipse
Import java. io. IOException;
Import java. io. PrintWriter;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;
Import javax. servlet. ServletException;
Import javax. servlet. annotation. WebServlet;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class Jdbc_Servlet extends HttpServlet {
Private static final long serialVersionUID = 1L;
Public Jdbc_Servlet (){
Super ();
}
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String driver = "com. mysql. jdbc. Driver ";
String url = "jdbc: mysql: // localhost: 3306/student? User = root & password = & characterEncoding = UTF-8 ";
Response. setContentType ("text/html; charset = UTF-8 ");
Request. setCharacterEncoding ("UTF-8 ");
PrintWriter out = response. getWriter ();
Out. print ("Out. print ("Out. print ("<body> ");
Try {
Class. forName (driver );
Connection conn = DriverManager. getConnection (url );
Statement stmt = conn. createStatement ();
ResultSet rs = stmt.exe cuteQuery ("select * from studentInfo ");
While (rs. next ()){
Out. print ("student ID:" + rs. getInt ("stu_number") + "<br> ");
Out. print ("name:" + rs. getString ("stu_name") + "<br> ");
Out. print ("Gender:" + rs. getString ("stu_gender") + "<br> ");
Out. print ("Age:" + rs. getInt ("stu_age") + "<br> ");
Out. print ("Address:" + rs. getString ("stu_address") + "<br> ");
}
If (rs! = Null ){
Rs. close ();
}
If (stmt! = Null ){
Stmt. close ();
}
If (conn! = Null ){
Conn. close ();
}
}
Catch (ClassNotFoundException ex ){
Ex. printStackTrace ();
}
Catch (SQLException ex ){
Ex. printStackTrace ();
}
Out. print ("</body> ");
Out. print ("
}
Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DoGet (request, response );
}
}