简单java web开发项目_开发简单的JavaWeb项目

//CourseDao.java

packagecom.hjf.dao;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.List;importcom.hjf.entity.Course;importcom.hjf.util.DBUtil;/*** 课程Dao

* Dao层操作数据

*@authorHu

**/

public classCourseDao {/*** 添加

*@paramcourse

*@return

*/

public booleanadd(Course course) {

String sql= "insert into course(name, teacher, classroom) values('" + course.getName() + "','" + course.getTeacher() + "','" + course.getClassroom() + "')";//创建数据库链接

Connection conn =DBUtil.getConn();

Statement state= null;boolean f = false;int a = 0;try{

state=conn.createStatement();

state.executeUpdate(sql);

}catch(Exception e) {

e.printStackTrace();

}finally{//关闭连接

DBUtil.close(state, conn);

}if (a > 0) {

f= true;

}returnf;

}/*** 删除

*

*@paramid

*@return

*/

public boolean delete (intid) {boolean f = false;

String sql= "delete from course where id='" + id + "'";

Connection conn=DBUtil.getConn();

Statement state= null;int a = 0;try{

state=conn.createStatement();

a=state.executeUpdate(sql);

}catch(SQLException e) {

e.printStackTrace();

}finally{

DBUtil.close(state, conn);

}if (a > 0) {

f= true;

}returnf;

}/*** 修改

*@paramname

*@parampass*/

public booleanupdate(Course course) {

String sql= "update course set name='" + course.getName() + "', teacher='" + course.getTeacher() + "', classroom='" +course.getClassroom()+ "' where id='" + course.getId() + "'";

Connection conn=DBUtil.getConn();

Statement state= null;boolean f = false;int a = 0;try{

state=conn.createStatement();

a=state.executeUpdate(sql);

}catch(SQLException e) {

e.printStackTrace();

}finally{

DBUtil.close(state, conn);

}if (a > 0) {

f= true;

}returnf;

}/*** 验证课程名称是否唯一

* true --- 不唯一

*@paramname

*@return

*/

public booleanname(String name) {boolean flag = false;

String sql= "select name from course where name = '" + name + "'";

Connection conn=DBUtil.getConn();

Statement state= null;

ResultSet rs= null;try{

state=conn.createStatement();

rs=state.executeQuery(sql);while(rs.next()) {

flag= true;

}

}catch(SQLException e) {

e.printStackTrace();

}finally{

DBUtil.close(rs, state, conn);

}returnflag;

}/*** 通过ID得到课程信息

*@paramid

*@return

*/

public Course getCourseById(intid) {

String sql= "select * from course where id ='" + id + "'";

Connection conn=DBUtil.getConn();

Statement state= null;

ResultSet rs= null;

Course course= null;try{

state=conn.createStatement();

rs=state.executeQuery(sql);while(rs.next()) {

String name= rs.getString("name");

String teacher= rs.getString("teacher");

String classroom= rs.getString("classroom");

course= newCourse(id, name, teacher, classroom);

}

}catch(Exception e) {

e.printStackTrace();

}finally{

DBUtil.close(rs, state, conn);

}returncourse;

}/*** 通过name得到Course

*@paramname

*@return

*/

publicCourse getCourseByName(String name) {

String sql= "select * from course where name ='" + name + "'";

Connection conn=DBUtil.getConn();

Statement state= null;

ResultSet rs= null;

Course course= null;try{

state=conn.createStatement();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值