📄 institutiondao.java
字号:
package com.buat.institution;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.buat.connect.Connect;
public class InstitutionDAO implements IInstitutionDAO {
Connect manager=new Connect();
public ArrayList QueyrInstitution() {
ArrayList userlist=new ArrayList();
Connection con=manager.getConnect();
StringBuffer sql=new StringBuffer();
sql.append("select * from institution");
PreparedStatement psmt=null;
ResultSet rs=null;
try {
psmt=con.prepareStatement(sql.toString());
rs=psmt.executeQuery();
while(rs.next()){
Institution inst=new Institution();
inst.setId(rs.getInt("institutionid"));
inst.setName(rs.getString("name"));
inst.setReason(rs.getString("reason"));
inst.setExplains(rs.getString("explains"));
userlist.add(inst);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(psmt !=null){
psmt.close();
}
if(con !=null){
con.close();
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
return userlist;
}
public boolean AddInstitution(Institution inst) {
Connection con=manager.getConnect();
boolean scu=false;
StringBuffer sql=new StringBuffer();
sql.append("insert into institution (name, reason,explains)");
sql.append(" values( ?, ?, ?)");
PreparedStatement psmt=null;
try {
psmt=con.prepareStatement(sql.toString());
psmt.setString(1,inst.getName());
psmt.setString(2,inst.getReason());
psmt.setString(3,inst.getExplains());
int rs=psmt.executeUpdate();
if(rs>0){
scu=true;
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return scu;
}
public boolean DeleteInstitution(int id) {
Connection con=manager.getConnect();
boolean successful=false;
StringBuffer sql=new StringBuffer();
sql.append("delete from institution where institutionid= ?");
PreparedStatement psmt=null;
try {
psmt=con.prepareStatement(sql.toString());
psmt.setInt(1,id);
int rs=psmt.executeUpdate();
if(rs>0){
successful=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(psmt !=null){
psmt.close();
}
if(con !=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return successful;
}
public boolean UpdateInstitution(Institution inst, int id) {
Connection con=manager.getConnect();
boolean successful=false;
StringBuffer sql=new StringBuffer();
sql.append("update institution set name= ? ,reason = ?,explains=? where institutionid=?");
PreparedStatement psmt=null;
try {
psmt=con.prepareStatement(sql.toString());
psmt.setString(1,inst.getName());
psmt.setString(2,inst.getReason());
psmt.setString(3,inst.getExplains());
psmt.setInt(4,id);
int rs=psmt.executeUpdate();
if(rs>0){
successful=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(psmt !=null){
psmt.close();
}
if(con !=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return successful;
}
public int QueryCountInstitution() {
int count=0;
Connection con=manager.getConnect();
StringBuffer sql=new StringBuffer();
sql.append("select count(*) from institution");
PreparedStatement psmt=null;
try {
psmt=con.prepareStatement(sql.toString());
ResultSet rs=null;
rs=psmt.executeQuery();
while(rs.next()){
count=rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
public ArrayList QueryInstitutionlimit(int beginIndex, int endIndex) {
ArrayList list=new ArrayList();
int maxlengh= (endIndex - beginIndex) + 1;
Connection con=manager.getConnect();
StringBuffer sql=new StringBuffer();
sql.append("select * from institution order by institutionid desc limit ?,?");
PreparedStatement psmt=null;
try {
psmt=con.prepareStatement(sql.toString());
psmt.setInt(1,beginIndex);
psmt.setInt(2,maxlengh);
ResultSet rs=null;
rs=psmt.executeQuery();
while(rs.next()){
Institution inst=new Institution();
inst.setId(rs.getInt(1));
inst.setName(rs.getString(2));
inst.setReason(rs.getString(3));
inst.setExplains(rs.getString(4));
list.add(inst);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public static void main(String args[]){
InstitutionDAO dao= new InstitutionDAO();
System.out.println(dao.QueryCountInstitution());
System.out.println(dao.QueryInstitutionlimit(1,3));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -