第14章 JAVA EE基础学习-JDBC概述说明-2-与WEB层整合说明-1

MySQL驱动发展情况:

1 TYPE 1:JDBC-ODBC Brdge

2 TYPE 2:C/C++

3 TYPE 3:C/JAVA 

4 TYPE 4:JAVA 编写


开发中遇到的问题:

1 我们在访问服务器时候,不允许外部人员直接访问我们的某个文件,我们可以在项目 web.xml 配置:

<!-- 设置所有jsps文件夹下的jsp文件不能直接被访问 -->
  <security-constraint>
  	<web-resource-collection>
  		<web-resource-name>jsp-pages</web-resource-name>
  		<url-pattern>/jsps/*</url-pattern><!-- 所有/jsps/文件下的文件都不能别访问 -->
  	</web-resource-collection>
  	<auth-constraint/>
  </security-constraint>
  
  <login-config>
  	<auth-method>BASIC</auth-method>
  </login-config>

2 我们项目开发时候,如果确认servlet已经从jsp中获取到数据,而无法存入数据库时候,我们需要检查:

   A数据库连接:是否已经连接进入数据库?

      驱动是否正确(驱动 jar包必须放在  WEB_INF/lib/   目录下)

      数据库名字,账号 ,密码 是否正确

   B数据库语句是否正确?补全准备语句是否正确?是否出现异常转换情况?

注:我们导入的外部jar包必须放在WEB_INF/lib/ 目录下程序才能识别。

如下是一些基本的程序片段:

index.jsp

<%@ 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>
    <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>
    <h3>学生管理系统</h3>
    <ul>
    	<li><a href="<c:url value="/smsMgr"/>?act=toReg">新生注册登记</a></li>
    	<li><a href="<c:url value="/smsMgr"/>?act=loadall">新生注册登记</a></li>
    	 
    </ul>
  </body>
</html>

StudentMgrServlet.java

package edu.fjnu.training.conrtoller;

import java.io.IOException;
import java.io.PrintWriter;

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

import edu.fjnu.training.dao.StudentDao;
import edu.fjnu.training.dao.StudentDaoJDBCImpl;
import edu.fjnu.training.domain.Student;

public class StudentMgrServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public StudentMgrServlet() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//设置请求编码为UTF-8
		request.setCharacterEncoding("utf-8");
		
		//获取请求参数信息
		String act=request.getParameter("act");
		//请求注册toReg
		if("toReg".equals(act))
		{
			request.getRequestDispatcher("/jsps/student/reg_student.jsp").forward(request,response);;
		}
		//请求执行存储操作
		else if("reg".equals(act))
		{
			Student stu=new Student();
			stu.setStuNo(request.getParameter("stuNo"));
			stu.setStuName(request.getParameter("stuName"));
			stu.setStuMark(Integer.parseInt(request.getParameter("stuMark")));
			
			StudentDao stuDao=new StudentDaoJDBCImpl();
			stuDao.addStudent(stu);
			response.sendRedirect("smsMgr?act=loadall");
		}
		//加载所有学生信息
		else if("loadall".equals(act))
		{
			StudentDao stuDao=new StudentDaoJDBCImpl();
			request.setAttribute("stuList", stuDao.loadAllStudent());
			request.getRequestDispatcher("./jsps/student/list_student.jsp").forward(request, response);
		}
		//删除学生信息
		else if("remove".equals(act))
		{
			System.out.println("I like ...........");
			String stuNo=request.getParameter("stuNo");
			StudentDao stuDao=new StudentDaoJDBCImpl();
			stuDao.removeStudent(stuNo);
			
			response.sendRedirect("smsMgr?act=loadall");
		}
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}

reg_student.jsp

<%@ 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>
    <base href="<%=basePath%>">
    
    <title>My JSP 'reg_student.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="../../../css/style.css">  --%> 
	<%-- <link rel="stylesheet" type="text/css" href="/sms/css/style.css">   --%>
	<%-- <link rel="stylesheet" type="text/css" href="<%=basePath%>/css/style.css">--%>
	<link rel="stylesheet" type="text/css" href="<c:url value="/css/style.css"/>">   
  </head>
  
  <body>
  	
    <h3>新生登记(2018年5月22日08:02:55)</h3>
    <form action="<c:url value="/smsMgr"/>?act=reg" method="post">
    	<div>
    		<span>学号:</span>
    		<input type="text" name="stuNo">
    	</div>
    	<div>
    		<span>姓名:</span>
    		<input type="text" name="stuName">
    	</div>
    	<div>
    		<span>成绩:</span>
    		<input type="text" name="stuMark">
    	</div>
    	<div>
    		<input type="submit" value="注册登记">
    	</div>
    	
    </form>
    <a href="<c:url value="/" />">首页</a>
    <%-- <jsp:include page="../../css/footer.jsp"></jsp:include>--%>
  </body>
</html>

list_student.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" isELIgnored="false"%>
<%@ 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>
    <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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script>
	
		function removeStudent(stuNo,stuName){
			if(confirm("您确定要删除(学号:"+stuNo+" 姓名:"+stuName+")的学生信息吗?"))
			{ 
				location.href="<c:url value="/smsMgr"/>?act=remove&stuNo="+stuNo;
			}
		}
	</script>
  </head>
  
  <body>
  	<h3>学生列表</h3>
  	<table border="1" cellspacing="0" cellpadding="3px">
  		<caption>学生信息列表</caption>
  		
  		<thead>
  			<tr>
  				<th>序号</th>
  				<th>学号</th>
  				<th>姓名</th>
  				<th>成绩</th>
  				<th>操作</th>
  			</tr>
  		</thead>
  			
  		<tbody>
  			<c:forEach var="stu"  varStatus="index" items="${requestScope.stuList }">
  			<tr>
  				<td>${index.index+1 }</td>
  				<td>${stu.stuNo }</td>
  				<td>${stu.stuName }</td>
  				<td>${stu.stuMark }</td>
  				<td>
  					<button>修改</button>
  					<button οnclick="removeStudent('${stu.stuNo}','${stu.stuName }');">删除</button>
  				</td>
  			</tr>
  			</c:forEach>
  		</tbody>
  	</table>
  	<hr>
  	<a href="<c:url value="/" />">首页</a>
  </body>
</html>

DBUtils.java

/**
 * 工  程   名:JDBCDemoPrj-20180221	<br>
 * 文  件   名:DBUtils.java	<br>
 * 工具包名:edu.fjnu.training.utils	<br>
 * 功能描述:TODO	<br>
 * 创建时间:2018年5月21日 下午7:27:45	<br>
 * 版本信息:V1.0
 * @创建人:周开伦	
 */
package edu.fjnu.training.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 类名:DBUtils	数据库连接类<br>
 * 功能描述:	<br> 
 * 创建日期:2018年5月21日 下午7:27:45	<br>
 * 修改备注:
 * @作者信息:Zhou kailun	<br>
 */
public class DBUtils {
	/**连接串,说明数据库所在位置,,连接方式jdbc 数据库名字:testdb*/
	private static final String CONN_URL="jdbc:mysql://localhost:3306/test";
	
	/**数据库连接用户账号*/
	private static final String USER="root";
	/**数据库连接密码*/
	private static final String PASSWORD="zklpeng";
	private static final DBUtils utils=new DBUtils();
	
	private DBUtils()
	{
		
	}
	/**
	 * 方法名:获得DBUtils实例对象(单例模式)	<br>
	 * 功能描述:获得数据库操作DBUtils实例对象(单例模式)		<br>  
	 * @return : DBUtils对象
	 * @throws:
	 * @see   :
	 */
	public static  DBUtils getInstance()
	{
		
		return utils;
	}
	/**
	 * 方法名:获得数据库连接	<br>
	 * 功能描述:取得数据的连接实例	<br>  
	 * @return : 
	 * @throws:
	 * @see   :
	 */
	public Connection getConn()
	{
		Connection conn=null;
		try{
			//在程序中以com.mysql.jdbc.Driver去查找类,看是否能够找到,找不到抛出异常,
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection(CONN_URL,USER,PASSWORD);
		}catch(ClassNotFoundException e){
			e.printStackTrace();
			//抛出驱动异常
			System.out.println("MySql驱动没找到或者型号不对,请检查驱动是否导入....");
		}catch(SQLException e){
			//抛出数据库连接异常
			e.printStackTrace();
			System.out.println("MySQL数据连接异常,请检查数据库是否存在或者数据库登录账号密码是否正确....");
		}
		return conn;
	}
	/**
	 * 方法名:释放数据库连接资源	<br>
	 * 功能描述:将获得的数据库连接资源,准备语句,结果集进行释放	<br>  
	 * @param connect
	 * @param pstmt
	 * @param rset : 
	 * @throws:
	 * @see   :
	 */
	public void releaseResource(Connection connect,PreparedStatement pstmt,ResultSet rset)
	{
		try{
			if(rset!=null)
			{
				rset.close();
			}
			if(pstmt!=null)
			{
				pstmt.close();
			}
			if(connect!=null)
			{
				connect.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("数据库资源连接释放资源失败,请检查连接和资源释放....");
		}catch(Exception e)
		{
			e.printStackTrace();
			System.out.println("异常产生在数据库资源释放模块,请检查操作是否正确....");
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值