java web 三层架构 oracle 实现学生管理系统

最近在看网易云课颜群的java web课程 做一些总结

首先看一下结构:

java 部分

jsp部分

DBUtil.java  这一部分主要是放置了连接数据库的一些通用方法。

package oge.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 javax.swing.text.html.HTMLDocument.HTMLReader.PreAction;

import org.student.entity.Student;

import com.sun.corba.se.spi.orbutil.fsm.State;

//数据库帮助类
public  class  DBUtil{
	 private static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
	 private static final String USERNAME="sys as sysdba";
	 private static final String PASSWORD="123456";	
public	 static	 Connection connection= null;
public	 static	 PreparedStatement pstmt=null;
	 static	ResultSet rs;
	//查询总人数
	 public static int getTotalCount(String sql) {
		 int count=-1;
		 try {
			 
		pstmt=	createPreparedStatement(sql, null);
			rs = pstmt.executeQuery();
		if(rs.next()) {	
			count = rs.getInt(1);
			
		}
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		 finally {
			closeAll(rs, pstmt, connection);
		}
		 return count;
		 
	 }
	 
	 //获取Connection
	 
public static Connection getConnection() throws ClassNotFoundException, SQLException {
		 Class.forName("oracle.jdbc.OracleDriver");
		return DriverManager.getConnection(URL,USERNAME,PASSWORD);
	 }
       //获取pstmt
public static PreparedStatement createPreparedStatement(String sql,Object[] params) throws ClassNotFoundException, SQLException {
		 pstmt= getConnection().prepareStatement(sql);
		//pstmt.setInt(1, sno);
		if(params!=null) {
		for(int i=0;i<params.length;i++) {
			pstmt.setObject(i+1, params[i]);
		}}
		return pstmt;
	 }
	 
public static  boolean executeupdate(String sql, Object[] params) {
	
try {
	 
//String sql="delete from student where sno = ?";
	pstmt =createPreparedStatement(sql,params);
int count =	pstmt.executeUpdate();
if(count>0) 
	return true;
else 
	return false;


}catch (ClassNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();	
	 return false;

} catch (SQLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	 return false;

}catch (Exception e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
	 return false;
	
}
finally {
//	closeAll(null,pstmt,connection);
}
}

public static  ResultSet executeQuery(String sql,Object[]params ) {
	
	List<Student> students= new ArrayList<>();


	Student student =null;
	try {
	
	//String sql="select * from student ";

	pstmt= createPreparedStatement(sql, params);
	 rs = pstmt.executeQuery();

	return rs;	
		
		
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();	
		return null;
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return null;
	}catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return null;
	}

	
};
	
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) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	   } 
    }
}

  IStudentDao 下面写Dao的接口

package org.student.dao;

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 java.util.List;

import org.student.entity.Student;

public interface IStudentDao {
	 
	 
public boolean isExist(int sno);
	
public int getTotalCount();
//int currentPage 页码    pageSize数据条数
public List<Student> queryStudentsByPage(int currentPage,int pageSize);

public boolean addStudent(Student student);
		
		
public boolean deleteStudentBySno(int sno);


public boolean  updateStudentBySno(int sno, Student student) ;

public List<Student> queryAll();

public Student queryStudentBysno(int sno);
}

StudentDaoImpl.java

package org.student.dao.impl;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import org.student.dao.IStudentDao;
import org.student.entity.Student;

import oge.student.util.DBUtil;
import sun.security.pkcs11.Secmod.DbMode;

public class StudentDaoImpl implements IStudentDao{
	 private final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
			 private final String USERNAME="sys as sysdba";
			 private final String PASSWORD="123456";	
			 
				
				PreparedStatement pstmt=null;
				ResultSet rs=null;
				Student student =null;
			 
public boolean isExist(int sno) {	
		
		    return queryStudentBysno(sno)==null?false:true; 
  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值