JavaBean DAO and VO查询数据库中的信息

ADO:将访问数据库的操作放到特定的类中去处理,JSP是表现层,所以在表现层调用这个特定的类提供的方法对数据库进行操作,通常将该java类叫做DAO类,它专门负责对数据库的访问

VO:就是普通的JavaBean

JavaBean:的属性,是根据数据中表的列(字段)决定的

JavaBean需要满足3个条件:

1.类 public

2.属性 private

3.属性的setter和gettre方法

例子:

 

项目结构:

 

 com.Bean包:Student.java,代码如下:

package com.Bean;

public class Student {
	private String id;
	private String name;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
}

com.Dao包:StudentDao.java,编写数据库相关的操作,代码如下:

package com.Dao;

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.Util.DBUtil;

import com.Bean.Student;

public class StudentDao {
	public static ArrayList query(String name) throws Exception{
		ArrayList<Student> students = null;//声明变量需要赋初值
		ResultSet rs=null;
		Connection conn=null;
		PreparedStatement ps=null;
		try{
			students=new ArrayList<>();
			 conn=DBUtil.getConectionDb();//连接数据库
			String s1 = "select * from users where name=?";
			ps = conn.prepareStatement(s1);//SQL查询
			ps.setString(1, name);
			 rs = ps.executeQuery();
			while(rs.next()){
				Student s = new Student();//创建JavaBean的Student对象
				s.setId(rs.getString(1));
				s.setName(rs.getString(2));
				students.add(s);//将对象放到list集合中
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtil.CloseDB(rs, ps, conn);//关闭数据库
		}
		return students;
	}
}

com.Util包:DBUtil.java,数据库的连接代码:

package com.Util;
 
import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
 
public class DBUtil {
	public static String username="root";//数据库登录用户
	public static String password="123456";//密码
	public static String database="student";//数据库名字
	public static String url="jdbc:mysql://localhost:3306/"+database+"?useUnicode=true&characterEncoding=utf-8";
	
	static{//静态的会自动调用
		try{
			Class.forName("com.mysql.jdbc.Driver");//加载驱动
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}
	}
	//连接数据库的方法
	public static Connection getConectionDb(){
		Connection conn=null;
		try{
			conn=DriverManager.getConnection(url,username,password);//获得数据库连接
		}catch(SQLException e){
			e.printStackTrace();
		}
		return conn;
	}
	//关闭数据库的方法
	public static void CloseDB(ResultSet rs,PreparedStatement stm,Connection conn){
		if(rs!=null){
			try{
				rs.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(stm!=null){
			try{
				stm.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try{
				conn.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
	}
	
 
}

创建查询页面,form.jsp,代码如下:

<%@ 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 'form.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>
    <form action="query.jsp" method="post">
           请输入要查询的名字:<input name="name" type="text">
    <input type="submit" value="查询">
    </form>
  </body>
</html>

创建查询的显示页面,query.jsp,代码如下:

<%@ page language="java" import="java.util.*" import="com.Bean.*" pageEncoding="UTF-8"%>
<%@ page import="com.Dao.*" %>
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 'query.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 name = request.getParameter("name");
    ArrayList list = StudentDao.query(name); //调用StudentDao的方法query
     %>
     <table border=1>
     <tr bgcolor="yellow">
     <td>学号</td>
     <td>姓名</td>
     </tr>
     <%
     for(int i=0;i<list.size();i++){//遍历循环list集合
     Student s=(Student)list.get(i);//转换类型获取Student的属性
      %>
      <tr bgcolor="yellow">
      <td><%=s.getId() %></td>
      <td><%=s.getName() %></td>
      </tr>
      <%
      }
       %>
     </table>
  </body>
</html>

运行form.jsp,输入要查询的数据:

 点击查询:

数据库找中的表:

 

猜你喜欢

转载自blog.csdn.net/dengfengling999/article/details/124305877