📄 kechengbean.java
字号:
package kecheng;
import conn.DataBaseConnection;
import java.sql.*;
import java.util.*;
import java.io.*;
public class KechengBean
{
private Connection con=null;
public KechengBean()
{
this.con=DataBaseConnection.getConnection();
}
//课程是否存在
public boolean existKecheng(Kecheng kecheng) {
try {
String courseId= kecheng.getCourseId();
String courseName= kecheng.getCourseName();
PreparedStatement pstmt = con.prepareStatement(
"select courseId,courseName from kecheng where courseId='" +
courseId + "' or courseName='"+courseName+"'");
ResultSet rst = pstmt.executeQuery();
if (rst.next()) {
return true;
}
} catch (Exception ex) {
return false;
}
return false;
}
//插入
public String addKecheng(Kecheng kecheng)
{
PreparedStatement pstmt=null;
String str="";
try
{
if (existKecheng(kecheng)) {
return "exist"; //存在此课程
}
pstmt=con.prepareStatement("insert into kecheng values(?,?,?,?,?)");
pstmt.setString(1,kecheng.getCourseId());
pstmt.setString(2,kecheng.getCourseName());
pstmt.setInt(3,kecheng.getXuefen());
pstmt.setString(4,kecheng.getCourseType());
pstmt.setString(5,"1");
pstmt.execute();
str="sucess";
}catch(SQLException ex)
{
System.err.println(ex.getMessage());
str="failer";
}
finally
{
try{
if(pstmt!=null){
pstmt.close();
}
if(con!=null){
con.close();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
return str;
}
//更新
public String updateKecheng(Kecheng kecheng)
{
PreparedStatement pstmt=null;
String str="";
try
{
pstmt=con.prepareStatement("update kecheng set courseName=?,xuefen=?,courseType=? where courseId=?");
pstmt.setString(1,kecheng.getCourseName());
pstmt.setInt(2,kecheng.getXuefen());
pstmt.setString(3,kecheng.getCourseType());
pstmt.setString(4,kecheng.getCourseId());
pstmt.execute();
str="sucess";
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
str="failer";
}
finally
{
try{
if(pstmt!=null){
pstmt.close();
}
if(con!=null){
con.close();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
return str;
}
//删除
public String deleteKecheng(String courseId)
{
PreparedStatement pstmt=null;
String str="";
try
{
pstmt=con.prepareStatement("update kecheng set flag=? where courseId=?");
pstmt.setString(1,"2");
pstmt.setString(2,courseId);
pstmt.execute();
str="sucess";
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
str="failer";
}
finally
{
try{
if(pstmt!=null){
pstmt.close();
}
if(con!=null){
con.close();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
return str;
}
//查询
public ResultSet executeQuery(String sql)
{
Statement stmt=null;
ResultSet rst=null;
try{
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rst=stmt.executeQuery(sql);
}catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
return rst;
}
//关闭
public void close()
{
try{
if(con!=null){
con.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
//所有课程
public String getAllCourse()
{
PreparedStatement pstmt = null;
ResultSet rst = null;
StringBuffer sBuf=new StringBuffer();
try {
pstmt = con.prepareStatement("select courseId,courseName from kecheng");
rst = pstmt.executeQuery();
while(rst.next()) {
String courseId= rst.getString("courseId");
String courseName= rst.getString("courseName");
sBuf.append("<option value='"+courseId+"'>"+courseName+"</option>\n");
}
}
catch (SQLException ex) {
return "";
}
finally
{
try{
if(rst!=null){
rst.close();
}
if(pstmt!=null){
pstmt.close();
}
if(con!=null){
con.close();
}
}catch(Exception e)
{
e.printStackTrace();
}
}
return sBuf.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -