jsp jdbc mysql增删改查_JSP + Servlet + JDBC + Mysql 实现增删改查 课程管理系统

1.项目目录结构

f8a7bbc13b1732a684de98ea0956d118.png

2.项目环境

Eclipse IDE  MYSQL  jdk tomcat

jar包

ac7db03ebd84ccfe55c20a2b499fab57.png

3.数据库相关配置

先创建一个course的数据库名,然后在建立一个course的表

a8f1f24e2c634f7a4a0ce3365f92cd38.png

要注意将id字段 自动递增

4.源代码

1.Course.java

package com.hjf.entity;

public class Course {

private int id;

private String name;

private String teacher;

private String classroom;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getTeacher() {

return teacher;

}

public void setTeacher(String teacher) {

this.teacher = teacher;

}

public String getClassroom() {

return classroom;

}

public void setClassroom(String classroom) {

this.classroom = classroom;

}

public Course(int id, String name, String teacher, String classroom) {

this.id = id;

this.name = name;

this.teacher = teacher;

this.classroom = classroom;

}

public Course(String name, String teacher, String classroom) {

this.name = name;

this.teacher = teacher;

this.classroom = classroom;

}

}

2.DBUtil.java

package com.hjf.util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

/**

* 数据库连接工具

* @author zlj

*

*/

public class DBUtil {

public static String db_url = "jdbc:mysql://localhost:3306/course?useSSL=false";

public static String db_user = "root";

public static String db_pass = "199126";

public static Connection getConn () {

Connection conn = null;

try {

Class.forName("com.mysql.jdbc.Driver");//加载驱动

conn = DriverManager.getConnection(db_url, db_user, db_pass);

} catch (Exception e) {

e.printStackTrace();

}

return conn;

}

/**

* 关闭连接

* @param state

* @param conn

*/

public static void close (Statement state, Connection conn) {

if (state != null) {

try {

state.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public static void close (ResultSet rs, Statement state, Connection conn) {

if (rs != null) {

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (state != null) {

try {

state.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

3.CourseDao.java

package com.hjf.dao;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.List;

import com.hjf.entity.Course;

import com.hjf.util.DBUtil;

/**

* 课程Dao

* Dao层操作数据

* @author zhang

*

*/

public class CourseDao {

/**

* 添加

* @param course

* @return

*/

public boolean add(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;

}

return f;

}

/**

* 删除

*

* @param id

* @return

*/

public boolean delete (int id) {

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;

}

return f;

}

/**

* 修改

* @param name

* @param pass

*/

public boolean update(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

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值