html文件调用servlet连接mysql数据库实例

 web.xml文件

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>FootballTeam</servlet-name> <servlet-class>FindFootOrPk</servlet-class> </servlet> <servlet-mapping> <servlet-name>FootballTeam</servlet-name> <url-pattern>/foot</url-pattern> </servlet-mapping> </web-app>

 

 

html文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>html调用servlet</title></head><body> <h1 align="center">请点击提交按钮</h1> <div align="center" class="footgame"> <form action="http://localhost:8080/ltb6w/foot" name="match" method="post"> <input type="submit" value="提交"> </form> </div> </body></html>

 

 

 

 

 

 

 

 

 

import java.sql.*; import java.io.*; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/FindFootOrPk") public class FindFootOrPk extends HttpServlet { private static final long serialVersionUID = 1L; //JDBC驱动名和数据库URL static final String JDBC_DRIVER="com.mysql.jdbc.Driver"; static final String DB_URL="jdbc:mysql://localhost:3306/dudu";//dudu数据库名称 //数据库用户名和密码 static final String USER="root"; static final String PASS="123456"; /** * @see HttpServlet#HttpServlet() */ public FindFootOrPk() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection conn=null; Statement stmt=null; //设置响应内容类型 response.setContentType("text/html;charset=UTF-8"); PrintWriter out=response.getWriter(); String title="前后端打通,连接数据库,我的世界来啦!"; // String name=request.getParameter("pk"); // String link=request.getParameter("link"); String docType = "<!DOCTYPE html>\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n"); try{ // 注册 JDBC 驱动器 Class.forName("com.mysql.jdbc.Driver"); // 打开一个连接 conn = DriverManager.getConnection(DB_URL,USER,PASS); out.println("数据库连接成功!"); // 执行 SQL 查询 stmt = conn.createStatement();//  String sql; //foot_ball 数据库表名称 sql = "SELECT*FROM foot_ball"; //sql语句,一定要保证正确.最好测试一下,要不html不显示. ResultSet rs = stmt.executeQuery(sql); //sql语句执行的结果 // 展开结果集数据库 while(rs.next()){ // 通过字段检索 int id = rs.getInt("id"); String tream = rs.getString("tream"); String url = rs.getString("url"); // 输出数据 out.println("ID: " + id); out.println(", 球队: " + tream); out.println(", 站点 URL: " + url); out.println("<br />"); } out.println("</body></html>"); // 完成后关闭 rs.close(); stmt.close(); conn.close(); } catch(SQLException se) { // 处理 JDBC 错误 se.printStackTrace(); } catch(Exception e) { // 处理 Class.forName 错误 e.printStackTrace(); }finally{ // 最后是用于关闭资源的块 try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ } try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }



 

相关文章