java 使用策略模式操作JDBC数据库
程序员文章站
2024-02-20 20:37:04
java 使用策略模式操作jdbc数据库
1:构造一个操作数据库的工具类,可以获得连接和释放连接
public class dbutil {
priva...
java 使用策略模式操作jdbc数据库
1:构造一个操作数据库的工具类,可以获得连接和释放连接
public class dbutil { private static connection conn = null; static { //静态初始块 try { class.forname("com.mysql.jdbc.driver"); conn = drivermanager.getconnection("jdbc:mysql://127.0.0.1:3306/test", "root", "064319"); //初始化获取连接 } catch (classnotfoundexception e) { e.printstacktrace(); } catch (sqlexception e) { e.printstacktrace(); } } /** * 释放连接 * @param rs * @param psmt * @param conn * @throws sqlexception */ public static void closeall(resultset rs, preparedstatement psmt, connection conn) throws sqlexception { if(rs != null) { rs.close(); } if(psmt != null) { psmt.close(); } if(conn != null) { conn.close(); } } /** * 获取连接 * @return */ public static connection getconnection() { return conn; } /** * 根据表的名字来获得表的列信息 * @param tablename */ public static void gettablecolumninfobytablename(string tablename) { connection conn = getconnection(); resultset rs = null; preparedstatement psmt = null; string sql = "select * from " + tablename; try { psmt = conn.preparestatement(sql); rs = psmt.executequery(); resultsetmetadata resultsetmetadata = rs.getmetadata(); for(int i = 1; i<= resultsetmetadata.getcolumncount(); i++) { system.out.println(resultsetmetadata.getcolumnname(i)); } } catch (sqlexception e) { e.printstacktrace(); } finally { try { closeall(rs, psmt, conn); } catch (sqlexception e) { e.printstacktrace(); } } } /** * 根据表的名字来获得表的信息 * @param tablename */ public static void gettableinfobytablename(string tablename) { connection conn = getconnection(); preparedstatement psmt = null; resultset rs = null; string sql = "select * from " + tablename; try { psmt = conn.preparestatement(sql); rs = psmt.executequery(); while(rs.next()) { resultsetmetadata resultsetmetadata = rs.getmetadata(); for(int i = 1; i<= resultsetmetadata.getcolumncount(); i++) { if(i < resultsetmetadata.getcolumncount()) { system.out.print(rs.getobject(resultsetmetadata.getcolumnname(i)) + ", "); } else { system.out.print(rs.getobject(resultsetmetadata.getcolumnname(i))); } } system.out.println(); } } catch (sqlexception e) { e.printstacktrace(); } finally { try { closeall(rs, psmt, conn); } catch (sqlexception e) { e.printstacktrace(); } } } }
2:构造一个操作数据库的basedao类
public class basedao { /** * 根据一些参数来保存相应的对象 * @param sql 要执行的sql语句 * @param params 为sql语句中相应的参数赋值 * @return */ protected boolean saveorupdate(string sql, object[] params) { connection conn = null; preparedstatement psmt = null; boolean flag = false; conn = dbutil.getconnection(); if(conn != null) { try { psmt = conn.preparestatement(sql); for(int i = 1; i <= params.length; i++) { psmt.setobject(i, params[i-1]); } if(psmt.executeupdate() > 0) { flag = true; } } catch (sqlexception e) { e.printstacktrace(); } finally { try { dbutil.closeall(null, psmt, conn); } catch (sqlexception e) { e.printstacktrace(); } } } return flag; } /** * 根据一定的参数获得某个具体的对象 * @param sql 要执行的sql语句 * @param params 为sql语句中相应的参数赋值 * @return */ public object queryforobject(string sql, object[] params, rowmapforobject rowmapforobject) { connection conn = null; preparedstatement psmt = null; conn = dbutil.getconnection(); object obj = null; resultset rs = null; if(conn != null) { try { psmt = conn.preparestatement(sql); if(params != null && params.length > 0) { for(int i = 1; i <= params.length; i++) { psmt.setobject(i, params[i - 1]); } } rs = psmt.executequery(); obj = rowmapforobject.rowmapforobject(rs); } catch (sqlexception e) { e.printstacktrace(); } finally { try { dbutil.closeall(null, psmt, conn); } catch (sqlexception e) { e.printstacktrace(); } } } return obj; } /** * 根据相应的参数获得查询的结果集 * @param sql * @param params * @return */ public list queryforlist(string sql, object[] params, rowmapforlist rowmapforlist) { connection conn = null; preparedstatement psmt = null; conn = dbutil.getconnection(); list list = null; resultset rs = null; if(conn != null) { try { psmt = conn.preparestatement(sql); if(params != null && params.length > 0) { for(int i = 1; i <= params.length; i++) { psmt.setobject(i, params[i - 1]); } } rs = psmt.executequery(sql); list = new arraylist(); list = rowmapforlist.rowmapforlist(rs); } catch (sqlexception e) { e.printstacktrace(); } finally { try { dbutil.closeall(null, psmt, conn); } catch (sqlexception e) { e.printstacktrace(); } } } return list; } }
3:新建一个studentdao类,该类继承自basedao,实现对student的管理
public class studentdao extends basedao { /** * 保存一个student的信息 */ public boolean savestudent(student student) { string sql = "insert into t_student(name, age) values(?, ?)"; object[] params = new object[]{student.getname(), student.getage()}; return super.saveorupdate(sql, params); } /** * 根据id获得一个student的信息 * @param id * @return */ public student getstudentbyid(long id) { string sql = "select * from t_student where id=?"; object[] params = new object[]{id}; return (student)super.queryforobject(sql, params, new rowmapforobject() { public object rowmapforobject(resultset rs) { student student = null; try { if(rs != null && rs.next()) { student = new student(); student.setage(rs.getint(student.age)); student.setid(rs.getlong(student.id)); student.setname(rs.getstring(student.name)); } } catch (sqlexception e) { e.printstacktrace(); } return student; } }); } /** * 获得所有student的信息 * @return */ public list getstudentall() { string sql = "select * from t_student"; list list = super.queryforlist(sql, null, new rowmapforlist() { @override public list rowmapforlist(resultset rs) { list list = null; try { if(rs != null) { list = new arraylist(); while(rs.next()) { student student = new student(); student.setid(rs.getlong("id")); student.setage(rs.getint("age")); student.setname(rs.getstring("name")); list.add(student); } } } catch(sqlexception e) { e.printstacktrace(); } return list; } }); return list; } }
如有疑问请留言或者到本站社区交流讨论,大家共同进步,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: 简述MySQL分片中快速数据迁移
下一篇: MySQLMerge存储引擎