用javabean写一个查询系统
2015-12-07 16:33
393 查看
mvc模式java类
数据库链接类
数据封装
页面二
数据库链接类
package com.csmy.db; import java.sql.*; public class Dao { static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getconn() throws SQLException { Connection conn = null; conn = DriverManager .getConnection( "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8", "root", "root"); return conn; } public static void closeconn(Connection conn) throws SQLException { if (conn != null && !conn.isClosed()) { conn.close(); } } }连接类方法
package com.csmy.db; import java.sql.*; import com.csmy.bean.Student; public class GetDao { public Student getStudent(String sno)throws SQLException { Connection conn = null; ResultSet rs=null; Student st=null; conn = Dao.getconn(); if (conn != null) { String sql = "select *from students where sno=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, sno); rs=ps.executeQuery(); if(rs.next()){ st=new Student(); st.setSno(sno); st.setSname(rs.getString("sname")); st.setSsex(rs.getString("ssex")); st.setSage(rs.getInt("sage")); } Dao.closeconn(conn); } return st; } }
数据封装
package com.csmy.bean; public class Student { private String sno; private String sname; private String ssex; private int sage; public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public int getSage() { return sage; } public void setSage(int sage) { this.sage = sage; } }servlet方法
package com.csmy.servlet; import java.io.IOException; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.csmy.bean.Student; import com.csmy.db.GetDao; @SuppressWarnings("serial") public class QueryStudent extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String sno = request.getParameter("sno"); if (sno == null) { } else { GetDao dao = new GetDao(); Student st; try { st = dao.getStudent(sno); if (st != null) { request.setAttribute("st", st); request.getRequestDispatcher("QueryResult.jsp").forward( request, response); } else { request.getRequestDispatcher("QueryStudent.jsp").forward( request, response); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }jsp页面
<%@ page language="java" import="com.csmy.bean.Student" 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>学生信息查询结果</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"> </head> <jsp:useBean id="st" type="com.csmy.bean.Student" scope="request"/> <body> 查询结果:<p> 学号:<jsp:getProperty property="sno" name="st"/> 姓名:<jsp:getProperty property="sname" name="st"/> 性别:<jsp:getProperty property="ssex" name="st"/> 年龄:<jsp:getProperty property="sage" name="st"/> </body> </html>
页面二
<%@ 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>学生信息查询</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"> </head> <body> <center> <form action="QueryStudent" method="post"> 请输入学号: <input type="text" name="sno"> <p> <input type="submit" value="查询"> <input type="reset" value="取消"> </form> </center> </body> </html>
相关文章推荐
- MyEclipse快捷键大全(简单粗暴)
- Spring连接数据库的几种常用的方式
- Java分布式事务-转载
- Java多线程简单介绍-1
- Java 8 Stream Parallel要点
- spring同时集成遇到多个properties资源文件加载的问题
- debug jdk source can't watch variable what it is
- java笔记-构造函数
- java集合
- 第二篇 虚拟机性能监控与故障处理工具(JDK的命令行工具/JDK的可视化工具)
- Java常见异常总结
- java.io.IOException: tmpFile.renameTo(classFile) failed
- Java中 <? super T>,<? extends T>,和<T>的区别
- 【java基础】Java 中的进程与线程
- Java 多线程 竞争造成的异常
- java中的字符串操作
- [转载]如何使用eclipse 生成runnable jar包
- Java中的数组类Array
- java使用Calendar制作万年历
- java常见错误--Access restriction: The type BASE64Encoder