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

被折叠的 条评论
为什么被折叠?



