当前位置: 首页> 技术文章> JDBC封装数据库的工具类

JDBC封装数据库的工具类

JDBC封装数据库的工具类

深圳多测师王sir原创

日期:2019-12-14


一、先写一个数据库的工具函数

要求:

1、可以通过IP地址账号密码等进行连接数据库

2、封装一

个查询的方法

package com.xiaoshu;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Properties;


public class DBUtils {


        //硬编码,写死的代码--》解耦-》配置文件

        /**

         * 连接字符串

         */

        private static String url;

        /**

         * 用户名

         */

        private static String user;

        /**

         * 密码

         */

        private static String password;


        /**

         * 上面的这些信息会变吗??

         *                 数据库名

         *                 用户名密码变了

         *                 数据库服务器ip换了

         *                 测试环境切换到生产服务器

         *                 数据库:mysql--》oracle

         * 测试环境

         */


        //注册驱动:只需要一次,当你用DBUtils类的时候,就注册好 把注册驱动写进静态代码块当中

        //静态代码块:只在类加载到jvm中执行一遍

        static {

                try {

                        Properties properties = new Properties();

                        properties.load(Tester.class.getResourceAsStream("/jdbc.properties"));

                        url = properties.getProperty("jdbc.url");

                        user = properties.getProperty("jdbc.user");

                        password = properties.getProperty("jdbc.password");


                        String driver = properties.getProperty("jdbc.driver");

                        Class.forName(driver);

                } catch (Exception e) {

                        e.printStackTrace();

                }

        }


        //获得连接的方法

        private static Connection getConnection() {

                try {

                        return DriverManager.getConnection(url, user, password);

                } catch (SQLException e) {

                        e.printStackTrace();

                }

                return null;

        }


        /**

         * 增删改

         * @param sql

         * @param parameters

         * @throws ClassNotFoundException

         * @throws SQLException

         */

        public static void excuteSQL(String sql, String... parameters) {

                Connection conn = null;      //先进行初始化 为了后面的资源可以在finally当中进行关闭

                PreparedStatement pstmt = null;

                try {

                        conn = getConnection();

                        pstmt = conn.prepareStatement(sql);

                        for (int i = 0; i < parameters.length; i++) {

                                pstmt.setString(i + 1, parameters);

                        }

                        pstmt.execute();

                } catch (SQLException e) {

                        e.printStackTrace();

                } finally {

                        close(conn, pstmt);

                }

        }


        


        /**

         * 查询

         * @param sql

         * @param parameters

         * @return

         * @throws ClassNotFoundException

         * @throws SQLException

         */

        public static List<HashMap<String, String>> select(String sql, String... parameters) {

                //是可能有多条记录记录--》保存到一个什么数据容器

                List<HashMap<String, String>> allRecordList = null;

                Connection conn = null;

                PreparedStatement pstmt = null;

                ResultSet resultSet = null;

                try {

                        //获得连接

                        conn = getConnection();

                        pstmt = conn.prepareStatement(sql);

                        //参数设值

                        for (int i = 0; i < parameters.length; i++) {

                                pstmt.setString(i + 1, parameters);

                        }

                        //执行查询获取结果集

                        resultSet = pstmt.executeQuery();

                        //获取结果集的元数据:   元数据(描述数据的数据)

                        ResultSetMetaData metaData = resultSet.getMetaData();

                        //获得每个字段,需要知道字段数,获得列数

                        int columnCount = metaData.getColumnCount();//5

                        allRecordList = new ArrayList<HashMap<String, String>>();

                        //遍历结果集

                        while (resultSet.next()) {

                                //一条记录是包装成一个map

                                HashMap<String, String> recordMap = new HashMap<String, String>();

                                //遍历所有的列

                                for (int i = 1; i <= columnCount; i++) {

                                        String columnName = metaData.getColumnName(i);

                                        String value = resultSet.getString(i);

                                        //                                System.out.print(columnName+":"+value +"    ");

                                        //把字段名作为key,对应数据作为值: key -value:

                                        recordMap.put(columnName, value);

                                }

                                //添加到List容器中

                                allRecordList.add(recordMap);

                        }

                } catch (SQLException e) {

                        e.printStackTrace();

                } finally {

                        close(conn, pstmt, resultSet);

                }


                return allRecordList;

        }


        /**

         * 关闭资源

         * @param conn

         * @param pstmt

         * @param resultSet

         */

        private static void close(Connection conn, PreparedStatement pstmt, ResultSet resultSet) {

                //关闭资源

                if (resultSet != null) {

                        try {

                                resultSet.close();

                        } catch (SQLException e) {

                                e.printStackTrace();

                        }

                }

                close(conn, pstmt);

        }


        /**

         * 关闭资源

         * @param conn

         * @param pstmt

         */

        private static void close(Connection conn, PreparedStatement pstmt) {

                if (pstmt != null) {

                        try {

                                pstmt.close();

                        } catch (SQLException e) {

                                e.printStackTrace();

                        }

                }

                if (conn != null) {

                        try {

                                conn.close();

                        } catch (SQLException e) {

                                e.printStackTrace();

                        }

                }

        }

}





二、调用工具类当中的select查询方法

package com.xiaoshu;

import java.io.IOException;

import java.sql.SQLException;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Properties;


public class Tester {


        public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {

                String sql = "select * from member;";

                List<HashMap<String, String>> recordList = DBUtils.select(sql);  //查询的结果以键值对的形式返回保存在列表容器当中

                for (HashMap<String, String> record : recordList) {   //对recordList对象进行循环遍历 取到单个的键和值

                        System.out.println(record);


                }



需要了解更多技术和获取更多免费技术视频的加入我们多测师的技术交流群或者可以微信我(15367499889

加入我们多测师的技术交流群定期获得福利哦!!!

upfile1583500570164.png                       upfile1583503305394.png


更多免费软件测试和python、Java开发、大数据和人工智能的学习资料就在多测师官网地址:http://www.duoceshi.cn

加入我们只需要三个月就可以月薪上万!!!

upfile1583500745149.png


上一篇: java反射之类的字节码对象的三种调用方式和JDBC

下一篇: 软件测试之手工测试人员如何转测试开发?

QQ技术交流群

多测师官方学习交流
556733550

加入群聊