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 list=new ArrayList();
		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+"/";
%>



  
   学生查询主页
  
  
  
 
姓名 学号 性别
${list.name}${list.number} ${list.sex}

你可能感兴趣的:(javaweb)