📄 typedaoimp.java
字号:
package com.myoa.cal.imp;
import com.myoa.cal.dao.*;
import com.myoa.cal.model.CalTypes;
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 org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class TypeDAOImp implements TypeDAO{
public int addType(CalTypes type) {
Connection conn=null;
PreparedStatement stmt=null;
String sql="insert into cal_types(userid,isglobal,eventype) values(?,?,?)";
int typeid = 0;
try {
conn=new DAOFactory().getDBFacotryDAO().get_DBMysql();
stmt=conn.prepareStatement(sql);
stmt.setInt(1, type.getUserid());
stmt.setInt(2, 0);
stmt.setString(3, type.getEventype());
stmt.executeUpdate();
ResultSet res=stmt.getGeneratedKeys();
while(res.next()){
typeid=res.getInt(1);
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
new DAOFactory().getDBFacotryDAO().closeConnection(conn);
new DAOFactory().getDBFacotryDAO().closeStatement(stmt);
}
return typeid;
}
public void deletType(Integer id) {
System.out.println(id);
Connection conn=null;
PreparedStatement stmt=null;
String sql="delete from cal_types where typeid=?";
try {
conn=new DAOFactory().getDBFacotryDAO().get_DBMysql();
stmt=conn.prepareStatement(sql);
stmt.setInt(1,(int)id);
stmt.executeUpdate();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
new DAOFactory().getDBFacotryDAO().closeConnection(conn);
new DAOFactory().getDBFacotryDAO().closeStatement(stmt);
}
}
public String getAllType(Integer userid) {
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
JSONArray array = new JSONArray();
String sql="select * from cal_types where userid=? or isglobal=1";
try {
conn=new DAOFactory().getDBFacotryDAO().get_DBMysql();
stmt=conn.prepareStatement(sql);
stmt.setInt(1,userid);
rs=stmt.executeQuery();
while(rs.next()){
JSONObject obj = new JSONObject();
obj.put("typeid", rs.getInt("typeid"));
obj.put("userid",rs.getInt("userid"));
obj.put("eventype", rs.getString("eventype"));
array.add(obj);
}
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
new DAOFactory().getDBFacotryDAO().closeResultSet(rs);
new DAOFactory().getDBFacotryDAO().closeConnection(conn);
new DAOFactory().getDBFacotryDAO().closeStatement(stmt);
}
return array.toString();
}
public void updateType(CalTypes type) {
Connection conn=null;
PreparedStatement stmt=null;
String sql="update cal_types set eventype=? where typeid=?";
try {
conn=new DAOFactory().getDBFacotryDAO().get_DBMysql();
stmt=conn.prepareStatement(sql);
stmt.setString(1,type.getEventype());
stmt.setInt(2, type.getTypeid());
stmt.executeUpdate();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally{
new DAOFactory().getDBFacotryDAO().closeConnection(conn);
new DAOFactory().getDBFacotryDAO().closeStatement(stmt);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -