最近在看网易云课颜群的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;