【javaweb笔记】8、三层优化,接口,DBUtil,WEB调试及BUG修复

三层优化
1.加入接口
    建议面向接口开发:先接口-再实现类
    --service、dao加入接口
    --接口与实现类的命名规范
        接口:interface,    起名   I实体类Service        IStudentService       IStudentDao    
        实现类:implements    起名   实体类ServiceImpl        StudentServiceImpl  StudentDaoImpl
        接口:    I实体类层所在包名    IStudentService、IStudentDao    接口所在的包:  xxx.service        xx.dao

       实现类:     实体类层所在包名Impl    StudentServiceImpl、StudentDaoImpl   实现类所在的包:xxx.service.impl        xx.dao.impl

    以后使用接口/实现类时,推荐写法:
    接口 x = new 实现类();
    IStudentDao studentDao = new StudentDaoImpl();

2.DBUtil 通用的数据库帮助类,可以简化Dao层的代码量

帮助类 一般建议写在  xxx.util包


A
{

    a(){
        B.connection
    }
}

B
{
    static Connection connection =..
    b{
        
    }
}

方法重构:  将多个方法 的共同代码 提炼出来,单独写在一个方法中,然后引入该方法即可
a()
{
    ..
    c();
    ..
    
}

b()
{
    ..
    c();
    ..
}

c()
{
        [..
    ..    
    ...        
    ..]
}


Web调试:
与java代码的调试 区别:启动方式不同

index.jsp ->index_jsp.java ->index_jsp.class 

jsp->java->class
jsp翻译成的Java 以及编译后的class文件 存在于tomcat中的work目录中

dao和DBUtil的区别:
dao 是处理特定 类的 数据库操作类:
DBUtil是通用  数据库操作类

DBUtil.java

package org.student.util;

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

import org.student.entity.Student;

//通用的数据操作方法
public class DBUtil {
	private static final String URL  ="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" ;
	private static final String USERNAME  ="scott" ;
	private static final String PASSWORD  ="tiger" ;
	public static PreparedStatement pstmt = null ;
	public static Connection connection = null ;
	public static ResultSet rs = null ; 
	//通用的增删改
	public static boolean executeUpdate(String sql,Object[] params) {//{"zs",1}
		try {
			 //Object[] obs = { name,age ,...,x} ; 
//			  String sql = "delete from xxx where Name = ? or id = ?  " ;
//			  pstmt.setInt(1,sno );
			  //setXxx()方法的个数 依赖于 ?的个数, 而?的个数 又和 数组params的个数一致
			  //setXxx()方法的个数 ->数组params的个数一致
				pstmt = createPreParedStatement(sql,params);
			  int count = pstmt.executeUpdate() ;
			  if(count>0)
				  return true ;
			  else 
				  return false ;
			  
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			  return false ;
		} catch (SQLException e) {
			e.printStackTrace();
			  return false ;
		}catch (Exception e) {
			e.printStackTrace();
			return false ;
		}
		finally {
			closeAll(null,pstmt,connection);
		}
}
	//Statement
	public static void closeAll(ResultSet rs,Statement stmt,Connection connection)
	{
		try {
			if(rs!=null)rs.close();
			if(pstmt!=null)pstmt.close();
			if(connection!=null)connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		
		
	}
		
	
	
	
	public static Connection getConnection() throws ClassNotFoundException, SQLException {
		 Class.forName("oracle.jdbc.OracleDriver") ;
		 return  DriverManager.getConnection( URL,USERNAME,PASSWORD ) ;
	}
	
	public static PreparedStatement createPreParedStatement(String sql,Object[] params) throws ClassNotFoundException, SQLException {
		  pstmt = getConnection() .prepareStatement( sql) ;
		  if(params!=null ) {
			  for(int i=0;i<params.length;i++) {
				  pstmt.setObject(i+1, params[i]);
			  }
		  }
		  return pstmt;
	}
	
		//通用的查  :通用 表示  适合与 任何查询
		public static ResultSet executeQuery( String sql ,Object[] params) {//select xxx from xx where name=? or id=?
			Student student = null;
		
			List<Student> students = new ArrayList<>();
			try {
				
				//				  String sql = "select * from student" ;//select enmae ,job from xxxx where...id>3
				
				pstmt = createPreParedStatement(sql,params);
				 rs =  pstmt.executeQuery() ;
				  return rs ;
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				return null ; 
			} catch (SQLException e) {
				e.printStackTrace();
				return null ; 
			}catch (Exception e) {
				e.printStackTrace();
				return null ; 
			}
//			finally {
//					try {
//						if(rs!=null)rs.close();
//						if(pstmt!=null)pstmt.close();
//						if(connection!=null)connection.close();
//					} catch (SQLException e) {
//						e.printStackTrace();
//					} 
//			}
		}
	
	
	
	
	
}

猜你喜欢

转载自blog.csdn.net/kuaileky/article/details/86630419
今日推荐