博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java连接mysql底层封装
阅读量:5316 次
发布时间:2019-06-14

本文共 6219 字,大约阅读时间需要 20 分钟。

1 package com.dao.db; 2  3 import java.sql.Connection; 4 import java.sql.SQLException; 5  6 /** 7  * 数据库连接层MYSQL 8  * @author Administrator 9  *10  */11 public class DBConnection {12     13     14     /**15      * 连接数据库16      * @return17      */18     public static Connection getDBConnection()19     {20         // 1. 注册驱动21         try {22             Class.forName("com.mysql.jdbc.Driver");23         } catch (ClassNotFoundException e) {24             // TODO Auto-generated catch block25             e.printStackTrace();26         }27         // 获取数据库的连接28         try {29             Connection conn  = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/mysql?useUnicode=true&characterEncoding=utf-8", "root", "root");30             return conn;31         } catch (SQLException e1) {32             e1.printStackTrace();33         }34         return null;35     }36     37 }
连接数据库
1 package com.dao.db;  2   3 import java.sql.Connection;  4 import java.sql.PreparedStatement;  5 import java.sql.ResultSet;  6 import java.sql.ResultSetMetaData;  7 import java.sql.SQLException;  8 import java.sql.Types;  9 import java.util.ArrayList; 10 import java.util.HashMap; 11  12 /** 13  * MYSQL数据库底层封装 14  * @author Administrator 15  * 16  */ 17 public class DBManager { 18      19     private PreparedStatement pstmt; 20     private Connection conn; 21     private ResultSet rs; 22      23  24     /** 25      * 打开数据库 26      */ 27     public DBManager() { 28         conn = DBConnection.getDBConnection(); 29     } 30      31     /** 32      * 执行修改添加操作 33      * @param coulmn 34      * @param type 35      * @param sql 36      * @return 37      * @throws SQLException 38      */ 39     public  boolean updateOrAdd(String[] coulmn, int[] type, String sql) throws SQLException 40     { 41         if(!setPstmtParam(coulmn, type, sql)) 42             return false; 43         boolean flag = pstmt.executeUpdate()>0?true:false; 44         closeDB(); 45         return flag; 46     } 47     /** 48      * 获取查询结果集 49      * @param coulmn 50      * @param type 51      * @param sql 52      * @throws SQLException 53      */ 54     public DataTable getResultData(String[] coulmn, int[] type, String sql) throws SQLException 55     { 56         DataTable dt = new DataTable(); 57          58         ArrayList
>list = new ArrayList
>(); 59 60 if(!setPstmtParam(coulmn, type, sql)) 61 return null; 62 rs = pstmt.executeQuery(); 63 ResultSetMetaData rsmd = rs.getMetaData();//取数据库的列名 64 int numberOfColumns = rsmd.getColumnCount(); 65 while(rs.next()) 66 { 67 HashMap
rsTree = new HashMap
(); 68 for(int r=1;r
数据层封装
1 package com.dao.db; 2  3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.Map; 7 import java.util.Set; 8  9 /**10  * 数据集封装11  * @author Administrator12  *13  */14 public class DataTable {15     16     public String[] column;//列字段17     public String[][] row; //行值18     public int rowCount = 0;//行数19     public int colCoun = 0;//列数20     21     22     public DataTable() {23         super();24     }25     26     public DataTable(String[] column, String[][] row, int rowCount, int colCoun) {27         super();28         this.column = column;29         this.row = row;30         this.rowCount = rowCount;31         this.colCoun = colCoun;32     }33 34 35     public void setDataTable(ArrayList
> list) {36 rowCount = list.size();37 colCoun = list.get(0).size();38 column = new String[colCoun];39 row = new String[rowCount][colCoun];40 for (int i = 0; i < rowCount; i++) {41 Set
> set = list.get(i).entrySet();42 int j = 0;43 for (Iterator
> it = set.iterator(); it44 .hasNext();) {45 Map.Entry
entry = (Map.Entry
) it46 .next();47 row[i][j] = entry.getValue();48 if (i == rowCount - 1) {49 column[j] = entry.getKey();50 }51 j++;52 }53 }54 }55 56 public String[] getColumn() {57 return column;58 }59 60 public void setColumn(String[] column) {61 this.column = column;62 }63 64 public String[][] getRow() {65 return row;66 }67 68 public void setRow(String[][] row) {69 this.row = row;70 }71 72 public int getRowCount() {73 return rowCount;74 }75 76 public void setRowCount(int rowCount) {77 this.rowCount = rowCount;78 }79 80 public int getColCoun() {81 return colCoun;82 }83 84 public void setColCoun(int colCoun) {85 this.colCoun = colCoun;86 }87 88 89 90 }
数据集封装
1 package com.bussiness.test; 2  3 import java.sql.SQLException; 4 import java.sql.Types; 5  6 import com.dao.db.DBManager; 7 import com.dao.db.DataTable; 8  9 public class TestBusIness{10     11     static String searchSql = "select * from score";12     static String insertSql = "insert into score(name, age, score)values(?,?,?)";13     static String deleteSql = "delete from score where id = ?";14     static String updateSql = "update score set name = ? where id = ?";15     16     public static void main(String[] args) {17         intsertData();18         searchData();19     }20     21     private static void intsertData()22     {    23         DBManager dm = new DBManager();24         String[] coulmn = new String[]{"wyf2",  "23", "89.5"};25         int[] type = new int[]{Types.CHAR, Types.INTEGER, Types.DOUBLE};26         27         try {28             boolean flag = dm.updateOrAdd(coulmn, type, insertSql);29             if(flag)30                 System.out.println("插入成功");31         } catch (SQLException e) {32             e.printStackTrace();33         }34     }35     private static void searchData()36     {    37         DBManager dm = new DBManager();38         String[] coulmn = null;39         int[] type = null;40         41         try {42             DataTable dt = dm.getResultData(coulmn, type, searchSql);43             if(dt != null && dt.getRowCount()> 0){            44                 for(int i = 0; i
测试Demo

 

转载于:https://www.cnblogs.com/FCWORLD/p/3401622.html

你可能感兴趣的文章
RxJS & Angular
查看>>
面向对象(多异常的声明与处理)
查看>>
MTK笔记
查看>>
ERROR: duplicate key value violates unique constraint "xxx"
查看>>
激活office 365 的启动文件
查看>>
无法根据中文查找
查看>>
[简讯]phpMyAdmin项目已迁移至GitHub
查看>>
转载 python多重继承C3算法
查看>>
【题解】 bzoj1597: [Usaco2008 Mar]土地购买 (动态规划+斜率优化)
查看>>
css文本溢出显示省略号
查看>>
git安装和简单配置
查看>>
面向对象:反射,双下方法
查看>>
鼠标悬停提示文本消息最简单的做法
查看>>
Java面向对象重要关键字
查看>>
课后作业-阅读任务-阅读提问-2
查看>>
面向对象设计中private,public,protected的访问控制原则及静态代码块的初始化顺序...
查看>>
fat32转ntfs ,Win7系统提示对于目标文件系统文件过大解决教程
查看>>
Awesome Adb——一份超全超详细的 ADB 用法大全
查看>>
shell cat 合并文件,合并数据库sql文件
查看>>
Android 将drawable下的图片转换成bitmap、Drawable
查看>>