java修改功能_javaweb之修改功能

本文介绍了如何使用Java实现课程信息的CRUD操作,包括通过PreparedStatement插入、删除和更新Course对象到数据库,以及查询特定教师的课程。重点展示了DAO层的数据库交互和SQL语句编写。

package dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;//import com.mysql.cj.protocol.ResultBuilder;

import Bean.Course;

import util.DBUtil;public classdaoCourse {publicboolean add(Course n)

{

boolean f=false;int a=0;

String sql="insert submitcourse(classname,teachername,place) values(?,?,?)";try{

Connection conn=DBUtil.getConnection();

PreparedStatement pstmt=conn.prepareStatement(sql);

pstmt.setString(1,n.getClassname());

pstmt.setString(2,n.getTeachername());

pstmt.setString(3,n.getPlace());

a=pstmt.executeUpdate();

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}if(a>0) f=true;returnf;

}publicboolean delete(Course n) {

boolean f=false;int a=0;

String sql="delete from submitcourse where teachername=?";try{

Connection conn=DBUtil.getConnection();

PreparedStatement pstmt=conn.prepareStatement(sql);

pstmt.setString(1,n.getTeachername());

a=pstmt.executeUpdate();

System.out.println("fuck you");

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}if(a>0) f=true;returnf;

}public Listlist(){

String sql="select * from submitcourse";

List list=new ArrayList<>();

ResultSet rs=null;

Course ns=newCourse();try{

Connection conn=DBUtil.getConnection();

PreparedStatement pstmt=conn.prepareStatement(sql);//pstmt.setString(1,n.getTeachername());

rs=pstmt.executeQuery();while(rs.next())

{

ns=new Course(rs.getString(1),rs.getString(2),rs.getString(3));

list.add(ns);

}//System.out.println(rs);

System.out.println(rs);

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}returnlist;

}publicboolean update(Course n,String old_teachername){//修改信息

boolean f=false;int a=0;

String sql= "update submitcourse set classname='"+n.getClassname()+"',teachername='"+n.getTeachername()+"',place='"+n.getPlace()+"' where teachername='"+old_teachername+"'";try{

Connection conn=DBUtil.getConnection();

PreparedStatement pstmt=conn.prepareStatement(sql);

a=pstmt.executeUpdate();

System.out.println(a);

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}if(a>0) f=true;returnf;

}publicCourse queryByTeachername(String teachername) {//查询信息

String sql="select * from submitcourse where teachername=?";

Course n=newCourse();try{

ResultSet rs=null;

Connection conn=DBUtil.getConnection();

PreparedStatement pstmt=conn.prepareStatement(sql);

pstmt.setString(1,teachername);

rs=pstmt.executeQuery();while(rs.next()) {

n=new Course(rs.getString(1), rs.getString(2),rs.getString(3));

}

System.out.println("fuck you");

}catch(SQLException e) {//TODO Auto-generated catch block

e.printStackTrace();

}returnn ;

}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值