CREATE TABLE pet ( petId INT(11) PRIMARY KEY AUTO_INCREMENT, petName VARCHAR(50) NOT NULL, petBreed INT NOT NULL, petSex INT NOT NULL, birthday DATETIME NOT NULL, description VARCHAR(200) ) ;
狗、猫、鸟、兔查询
添加信息
entity-pet
package entity; public class Pet { int petId; String petName; int petBreed; int petSex; String birthday; String description; public int getPetId() { return petId; } public void setPetId(int petId) { this.petId = petId; } public String getPetName() { return petName; } public void setPetName(String petName) { this.petName = petName; } public int getPetBreed() { return petBreed; } public void setPetBreed(int petBreed) { this.petBreed = petBreed; } public int getPetSex() { return petSex; } public void setPetSex(int petSex) { this.petSex = petSex; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
dao-basedao
package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class BaseDao { //获取连接 protected Connection getConnection(){ Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "root", "ROOT"); } catch (Exception e) { e.printStackTrace(); } return conn; } //关闭连接 protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){ try { if(rs != null) rs.close(); if(ps != null) ps.close(); if(con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } } }
petdao
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; import entity.Pet; public class petDao extends BaseDao { // 发送,回复---insert操作 public void addPet(Pet pet) { try { Connection con = getConnection(); String sql = "insert into pet(petName,petBreed,petSex,birthday,description) values(?,?,?,?,?);"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, pet.getPetName());// 给sql语句的问号赋值 ps.setInt(2,pet.getPetBreed()); ps.setInt(3, pet.getPetSex()); ps.setString(4, pet.getBirthday()); ps.setString(5, pet.getDescription()); ps.executeUpdate(); closeAll(con, ps, null); } catch (SQLException ex) { ex.printStackTrace(); } } public List<Pet> getPetByBreed(int petBreed) { List<Pet> list = new ArrayList<Pet>(); try { Connection con = getConnection();// 获取连接 String sql = "select * from pet where petBreed=?";// 编写sql语句 PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, petBreed);// //给sql问号赋值 ResultSet rs = ps.executeQuery();// 执行查询 // 处理查询结果 while (rs.next()) { // 循环读取rs结果集,每一行作为一个msg对象,放入list集合中 Pet pet = new Pet(); pet.setPetName(rs.getString("petName")); pet.setPetSex(rs.getInt("petSex")); pet.setPetBreed(rs.getInt("petBreed")); pet.setBirthday(rs.getString("birthday")) ; pet.setDescription(rs.getString("description"));; list.add(pet); } closeAll(con, ps, rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } // public static void main(String[] args) { // petDao p=new petDao(); // List<Pet> list=p.getPetByBreed(1); // System.out.println(list.size()); // for (int i = 0; i < list.size(); i++) { // System.out.println(list.get(i).getPetName()+ // list.get(i).getBirthday()+ // // list.get(i).getPetSex() //); // // } //} }
index
<%@page import="entity.Pet"%> <%@page import="dao.petDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% request.setCharacterEncoding("utf-8"); String Breed = (String)request.getParameter("petBreed"); // out.print(Breed); int petBreed = Integer.parseInt(Breed); //out.print(petBreed); // out.print(petBreed); petDao p=new petDao(); List<Pet> list=p.getPetByBreed(petBreed); petDao pd = new petDao(); // out.print(list.size()); %> <FORM action="index.jsp" method="post" name="myform"> <TABLE border="0" cellpadding="0" cellspacing="0" align="center" width="530"> <TR> <TD height="58" colspan="2"><IMG src="images/main.jpg"></TD> </TR> <TR><TD colspan="2" align="center">品种: <SELECT name="petBreed" > <OPTION value=1>狗</OPTION> <OPTION value=2>猫</OPTION> <OPTION value=3>鸟</OPTION> <OPTION value=4>兔</OPTION> </SELECT> </TR> <TR><TD colspan="2" align="center"> <input type="submit" value="查询"> <a href="insert.jsp">【新增宠物】 </TD></TR> </TABLE> </FORM> <table border="1" width="1000"align="center"> <tr> <td>宠物昵称</td> <td>出生日期</td> <td>性别</td> </tr> <% for (int i = 0; i < list.size(); i++) { String sex="雄"; int s=list.get(i).getPetSex(); if(s==0){ sex="雌"; } %> <tr> <td><%=list.get(i).getPetName()%></td> <td><%=list.get(i).getBirthday()%></td> <td><%=sex%> </td> </tr> <% } %> </table> </body> </html>
insert
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript"> function display_alert() { alert("提交成功") } </script> </head> <body> <FORM action="doinsert.jsp" method="post" name="myform"> <TABLE border="0" cellpadding="0" cellspacing="0" align="center" width="530"> <TR> <TD height="108" colspan="2"><IMG src="images/pet.jpg"></TD> </TR> <TR> <TD width="107" height="36">昵称:</TD> <TD width="524"><INPUT name="petName" type="text" maxlength="16"></TD> </TR> <TR> <TD width="107" height="36">品种:</TD> <TD width="524"> <SELECT name="petBreed" > <OPTION value=1>狗</OPTION> <OPTION value=2>猫</OPTION> <OPTION value=3>鸟</OPTION> <OPTION value=4>兔</OPTION> </SELECT> </TD> </TR> <TR> <TD width="107" height="36">性别:</TD> <TD width="524"> <INPUT name="petSex" type="radio" value="雄" checked>雄 <INPUT name="petSex" type="radio" value="雌" class="input">雌 </TD> </TR> <TR> <TD width="107" height="36">出生日期:</TD> <TD width="524"><INPUT name="birthday" type="text" maxlength="16">yyyy-mm-dd</TD> </TR> <TR> <TD width="107" height="36">宠物描述:</TD> <TD width="524"><INPUT name="description" type="text" maxlength="160"></TD> </TR> <TR><TD colspan="2" align="center"> <INPUT type="submit" value="同意以下协议条款并提交"onclick="display_alert()" value="Display alert box"> </TD></TR> <TR><TD colspan="2"> <TEXTAREA cols="" rows="" readonly="readonly" style="480px;height:110px;font-size:12px;color:#666"> 一、总则 1.1 用户应当同意本协议的条款并按照页面上的提示完成全部的注册程序。用户在进行注册程序过程中点击"同意"按钮即表示用户与百度公司达成协议,完全接受本协议项下的全部条款。 1.2 用户注册成功后,百度将给予每个用户一个用户帐号及相应的密码,该用户帐号和密码由用户负责保管;用户应当对以其用户帐号进行的所有活动和事件负法律责任。 </TEXTAREA> </TD> </TR> </TABLE> </FORM> </body> </html>
doinsert
<%@page import="entity.Pet"%> <%@page import="dao.petDao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% request.setCharacterEncoding("utf-8"); String petName = request.getParameter("petName"); String Breed = request.getParameter("petBreed"); int petBreed = Integer.parseInt(Breed); String Sex = request.getParameter("petSex"); int petSex=1; if(Sex.equals("雌")){ petSex=0; } String birthday = request.getParameter("birthday"); String description = request.getParameter("description"); Pet pet=new Pet(); pet.setPetName(petName); pet.setPetSex(petSex); pet.setPetBreed(petBreed); pet.setBirthday(birthday) ; pet.setDescription(description); petDao pd=new petDao(); pd.addPet(pet); request.getRequestDispatcher("index.jsp").forward(request, response); %>