Rumah > Java > javaTutorial > teks badan

java_web学习-显示mysql中的数据

巴扎黑
Lepaskan: 2017-06-26 10:41:30
asal
1398 orang telah melayarinya

一.建立数据库

  create database animal;

  create table animal(

  sno int,

  name varchar(20),

  weight varcahr(20),

  color varchar(20),

  birth date

  );

  插入数据

  insert into animal values('1','狮子','200kg','黄色','2014-07-30'),('3','老虎','180kg','黄色','2010-02-14'),('2','猴子','20kg','棕色','2003-04-14'),('6','大象','1000kg','黑色','2009-07-12'),('5','长颈鹿','700kg','黄色','2007-07-13');

  如图:

   

 

二.建立一个 web Project项目

     在lib文件夹下导入五个包:   

    

 

     项目名为:Animal1

     项目里有三个包:entity , action , util

     一个jsp文件:An.jsp

     

ConnManager.java里的代码

 1 package util; 2  3 import java.sql.Connection; 4 import java.sql.DriverManager; 5  6 public class ConnManager { 7  8     //数据库5大参数 9     private static final String IP = "127.0.0.1";10     private static final String PORT = "3306";11     private static final String DATABASE_NAME = "zhz";12     private static final String USER_NAME = "root";13     private static final String PASSWORD = "";14     private static final String DRIVER = "org.gjt.mm.mysql.Driver";15     16     public static Connection getConnection() throws Exception {17         String url = "jdbc:mysql://"+IP+":"+PORT+"/"+DATABASE_NAME+"?user="+USER_NAME+"&password="+PASSWORD+"";18         Class.forName(DRIVER);19         Connection conn = DriverManager.getConnection(url);20         return conn;21     }22 23 }
Salin selepas log masuk

 

Pig.java里的代码

 1 package entity; 2  3 import java.util.Date; 4  5 public class Pig { 6     private Integer id=null; 7     private String name=null; 8     private String weight=null; 9     private String color=null;10     private Date birth=null;11     public Integer getId() {12         return id;13     }14     public void setId(Integer id) {15         this.id = id;16     }17     public String getName() {18         return name;19     }20     public void setName(String name) {21         this.name = name;22     }23     public String getWeight() {24         return weight;25     }26     public void setWeight(String weigth) {27         this.weight = weigth;28     }29     public String getColor() {30         return color;31     }32     public void setColor(String color) {33         this.color = color;34     }35     public Date getBirth() {36         return birth;37     }38     public void setBirth(Date birth) {39         this.birth = birth;40     }41     42 }
Salin selepas log masuk

showPig.java里的代码  

 1 package action; 2  3 import java.io.IOException; 4 import java.sql.Connection; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import javax.servlet.ServletException;11 import javax.servlet.annotation.WebServlet;12 import javax.servlet.http.HttpServlet;13 import javax.servlet.http.HttpServletRequest;14 import javax.servlet.http.HttpServletResponse;15 16 import entity.Pig;17 import util.ConnManager;18 19 @WebServlet("/showPig")20 public class showPig extends HttpServlet {21     private static final long serialVersionUID = 1L;22       23    24     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {25         try {26             Connection conn = ConnManager.getConnection();27             String sql = "select * from animal";28             PreparedStatement ps = conn.prepareStatement(sql);29             //建立一个池,用于存放数据30             List<Pig> pigList = new ArrayList<Pig>();31             ResultSet rs = ps.executeQuery();32             while(rs.next()){34                 Integer id = rs.getInt("id");35                 String name = rs.getString("name");36                 String weight = rs.getString("weight");37                 String color = rs.getString("color");38                 java.sql.Date birth= rs.getDate("birth");39                 Pig s = new Pig();40                 s.setId(id);41                 s.setName(name);42                 s.setWeight(weight);43                 s.setColor(color);44                 s.setBirth(birth);45                 pigList.add(s);46             }47             rs.close();48             ps.close();49             conn.close();50             request.setAttribute("pigList", pigList);51             request.getRequestDispatcher("An.jsp").forward(request, response);52             53         } catch (Exception e) {54             System.out.println("发生异常"+e.getMessage());55         }56     }57 58     59     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {60         61         doGet(request, response);62     }63 64 }
Salin selepas log masuk

 An.jsp里的代码

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%> 4 <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title>10 </head>11 <body>12 <table border = "2px" width = "80%">13     <tr>14         <td>编号</td>15         <td>名字</td>16         <td>体重</td>17         <td>颜色</td>18         <td>入园日期</td>19     </tr>20     <c:forEach var="L" items="${pigList}">21     <tr>22         <td>${L.id }</td>23         <td>${L.name }</td>24         <td>${L.weight }</td>25         <td>${L.color }</td>26         <td><fmt:formatDate value="${L.birth }" pattern="yyyy-MM-dd"></fmt:formatDate></td>27     </tr>28     </c:forEach>29 </table>30 </body>31 </html>
Salin selepas log masuk

 三.运行结果

        运行Servlet,运行结果如图:

        

 

Atas ialah kandungan terperinci java_web学习-显示mysql中的数据. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan