java连接mysql底层封装详解
本文实例为大家分享了java连接mysql底层封装代码,供大家参考,具体内容如下
连接数据库
packagecom.dao.db; importjava.sql.Connection; importjava.sql.SQLException; /** *数据库连接层MYSQL *@authorAdministrator * */ publicclassDBConnection{ /** *连接数据库 *@return */ publicstaticConnectiongetDBConnection() { //1.注册驱动 try{ Class.forName("com.mysql.jdbc.Driver"); }catch(ClassNotFoundExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } //获取数据库的连接 try{ Connectionconn=java.sql.DriverManager.getConnection("jdbc:mysql://localhost/mysql?useUnicode=true&characterEncoding=utf-8","root","root"); returnconn; }catch(SQLExceptione1){ e1.printStackTrace(); } returnnull; } }
数据层封装
packagecom.dao.db; importjava.sql.Connection; importjava.sql.PreparedStatement; importjava.sql.ResultSet; importjava.sql.ResultSetMetaData; importjava.sql.SQLException; importjava.sql.Types; importjava.util.ArrayList; importjava.util.HashMap; /** *MYSQL数据库底层封装 *@authorAdministrator * */ publicclassDBManager{ privatePreparedStatementpstmt; privateConnectionconn; privateResultSetrs; /** *打开数据库 */ publicDBManager(){ conn=DBConnection.getDBConnection(); } /** *执行修改添加操作 *@paramcoulmn *@paramtype *@paramsql *@return *@throwsSQLException */ publicbooleanupdateOrAdd(String[]coulmn,int[]type,Stringsql)throwsSQLException { if(!setPstmtParam(coulmn,type,sql)) returnfalse; booleanflag=pstmt.executeUpdate()>0?true:false; closeDB(); returnflag; } /** *获取查询结果集 *@paramcoulmn *@paramtype *@paramsql *@throwsSQLException */ publicDataTablegetResultData(String[]coulmn,int[]type,Stringsql)throwsSQLException { DataTabledt=newDataTable(); ArrayList>list=newArrayList >(); if(!setPstmtParam(coulmn,type,sql)) returnnull; rs=pstmt.executeQuery(); ResultSetMetaDatarsmd=rs.getMetaData();//取数据库的列名 intnumberOfColumns=rsmd.getColumnCount(); while(rs.next()) { HashMap rsTree=newHashMap (); for(intr=1;r 数据集封装
packagecom.dao.db; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.Iterator; importjava.util.Map; importjava.util.Set; /** *数据集封装 *@authorAdministrator * */ publicclassDataTable{ publicString[]column;//列字段 publicString[][]row;//行值 publicintrowCount=0;//行数 publicintcolCoun=0;//列数 publicDataTable(){ super(); } publicDataTable(String[]column,String[][]row,introwCount,intcolCoun){ super(); this.column=column; this.row=row; this.rowCount=rowCount; this.colCoun=colCoun; } publicvoidsetDataTable(ArrayList>list){ rowCount=list.size(); colCoun=list.get(0).size(); column=newString[colCoun]; row=newString[rowCount][colCoun]; for(inti=0;i >set=list.get(i).entrySet(); intj=0; for(Iterator >it=set.iterator();it .hasNext();){ Map.Entry entry=(Map.Entry )it .next(); row[i][j]=entry.getValue(); if(i==rowCount-1){ column[j]=entry.getKey(); } j++; } } } publicString[]getColumn(){ returncolumn; } publicvoidsetColumn(String[]column){ this.column=column; } publicString[][]getRow(){ returnrow; } publicvoidsetRow(String[][]row){ this.row=row; } publicintgetRowCount(){ returnrowCount; } publicvoidsetRowCount(introwCount){ this.rowCount=rowCount; } publicintgetColCoun(){ returncolCoun; } publicvoidsetColCoun(intcolCoun){ this.colCoun=colCoun; } } 测试Demo
packagecom.bussiness.test; importjava.sql.SQLException; importjava.sql.Types; importcom.dao.db.DBManager; importcom.dao.db.DataTable; publicclassTestBusIness{ staticStringsearchSql="select*fromscore"; staticStringinsertSql="insertintoscore(name,age,score)values(?,?,?)"; staticStringdeleteSql="deletefromscorewhereid=?"; staticStringupdateSql="updatescoresetname=?whereid=?"; publicstaticvoidmain(String[]args){ intsertData(); searchData(); } privatestaticvoidintsertData() { DBManagerdm=newDBManager(); String[]coulmn=newString[]{"wyf2","23","89.5"}; int[]type=newint[]{Types.CHAR,Types.INTEGER,Types.DOUBLE}; try{ booleanflag=dm.updateOrAdd(coulmn,type,insertSql); if(flag) System.out.println("插入成功"); }catch(SQLExceptione){ e.printStackTrace(); } } privatestaticvoidsearchData() { DBManagerdm=newDBManager(); String[]coulmn=null; int[]type=null; try{ DataTabledt=dm.getResultData(coulmn,type,searchSql); if(dt!=null&&dt.getRowCount()>0){ for(inti=0;i以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。