本文实例为大家分享了java连接mysql底层封装代码,供大家参考,具体内容如下
连接数据库
package com.dao.db; import java.sql.Connection; import java.sql.SQLException; /** * 数据库连接层MYSQL * @author Administrator * */ public class DBConnection { /** * 连接数据库 * @return */ public static Connection getDBConnection() { // 1. 注册驱动 try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 获取数据库的连接 try { Connection conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/mysql", "root", "root"); return conn; } catch (SQLException e1) { e1.printStackTrace(); } return null; } }
数据层封装
package com.dao.db; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Types; import java.util.ArrayList; import java.util.HashMap; /** * MYSQL数据库底层封装 * @author Administrator * */ public class DBManager { private PreparedStatement pstmt; private Connection conn; private ResultSet rs; /** * 打开数据库 */ public DBManager() { conn = DBConnection.getDBConnection(); } /** * 执行修改添加操作 * @param coulmn * @param type * @param sql * @return * @throws SQLException */ public boolean updateOrAdd(String[] coulmn, int[] type, String sql) throws SQLException { if(!setPstmtParam(coulmn, type, sql)) return false; boolean flag = pstmt.executeUpdate()>0"htmlcode">package com.dao.db; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * 数据集封装 * @author Administrator * */ public class DataTable { public String[] column;//列字段 public String[][] row; //行值 public int rowCount = 0;//行数 public int colCoun = 0;//列数 public DataTable() { super(); } public DataTable(String[] column, String[][] row, int rowCount, int colCoun) { super(); this.column = column; this.row = row; this.rowCount = rowCount; this.colCoun = colCoun; } public void setDataTable(ArrayList<HashMap<String, String list) { rowCount = list.size(); colCoun = list.get(0).size(); column = new String[colCoun]; row = new String[rowCount][colCoun]; for (int i = 0; i < rowCount; i++) { Set<Map.Entry<String, String set = list.get(i).entrySet(); int j = 0; for (Iterator<Map.Entry<String, String it = set.iterator(); it .hasNext();) { Map.Entry<String, String> entry = (Map.Entry<String, String>) it .next(); row[i][j] = entry.getValue(); if (i == rowCount - 1) { column[j] = entry.getKey(); } j++; } } } public String[] getColumn() { return column; } public void setColumn(String[] column) { this.column = column; } public String[][] getRow() { return row; } public void setRow(String[][] row) { this.row = row; } public int getRowCount() { return rowCount; } public void setRowCount(int rowCount) { this.rowCount = rowCount; } public int getColCoun() { return colCoun; } public void setColCoun(int colCoun) { this.colCoun = colCoun; } }测试Demo
package com.bussiness.test; import java.sql.SQLException; import java.sql.Types; import com.dao.db.DBManager; import com.dao.db.DataTable; public class TestBusIness{ static String searchSql = "select * from score"; static String insertSql = "insert into score(name, age, score)values("; static String deleteSql = "delete from score where id = "; static String updateSql = "update score set name = "; public static void main(String[] args) { intsertData(); searchData(); } private static void intsertData() { DBManager dm = new DBManager(); String[] coulmn = new String[]{"wyf2", "23", "89.5"}; int[] type = new int[]{Types.CHAR, Types.INTEGER, Types.DOUBLE}; try { boolean flag = dm.updateOrAdd(coulmn, type, insertSql); if(flag) System.out.println("插入成功"); } catch (SQLException e) { e.printStackTrace(); } } private static void searchData() { DBManager dm = new DBManager(); String[] coulmn = null; int[] type = null; try { DataTable dt = dm.getResultData(coulmn, type, searchSql); if(dt != null && dt.getRowCount()> 0){ for(int i = 0; i<dt.getRowCount(); i++) { for(int j = 0; j<dt.getColCoun(); j++) System.out.printf(dt.getRow()[i][j]+"\t"); System.out.println(); } } else System.out.println("查询失败"); } catch (SQLException e) { e.printStackTrace(); } } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“java连接mysql底层封装详解”评论...
更新日志
2024年11月06日
2024年11月06日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]