您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

java_web学习-显示mysql中的数据

2024/4/24 2:50:02发布10次查看
一.建立数据库  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 }
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 }
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 }
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>
三.运行结果        运行servlet,运行结果如图:
以上就是java_web学习-显示mysql中的数据的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product