JDBC-Eclipse & amp; Mysql & amp; Servlet implementation, eclipse configuration jdbc

Source: Internet
Author: User

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 );
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.