📄 sharedao.java
字号:
package com.buat.hr.share;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public abstract class ShareDAO implements IShareDAO{
protected DBConnection db=new DBConnection();
//根据Id删除表中信息
public boolean deleteByid(String tableName,int id) {
boolean flag=false;
Connection con=null;
PreparedStatement ps=null;
String str=null;
con=db.getConnection();
str="delete from "+tableName+" where id="+id;
try {
ps=con.prepareStatement(str);
ps.executeUpdate();
flag=true;
} catch (SQLException e) {
flag=false;
}finally{
try {
if(ps!=null){
ps.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
}
}
return flag;
}
public boolean deleteByid(String id,String tableName,String colName) {
boolean flag=false;
Connection con=null;
PreparedStatement ps=null;
String str=null;
int row=0;
con=db.getConnection();
str="delete from "+tableName+" where "+colName+"="+id;
System.out.print("sql="+str.toString());
try {
ps=con.prepareStatement(str);
row=ps.executeUpdate();
if(row>0){
flag=true;
}
} catch (SQLException e) {
}finally{
try {
if(ps!=null){
ps.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
}
}
return flag;
}
/*
* 作用:向表中添加记录
* 注:
* sql——用于保存新增记录的sql语句
*
* str——保存sql语句
*
* flag——用语判断删除是否成功
* true:删除成功
*
*/
public boolean add(String sql) {
boolean flag=false;
Connection con=null;
PreparedStatement ps=null;
con=db.getConnection();
try {
ps=con.prepareStatement(sql);
ps.executeUpdate();
flag=true;
} catch (SQLException e) {
flag=false;
}finally{
try {
if(ps!=null){
ps.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
}
}
return flag;
}
/*
* 作用:修改表中记录
* 注:
* sql——用于保存新增记录的sql语句
* flag——用语判断删除是否成功
* true:删除成功
*
*/
public boolean update(String sql) {
boolean flag=false;
Connection con=null;
PreparedStatement ps=null;
con=db.getConnection();
try {
ps=con.prepareStatement(sql);
ps.executeUpdate();
flag=true;
} catch (SQLException e) {
flag=false;
}finally{
try {
if(ps!=null){
ps.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
}
}
return flag;
}
//得到表中总数
public int getCount(String sql) {
int count=0;
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
con=db.getConnection();
try {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
count=rs.getShort(1);
}
} catch (SQLException e) {
}finally{
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return count;
}
public Date Time(){ //得到系统时间
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select current_date() as date";
con=db.getConnection();
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
rs.next();
String date=rs.getString("date");
return Date.valueOf(date);
}catch(Exception e){return null;}
}
public abstract ArrayList query(int startNo, int perCount, String tableName); //查询部分记录(用于分页)
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -