JDBC閫氱敤鐨勬暟鎹簱鏁版嵁鎿嶄綔[CRUD]灏佽
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC閫氱敤鐨勬暟鎹簱鏁版嵁鎿嶄綔[CRUD]灏佽相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/false' title='false'>false tar column tin statement 椤电爜 ack 鏌ヨ turn
package com.lyang.dao;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.lyang.util.PageInfo;
import com.lyang.util.JdbcUtil;
/**
* 閫氱敤鐨勬暟鎹簱鏁版嵁鎿嶄綔绫? */
public class BaseDao {
/**
* 閫氱敤鏌ヨ鏁版嵁鍒楄〃
* @param <T> 鍏蜂綋鐨勭被
* @param sql sql鎸囦护
* @param cls 鍙嶅皠绫? * @param param 鍙傛暟
* @return
*/
public <T> List<T> selectList(String sql, Class<T> cls, Object... param) {
Connection conn = JdbcUtil.getConn();
PreparedStatement prep = null;
ResultSet rs = null;
List<T> data = new ArrayList<T>();
try {
prep = conn.prepareStatement(sql);
//寰幆璁剧疆鍙傛暟
for (int i = 0; i < param.length; i++) {
prep.setObject(i + 1, param[i]);
}
//鏌ヨ缁撴灉
rs = prep.executeQuery();
while (rs.next()) {
T bean = rsToBean(rs, cls);
data.add(bean);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtil.close(conn, prep, rs);
}
return data;
}
/**
* 鏌ヨ涓€鏉℃暟鎹? * @param <T> 鍏蜂綋鐨勭被
* @param sql sql鎸囦护
* @param cls 鍙嶅皠绫? * @param param 鍙傛暟
* @return
*/
public <T> T selectOne(String sql, Class<T> cls, Object... param) {
List<T> list = this.selectList(sql, cls, param);
if (!list.isEmpty() && list.size() == 1) {
return list.get(0);
}
return null;
}
/**
* 灏哛esultSet 杞寲涓轰换鎰忓搴旂殑JavaBean
* @param <T> 鍏蜂綋鐨勭被
* @param rs 缁撴灉闆? * @param cls 鍙嶅皠绫? * @return
*/
public <T> T rsToBean(ResultSet rs, Class<T> cls) {
T t = null;
try {
t = cls.newInstance();
// 鑾峰彇鏌ヨ缁撴灉鐨勫厓鏁版嵁
ResultSetMetaData metaData = rs.getMetaData();
// 鑾峰彇鍒楃殑涓暟 鏌ヨ缁撴灉鐨勫垪鐨勪釜鏁? int columnCount = metaData.getColumnCount();
for (int i = 0; i < columnCount; i++) {
// 鑾峰彇鍒楀悕绉? String columnLabel = metaData.getColumnLabel(i + 1);
// 鑾峰彇璇ュ垪瀵瑰簲鐨勫€? Object value = rs.getObject(columnLabel);
// 鏍规嵁缁撴灉鍒楀悕绉?鏌ユ壘瀵瑰簲灞炴€? Field field = cls.getDeclaredField(columnLabel);
// 璁剧疆鏉冮檺锛屽洜涓哄睘鎬ч兘涓虹鏈夌殑锛屾棤娉曠洿鎺ヨ幏鍙? field.setAccessible(true);
// 涓哄睘鎬ц缃€? field.set(t, value);
}
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
/**
* 閫氱敤鍒犻櫎
* @param sql sql鎸囦护
* @return
*/
public boolean deleteById(String sql) {
Connection conn = JdbcUtil.getConn();
PreparedStatement prep = null;
try {
prep = conn.prepareStatement(sql);
int m = prep.executeUpdate();
return m == 1 ? true : false;
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(conn, prep);
}
return false;
}
/**
* 鍚戣〃鎻掑叆鏁版嵁 杩斿洖 鑷娑ㄤ富閿€? * @param sql sql鎸囦护
* @param param 鍙傛暟
* @return
*/
public int insert(String sql, Object... param) {
Connection conn = JdbcUtil.getConn();
PreparedStatement prep = null;
ResultSet rs = null;
try {
// PreparedStatement.RETURN_GENERATED_KEYS锛氳繑鍥炵敓鎴愮殑key
prep = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
for (int i = 0; i < param.length; i++) {
prep.setObject(i + 1, param[i]);
}
prep.executeUpdate();
// 鐢熸垚鐨勪富閿? rs = prep.getGeneratedKeys();
if (rs != null) {
rs.next();
return rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(conn, prep);
}
return 0;
}
/**
* 閫氱敤鏇存柊
* @param sql sql鎸囦护
* @param param 鍙傛暟
* @return
*/
public boolean update(String sql, Object... param) {
Connection conn = JdbcUtil.getConn();
PreparedStatement prep = null;
try {
prep = conn.prepareStatement(sql);
for (int i = 0; i < param.length; i++) {
prep.setObject(i + 1, param[i]);
}
int m = prep.executeUpdate();
return m > 0 ? true : false;
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(conn, prep);
}
return false;
}
/**
* 鍒嗛〉鏌ヨ
* @param <T> 鍏蜂綋鐨勭被
* @param sql sql鎸囦护
* @param page 椤电爜
* @param limit 姣忛〉鏉℃暟
* @param cls 鍙嶅皠绫? * @return
*/
public <T> PageInfo<T> selectPage(String sql, Integer page, Integer limit, Class<T> cls) {
// 鎬昏鏁? int total = this.selectCount(sql);
// 鏈€澶ч〉鐮? int maxPage = total % limit == 0 ? total / limit : total / limit + 1;
// 濡傛灉page姣旀渶澶ч〉鐮佸ぇ 鍒欏€间负鏈€澶ч〉鐮? if (page > maxPage) {
page = maxPage;
}
// 濡傛灉page鍊煎皬浜? 鍒欎负1
if (page < 1) {
page = 1;
}
int startIndex = (page - 1) * limit;
sql = sql + " limit " + startIndex + "," + limit;
// 鏌ヨ鏁版嵁
List<T> data = this.selectList(sql, cls);
PageInfo<T> pageInfo = new PageInfo<T>(page, limit, total, maxPage, data);
return pageInfo;
}
/**
* 绗﹀悎鏉′欢鐨勬€昏鏁? * @param sql sql鎸囦护
* @return
*/
private int selectCount(String sql) {
sql = "select count(1) from (" + sql + ") as rs";
Connection conn = JdbcUtil.getConn();
PreparedStatement prep = null;
ResultSet rs = null;
try {
prep = conn.prepareStatement(sql);
rs = prep.executeQuery();
rs.next();
return rs.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(conn, prep, rs);
}
return 0;
}
}
以上是关于JDBC閫氱敤鐨勬暟鎹簱鏁版嵁鎿嶄綔[CRUD]灏佽的主要内容,如果未能解决你的问题,请参考以下文章
koa,express,node閫氱敤鏂规硶杩炴帴MongoDB
鏁版嵁搴撶瑪璁?馃搾 MySQL鍩虹鎿嶄綔鍛戒护 鍒嗙被娓呮櫚