/**
* 查詢數據列表
* @param sql 查詢SQL語句
* @param queryColum 要查詢的字段
* @param type 查詢類型 當type = "count" 爲查詢總條數
* @throws SQLException
*/
public static List<Map<String,Object>> query(String sql,String queryColum,String type,String timeColum) throws SQLException{
Connection conn = DbUtils.getConnection();
PreparedStatement st = conn.prepareStatement(sql);
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
ResultSet result = st.executeQuery(sql);
if("count".equals(type)){//查詢總條數
while(result.next()){
Map<String,Object> map = new HashMap<String, Object>();
String total = result.getString("total");
map.put("total", total);
list.add(map);
}
}
if(null !=queryColum && !"".equals(queryColum)){
while(result.next()){
Map<String,Object> map = new HashMap<String, Object>();
String[] clounName = queryColum.split(",");
for (String s : clounName) {
if(s.trim().equals(timeColum)){
long ll = result.getLong(s);
map.put(s, Tools.transLong2Date(ll));
}else{
map.put(s, result.getString(s).replaceAll("<", "").replaceAll(">", ""));
}
}
list.add(map);
}
}
result.close();
st.close();
conn.close();
return list;
}
/**
* 更新
* @param string
* @param type 當type 爲del是不拆分
* @param updateColumnValue
* @throws SQLException
*/
public static int update(String sql, String updateColumn,String type) throws SQLException {
Connection conn = DBAccesser.getMachineConnection();
PreparedStatement ps = null;
int status = 0;
if(null != updateColumn && !"".equals(updateColumn)){
ps = conn.prepareStatement(sql);
if(!"del".equals(type)){
String[] array = updateColumn.split(",");
for (int i = 0; i < array.length; i++) {
ps.setString(i+1, array[i]);
}
}else{
ps.setString(1, updateColumn);
}
status = ps.executeUpdate();
}
ps.close();
conn.close();
return status;
}
public static int update2(String sql) throws SQLException{
Connection conn = DBAccesser.getMachineConnection();
Statement ps = null;
int i =0;
try {
ps = conn.createStatement();
i = ps.executeUpdate(sql);
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
Jdbc 增刪改查工具方法
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
java 學習泛型遇到的問題
爱上学习
2018-08-27 20:04:12
JSP 中的2種包含(導入)
爱上学习
2018-08-27 20:03:43
HashMap部分源碼閱讀,主要是put方法相關的部分
HeiShuaiGuo
2018-08-27 20:03:32
public String(byte[] bytes, int offset, int length)
Myrkyn
2018-08-27 20:03:04
jstl標籤 fmt:formatDate 設置當前時間顯示星期幾
HaHa_Sir
2018-08-27 20:02:59
Java 反射,reflect
crossy
2018-08-27 20:02:58
JVM的內存區域劃分
相由心生fhy
2018-08-27 20:02:39
SpringMVC使用ajax 實現文件下載
HaHa_Sir
2018-08-27 20:02:23
java寫一個簡單的jdbc數據庫連接池
HaHa_Sir
2018-08-27 20:02:23
Servlet輸出JSON數據Servlet向瀏覽器返回JSON數據
HaHa_Sir
2018-08-27 20:02:22
原生Servlet文件上傳和下載Servlet多個文件上傳
HaHa_Sir
2018-08-27 20:02:22
GET和POST請求的區別
HaHa_Sir
2018-08-27 20:02:21
java Object 類中clone方法用法記錄
HaHa_Sir
2018-08-27 20:02:21
SpringMVC重定向參數中文丟失和MyBatis關聯查詢時問題總結
HaHa_Sir
2018-08-27 20:02:21
java註解學習---@Inherited註解的理解學習(四)
HaHa_Sir
2018-08-27 20:02:09