您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

数据库jdbc封装

2025/10/14 20:25:24发布14次查看
经过几天的努力终于搞好了,这个类可以不用管数据库字段,不用写dao类,直接map添加,添加删除和修改,jdbc链接,分享给大家,用的话记得加上连接池,尊重原创,转载请注明package jdbc;import java.sql.connection;import java.sql.drivermanager;import java.sql.preparedstatement;import java.sql.resultset;import java.util.arraylist;import java.util.hashmap;import java.util.list;import java.util.map;/** * 操作数据库工具类 * * */public class domain { /** * 连接数据 * * @return conn */ public static connection getconnection(string url, string username, string password) { connection conn = null; try { class.forname("com.mysql.jdbc.driver"); conn = drivermanager.getconnection("jdbc:mysql://localhost:3306/" + url, username, password); } catch (exception e) { e.printstacktrace(); } return conn; } /** * 关闭连接对象 * * @param conn * 连接对象 * @param pstmt * 预编译对象 * @param rs * 结果集 */ public static void closeall(connection conn, preparedstatement pstmt, resultset rs) { try { if (rs != null) { rs.close(); } if (pstmt != null) { pstmt.close(); } if (conn != null) { conn.close(); } } catch (exception e) { e.printstacktrace(); } } /** * 增删改操作 * * @param sql * sql命令 * @param param * 参数 * @return */ public static int executupdate(connection conn, string sql, object[] param) { int result = 0; preparedstatement pstmt = null; try { system.out.println(sql); pstmt = conn.preparestatement(sql); if (param != null) { for (int i = 0; i < param.length; i++) { pstmt.setobject(i + 1, param[i]); } } result = pstmt.executeupdate(); } catch (exception e) { e.printstacktrace(); } finally { closeall(conn, pstmt, null); } return result; } /** * 查询 * * @return int * @date 2015-7-25 上午11:10:06 */ public static resultset executquery(connection conn, string sql, string[] param) { preparedstatement pstmt = null; resultset result = null; try { pstmt = conn.preparestatement(sql); if (param != null) { for (int i = 0; i < param.length; i++) { pstmt.setstring(i + 1, param[i]); } } result = pstmt.executequery(); } catch (exception e) { e.printstacktrace(); } return result; } /** * 简单增删改sql语句执行,复杂sql执行executupdate方法 * 字符串类型需要加单引号 * @param url * 数据库 * @param username * 数据库用户名 * @param password * 数据库密码 * @param map * 参数列,值 * @param tablename * 表名 * @param typesql * sql类型 insert,dell,update * @param oldline * 删除时为要删除的列名,更新的时候为where前的条件列名(仅当且使用dell和update时填写,insert时不作用) * @param newline * 更新时where候的条件列名(仅当且使用update时填写,dell,insert时不作用) * @param oldcondition * 删除时为要删除的条件,更新的时候为where前的条件(仅当且使用dell和update时填写,insert时不作用) * @param newcondition * 更新的时候为where后的条件(仅当且使用update时填写,dell,insert时不作用) */ public static void sql(string url, string username, string password, map<string, object> map, string tablename, string typesql, string oldline, string oldcondition, string newline, string newcondition) { string sql = ""; connection conn = getconnection(url, username, password); object[] valuearray = null; if (typesql.equals("insert")) { list<object> key = new arraylist<object>(); list<object> value = new arraylist<object>(); stringbuffer sb = new stringbuffer(); stringbuffer wen = new stringbuffer(); for (object string : map.keyset()) { key.add(string); value.add(map.get(string)); } object[] keyarray = key.toarray(); valuearray = value.toarray(); for (int i = 0; i < keyarray.length; i++) { sb.append(keyarray[i] + ","); wen.append("?,"); } string string = sb.tostring(); string = string.substring(0, string.length() - 1); string wenstr = wen.tostring(); wenstr = wenstr.substring(0, wenstr.length() - 1); sql = "insert into " + tablename + "(" + string + ") values (" + wenstr + ")"; } else if (typesql.equals("dell")) { sql = "delete from " + tablename + " where " + oldline + " = " + oldcondition; } else if (typesql.equals("update")) { sql = "update " + tablename + " set " + oldline + "= " + oldcondition + " where " + newline + " = " + newcondition; } int executupdate = executupdate(conn, sql + ";", valuearray); system.out.println(executupdate); }//测试 public static void main(string[] args) { map<string, object> map = new hashmap<string, object>(); map.put("appid", "lisi"); for (int i = 0; i < 100; i++) { system.out.println(i); map.put("id",i); // 增加 sql("test", "root", "", map, "user", "insert", "", "", "", ""); // 删除 sql("test", "root", "", map, "user", "dell", "id", i+"","", ""); // 修改// sql("test", "root", "", map, "user", "update", "appid", "'zhang'", "id", "0"); } // map.put("password", "123"); } }
以上就是数据库jdbc封装的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product