myeclips+mysql+jsp做的一个简单的查询功能

当你在学习javaweb的时候想做一个简单的查询系统。而你确无法下手的时候。那么请你重新查看一下mvc模式。
一般在开发的时候MVC为主要的开发模式。现在我以查询功能为例写一个简单的javaweb.

com.student.vo这里主要是定义的属性和get/set方法
com.student.dao这里写接口,根据业务的需求来是实现
com.student.dao.Impl这里是对功能的实现,而且还要实现数据库的操作
com.student.service这里主要写的是service的接口    
com.student.service.Impl    对service接口的实现
com.student.db这个包里主要写的是数据库的连接方式
com.student.servlet处理用户的请求

建立文件包的顺序vo-->dao-->daoImpl-->service-->serviceImpl-->db-->sevlet
其中数据库的包一开始就可以创建成功。

1.vo的创建
package com.student.vo;


public class Student {
	private String number;
	private String name;
	private String sex;

	public Student() {
		super();
	}
	public Student(String number, String name, String sex) {
		super();
		this.number = number;
		this.name = name;
		this.sex = sex;
		
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
}

2.数据库的创建
我这里用的是利用反射的方式读取数据库的方法:

db.propertise:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/student?useUniocde=ture&characterEncoding=UTF-8
username=root
password=14143

dbUtil.java文件

package com.student.db;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Properties;

public class dbUtil {
	private static PreparedStatement pstmt=null;
	private static ResultSet rs=null;
	//获得一个连接方法
	public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException{
		//利用加载器类
		InputStream in=dbUtil.class.getClassLoader().getResourceAsStream("com/student/db/db.propertise");
		Properties pro =new Properties();
		pro.load(in);
		String driver=pro.getProperty("driver");
		String url=pro.getProperty("url");
		String username=pro.getProperty("username");
		String password=pro.getProperty("password");
		Class.forName(driver);
		Connection connection=DriverManager.getConnection(url, username, password);
		return connection;
		
	}
	//执行
	public static ResultSet execuQuery( Connection connection,String sql,Object...objects){
		//Object...表示是一个可变参数可以同时传多个参数
		try {
			pstmt=connection.prepareStatement(sql);
			//判断当传入的
			if(objects!=null){
				for (int i = 0; i < objects.length; i++) {
					pstmt.setObject(i+1, objects[i]);
				}
			}
			rs=pstmt.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("数据库执行异常");
		}
		return rs;
	}
	//关闭数据库
	public static void closeAll(Connection connection){//为什么传入一个参数?
		try {
			if (rs != null) {
				rs.close();
			} 
			if (pstmt!=null) {
				pstmt.close();
			}
			if (connection!=null) {
				connection.close();
			}
		} catch (Exception e) {
			System.out.println("数据库关闭异常");
		}
	}
}

3.写dao中的代码:

package com.student.dao; import com.student.vo.Student; public interface findDao { //查找用户 public Student findUser(String userCount); }

daoImpl接口的实现

package com.student.dao.Impl;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.fabric.xmlrpc.base.Data;
import com.student.dao.findDao;
import com.student.db.dbUtil;
import com.student.vo.Student;

public class StudentDaoImpl implements findDao{
	private Student student=null;
	Connection connection=null;
	ResultSet rs=null;
	@Override
	public Student findUser(String userCount) {
		//查找账号
		String sql="select *from stu where id=?";
		//调用封装好的dbUtil查询
		try {
			connection=dbUtil.getConnection();
			rs=dbUtil.execuQuery(connection, sql, userCount);
			if (rs!=null) {
				student =new Student();
				while (rs.next()) {
					student.setNumber(((String)rs.getObject(1)));
					student.setName((String)rs.getObject(2));
					student.setSex((String)rs.getObject(3));
					
				}
			}
			dbUtil.closeAll(connection);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return student;
		
	}
	

}

4.service接口

package com.student.service;

import com.student.vo.Student;

public interface service {
	//根据学号查找学生的信息
	public Student findUser(String userCount);
}

serviceImpl.java:

package com.student.service.Impl;

import com.student.dao.findDao;
import com.student.dao.Impl.StudentDaoImpl;
import com.student.service.service;
import com.student.vo.Student;

public class serviceImpl implements service{
	private findDao studentDao=new StudentDaoImpl();
	@Override
	public Student findUser(String userCount) {
		//根据Dao层的用户查询用户
		return studentDao.findUser(userCount);
	}

}

5.最后是servlet:

package com.student.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.student.service.service;
import com.student.service.Impl.serviceImpl;
import com.student.vo.Student;
public class StudentsServlet extends HttpServlet {
	
	private static final long serialVersionUID=1L;
	private Student student=null;
	private service studentService=new serviceImpl();
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setIntHeader("Refresh", 5);
		List<Student> list=new ArrayList<Student>();
		String scount =request.getParameter("scount");
		student=studentService.findUser(scount);
		list.add(student);
		request.setAttribute("studentlist", list);
		try {
			request.getRequestDispatcher("index.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

}

6.再把首页index粘贴出来:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
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>
   <title>学生查询主页</title>
  </head>
  
  <body>
 <form action=StudentsServlet method="post"> 
<input type="text"  name="scount">
<input type="submit" name="Submit">
</form> 
 
<table>  
      <tr>  
          <th>姓名</th>  
          <th>学号</th>  
          <th>性别<th>  
      </tr>  
	  <c:if test="${not empty studentlist}">
      <c:forEach items="${studentlist}" var="list">  
      <tr>  
              <td>${list.name}</td><td>${list.number}</td> <td>${list.sex}</td>   
          </tr>  
      </c:forEach>  

	 </c:if>
  </table>

  </body>
</html>

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值