One, this example uses JDBC to connect to the MySQL database
Packagecom.test;Importjava.io.IOException;ImportJava.io.PrintWriter;ImportJava.sql.*;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classGetmysqlextendsHttpServlet {/*** Constructor of the object. */ PublicGetmysql () {Super(); } /*** Destruction of the servlet. <br>*/ Public voiddestroy () {Super. Destroy ();//Just puts "destroy" string in log//Put Your code here } /*** The Doget method of the servlet. <br> * * This method was called when a form have its tag value met Hod equals to get. * * @paramrequest the request send by the client to the server *@paramresponse The response send by the server to the client *@throwsservletexception If an error occurred *@throwsIOException If an error occurred*/ Private Static Final Longseruid=1l; //Load Driver Static FinalString jdbc= "Com.mysql.jdbc.Driver"; //the database URL to connect to Static FinalString db_url= "Jdbc:mysql://localhost:3306/test"; //Database user name Static FinalString user= "Dyb"; //Database Password Static FinalString pass= "174372150"; Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {Connection conn=NULL; Statement stmt=NULL; //The format of the displayed dataResponse.setcontenttype ("Text/html;charset=utf-8"); PrintWriter out=Response.getwriter (); Try{class.forname ("Com.mysql.jdbc.Driver"); //Enter the user name password and connection. conn=drivermanager.getconnection (Db_url,user,pass); //used to send SQL statements to the databasestmt=conn.createstatement (); String SQL=NULL; //enter the SQL statement to get the data you wantSql= "Select t.title,t.new_id," + "t.news_type_id,t.new_date," + "D.name from T_news T "+" JOIN t_type_id D on t.news_type_id=d.t_type_id "; //send SQL statement and return resultResultSet rs=stmt.executequery (SQL); //Looping through print results while(Rs.next ()) {intId=rs.getint ("t.news_type_id"); String LR=rs.getstring ("T.title"); String DD=rs.getstring ("T.new_date"); String name=rs.getstring ("D.name"); Out.print ("ID:" +ID); Out.print ("Content:" +LR); Out.print ("Date:" +DD); Out.print ("Name:" +name); Out.println ("</BR>"); } //Close ChannelRs.close (); Conn.close (); Stmt.close (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } /*** The DoPost method of the servlet. <br> * * This method was called when a form have its tag value me Thod equals to post. * * @paramrequest the request send by the client to the server *@paramresponse The response send by the server to the client *@throwsservletexception If an error occurred *@throwsIOException If an error occurred*/ Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/html"); PrintWriter out=Response.getwriter (); Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > "); Out.println ("<HTML>"); Out.println ("); Out.println ("<BODY>"); Out.print ("This is"); Out.print ( This. GetClass ()); Out.println (", using the POST method"); Out.println ("</BODY>"); Out.println ("</HTML>"); Out.flush (); Out.close (); } /*** Initialization of the servlet. <br> * *@throwsServletexception If an error occurs*/ Public voidInit ()throwsservletexception {//Put Your code here }}
Jsp:jdbcmysql Database Connection