Servlet + JDBC + MySQL simple web Exercise 1. Servlet structure 1> constructor2> init (); Initialization --> change the web. the SQL configuration and related connection statements in xml are encapsulated in this function. 3> doGet (); doPost (); encapsulate SQL statements for database operations into this function. 4> destory (); Release related resources, for example, close
Servlet + JDBC + MySQL simple web Exercise 1. Servlet structure 1> constructor 2> init (); Initialization --> change web. the SQL configuration and related connection statements in xml are encapsulated in this function. 3> doGet (); doPost (); encapsulate SQL statements for database operations into this function. 4> destory (); Release related resources, for example, close
Servlet + JDBC + MySQL simple web exercises
1. Servlet Structure
1> constructor
2> init (); Initialization --> encapsulate SQL configuration and connection statements in web. xml into this function.
3> doGet (); doPost (); encapsulate SQL statements for database operations into this function.
4> destory (); Release related resources, such as disabling database statements and encapsulating them into this function.
2. JDBC (oop) + Mysql (db Development)
1> driver package (*. jar)
2> establish a Connection with db.
3> ResultSet (record result set)
4> get the field.
5> release resources.
3. From the Basic Framework-> to the process idea.
Achieve one and two resource integration.
4. Steps.
Requirement: You have created a school database and studentinfo (id, name, age) in MySQL ).
1> Create a webproject (web Application name myweb)
2> Create a package named web in webproject)
3> Create a servlet (MyServlet) in the package)
4> complete 1 conversion (encapsulation)
// Initialize the function to connect to the database
Public void init () throws ServletException {
String url = "jdbc: mysql: // localhost: 3306/school ";
String user = "root ";
String pwd = "root ";
Try {
Class. forName ("com. mysql. jdbc. Driver ");
// Load the Driver. This sentence can also be written as: Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
// Establish a connection to MySQL
Conn = DriverManager. getConnection (url, user, pwd );
} Catch (Exception ex ){
System. out. println ("Error:" + ex. toString ());
}
}
// Define member variables
Connection conn;
ResultSet rs;
// Database operations
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
// Solve Chinese garbled characters
Response. setContentType ("text/html; charset = UTF-8"); // encode the browser as UTF-8 by default.
// Response. setCharacterEncoding ("UTF-8"); // Chinese garbled characters
PrintWriter out = response. getWriter ();
Out. println ("");
Out. println ("");
Out. println ("A Servlet");
Out. println ("");
Try {
// Execute the SQL statement
Statement stmt = conn. createStatement (); // create a Statement object to execute the SQL language
ResultSet rs = stmt.exe cuteQuery ("select * from studentinfo ");
// Process the result set
Out. println ("
");
Out. println ("");
Out. println ("");
Out. println ("");
Out. println ("");
Out. println ("");
Out. println ("");
Out. println ("");
Out. println ("");
While (rs. next ()){
String id = rs. getString ("id ");
String name = rs. getString ("name ");
String age = rs. getString ("age ");
Out. println ("");
Out. println ("");
Out. println ("");
Out. println ("");
Out. println ("");
}
Out. print ("
"); Out. println (" Student info table "); out. println (" |
"); Out. println (" student ID 1 "); out. println (" |
"); Out. println (" name "); out. println (" |
"); Out. println (" Age "); out. println (" |
"); Out. println (id); out. println (" |
"); Out. print (name); out. println (" |
"); Out. println (age); out. println (" |
");
} Catch (Exception ex ){
System. out. println ("Error:" + ex. toString ());
}
Out. println ("");
Out. println ("");
Out. flush ();
Out. close ();
}
// Release resources
Public void destroy (){
Super. destroy ();
Try {
Rs. close (); // close the database
Conn. close ();
} Catch (Exception ex ){
System. out. println ("Error:" + ex. toString ());
}
Automatically Generated package
Package web;
Package imported after modification (the system automatically imports the package during programming)
Import java. io .*;
Import java. SQL .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
The complete code is as follows:
Package web;
Import java. io .*;
Import java. SQL .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Public class Myservlet extends HttpServlet {
// Define the member variable Connection conn; ResultSet rs; public Myservlet () {super () ;}// initialize the function to connect to the database public void init () throws ServletException {String url = "jdbc: mysql: // localhost: 3306/school"; String user = "root"; String pwd = "root"; try {Class. forName ("com. mysql. jdbc. driver "); // load the Driver, which can also be written as: Class. forName ("com. mysql. jdbc. driver "). newInstance (); // connection established to MySQL conn = DriverManager. getConnection (url, user, pwd);} catch (Exception ex) {System. out. println ("Error:" + ex. toString () ;}}// public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// solve the Chinese garbled problem response. setContentType ("text/html; charset = UTF-8"); // use the default encoding of the browser to UTF-8 // response. setCharacterEncoding ("UTF-8"); // It can also solve Chinese garbled characters PrintWriter out = response. getWriter (); out. println (""); Out. println (""); Out. println ("A Servlet"); Out. println (""); Try {// execute the SQL Statement stmt = conn. createStatement (); // create a statement object to execute the SQL language ResultSet rs = stmt.exe cuteQuery ("select * from studentinfo"); // process the result set out. println ("
"); Out. println ("
"); Out. println ("
"); Out. println (" student information table "); out. println (" | "); Out. println ("
"); Out. println ("
"); Out. println ("
"); Out. println (" student ID 1 "); out. println (" | "); Out. println ("
"); Out. println (" name "); out. println (" | "); Out. println ("
"); Out. println (" Age "); out. println (" | "); Out. println ("
"); While (rs. next () {String id = rs. getString ("id"); String name = rs. getString ("name"); String age = rs. getString ("age"); out. println ("
"); Out. println ("
"); Out. println (id); out. println (" | "); Out. println ("
"); Out. print (name); out. println (" | "); Out. println ("
"); Out. println (age); out. println (" | "); Out. println ("
");} Out. print ("
");} Catch (Exception ex) {System. out. println (" Error: "+ ex. toString ();} out. println (""); Out. println (""); Out. flush (); out. close () ;}// release the public void destroy () {super. destroy (); try {rs. close (); // close the database conn. close ();} catch (Exception ex) {System. out. println ("Error:" + ex. toString ());}}
}
6> BuildPach --> AddExternalArchives...
Import the downloaded database connection package (mysql-connector-java.jar)
7> after saving, publish the web application to the configured tomcat.
8> start tomcat.
9> in the URL bar of the browser, enter http: // localhost: 8080/web Application name/servlet name
For example, http: // localhost: 8080/myweb/servlet/Myservlet
10> the student information table is displayed in the browser.
11> restart tomcat and re-release it to tomcat every time you debug the program.