📄 educatedao.java
字号:
package com.buat.educate;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.*;
import com.buat.connect.Connect;
public class EducateDAO implements IEducateDAO {
public boolean addEducate(Educate educate) {//添加
boolean successful = false;
Connection con = Connect.getConnect();
StringBuffer sbf = new StringBuffer();
sbf.append("insert into educate(name,purpose,begintime,endtime,teacher,personnel,material)");
sbf.append("values(?,?,?,?,?,?,?)");
PreparedStatement pst = null;
try {
pst = con.prepareStatement(sbf.toString());
pst.setString(1, educate.getName());
pst.setString(2, educate.getPurpose());
pst.setString(3, educate.getBegintime());
pst.setString(4, educate.getEndtime());
pst.setString(5, educate.getTeacher());
pst.setString(6, educate.getPersonnel());
pst.setString(7, educate.getMaterial());
int mm = pst.executeUpdate();
if (mm > 0) {
successful = true;
//JOptionPane.showMessageDialog(null,"添加成功");
} else {
//JOptionPane.showMessageDialog(null,"添加失败");
successful=false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return successful;
}
public ArrayList queryEducate(int beginIndex, int endIndex) {//分页查询
ArrayList educateList = new ArrayList();
Connection con = Connect.getConnect();
StringBuffer sbf = new StringBuffer();
sbf.append("select * from educate order by Id desc limit ?,?");
PreparedStatement pst = null;
try {
pst = con.prepareStatement(sbf.toString());
pst.setInt(1,beginIndex);//第一个数
int maxLangth = endIndex - beginIndex + 1;
pst.setInt(2, maxLangth);//最后一个数
ResultSet rs = pst.executeQuery();
rs.beforeFirst();
while (rs.next()) {
Educate educate = new Educate();
educate.setId(rs.getInt("Id"));
educate.setName(rs.getString("name"));
educate.setPurpose(rs.getString("purpose"));
educate.setBegintime(rs.getString("begintime"));
educate.setEndtime(rs.getString("endtime"));
educate.setTeacher(rs.getString("teacher"));
educate.setPersonnel(rs.getString("personnel"));
educate.setMaterial(rs.getString("material"));
educate.setEffect(rs.getString("effect"));
educate.setSummarize(rs.getString("summarize"));
educateList.add(educate);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return educateList;
}
public ArrayList queryEducatet(int id) {//查询所有
ArrayList educateList = new ArrayList();
Connection con = Connect.getConnect();
StringBuffer sbf = new StringBuffer();
sbf.append("select * from educate where Id=?");
PreparedStatement pst = null;
try {
pst = con.prepareStatement(sbf.toString());
pst.setInt(1, id);
ResultSet rs = pst.executeQuery();
rs.beforeFirst();
while (rs.next()) {
Educate educate = new Educate();
educate.setId(rs.getInt("Id"));
educate.setName(rs.getString("name"));
educate.setPurpose(rs.getString("purpose"));
educate.setBegintime(rs.getString("begintime"));
educate.setEndtime(rs.getString("endtime"));
educate.setTeacher(rs.getString("teacher"));
educate.setPersonnel(rs.getString("personnel"));
educate.setMaterial(rs.getString("material"));
educate.setEffect(rs.getString("effect"));
educate.setSummarize(rs.getString("summarize"));
educateList.add(educate);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return educateList;
}
public boolean deleteEducate(int id) {//按id删除
boolean successful = false;
Connection con = Connect.getConnect();
StringBuffer sbf = new StringBuffer();
sbf.append("delete from educate where Id=?");
PreparedStatement pst = null;
try {
pst = con.prepareStatement(sbf.toString());
pst.setInt(1, id);
int mm = pst.executeUpdate();
if (mm > 0) {
successful = true;
System.out.println("删除成功");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return successful;
}
public boolean updateEducate(int id, Educate educate) {//修改
boolean successful = false;
Connection con = Connect.getConnect();
StringBuffer sbf = new StringBuffer();
sbf.append("update educate set effect=?,summarize=? where Id=?");
PreparedStatement pst = null;
try {
pst = con.prepareStatement(sbf.toString());
pst.setString(1, educate.getEffect());
pst.setString(2, educate.getSummarize());
pst.setInt(3,educate.getId());
int mm = pst.executeUpdate();
if (mm > 0) {
successful = true;
System.out.println(" 修改成功");
}
else{
successful =false;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return successful;
}
public int QueryAllAnnalNumber() {//查询表中信息个数
PreparedStatement ps = null;
ResultSet rs = null;
int number = 0;
Connection con = Connect.getConnect();
try {
ps = con.prepareStatement("select count(*) from educate");
rs = ps.executeQuery();
while (rs.next()) {
number = rs.getInt(1);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
ps.close();
if (con != null)
con.close();
} catch (Exception e) {
}
}
return number;
}
public static void main(String []args){
System.out.println(new EducateDAO().QueryAllAnnalNumber());
}
public ArrayList queryEducateSummarize(int beginIndex, int endIndex) {
ArrayList educateList = new ArrayList();
Connection con = Connect.getConnect();
StringBuffer sbf = new StringBuffer();
sbf.append("select * from educate where effect not in('') order by Id desc limit ?,?");
PreparedStatement pst = null;
try {
pst = con.prepareStatement(sbf.toString());
pst.setInt(1,beginIndex);//第一个数
int maxLangth = endIndex - beginIndex + 1;
pst.setInt(2, maxLangth);//最后一个数
ResultSet rs = pst.executeQuery();
rs.beforeFirst();
while (rs.next()) {
Educate educate = new Educate();
educate.setId(rs.getInt("Id"));
educate.setName(rs.getString("name"));
educate.setPurpose(rs.getString("purpose"));
educate.setBegintime(rs.getString("begintime"));
educate.setEndtime(rs.getString("endtime"));
educate.setTeacher(rs.getString("teacher"));
educate.setPersonnel(rs.getString("personnel"));
educate.setMaterial(rs.getString("material"));
educate.setEffect(rs.getString("effect"));
educate.setSummarize(rs.getString("summarize"));
educateList.add(educate);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pst != null) {
try {
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return educateList;
}
public int QueryAllAnnalNumberSummarize() {
PreparedStatement ps = null;
ResultSet rs = null;
int number = 0;
Connection con = Connect.getConnect();
try {
ps = con.prepareStatement("select count(*) from educate where effect not in('')");
rs = ps.executeQuery();
while (rs.next()) {
number = rs.getInt(1);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
ps.close();
if (con != null)
con.close();
} catch (Exception e) {
}
}
return number;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -