栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Properties集合与Jdbc工具类

Properties集合与Jdbc工具类

1.Properties集合 1.1 Properties作为Map集合的特有方法
方法名说明

Object setProperty(String key, String value)

设置集合的键和值,都是String类型,底层调用 Hashtable方法

put String getProperty(String key)

使用此属性列表中指定的键搜索属性

Set stringPropertyNames()

从该属性列表中返回一个不可修改的键集,其中键及其对应的 值是字符串

示例代码:

public class PropertiesDemo {
    public static void main(String[] args) {
        //创建集合对象
        Properties prop = new Properties();
        //Object setProperty(String key, String value):设置集合的键和值,都是String类型,底层调用Hashtable方法put
        prop.setProperty("test1", "孙悟空");
        prop.setProperty("test2", "猪八戒");
        prop.setProperty("test3", "唐三藏");

        //Set stringPropertyNames():从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
        Set names = prop.stringPropertyNames();
        for (String key : names) {
//            System.out.println(key);
            String value = prop.getProperty(key);
            System.out.println(key + "," + value);
        }
    }
}
1.2 Properties和IO流相结合的方法
方法名说明

void load(InputStream inStream)

从输入字节流读取属性列表(键和元素对)

void load(Reader reader)

从输入字符流读取属性列表(键和元素对)

void store(OutputStream out, String comments)

将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法的格式写入输出字节流

void store(Writer writer, String comments)

将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流

示例代码:

public class PropertiesDemo01 {
    public static void main(String[] args) throws IOException {
        //把集合中的数据保存到文件
        myStore();
        //把文件中的数据加载到集合
        myLoad();
    }
    private static void myLoad() throws IOException {
        Properties prop = new Properties();
        //void load(Reader reader):
        FileReader fr = new FileReader("myfile\fw.txt");
        prop.load(fr);
        fr.close();
        System.out.println(prop);
    }
    private static void myStore() throws IOException {
        Properties prop = new Properties();
        prop.setProperty("test1","孙悟空");
        prop.setProperty("test2","猪八戒");
        prop.setProperty("test3","唐三藏");
        //void store(Writer writer, String comments):
        FileWriter fw = new FileWriter("myfile\fw.txt");
        prop.store(fw,null);
        fw.close();
    }
}
2.Jdbc工具类 2.1 简单工具类

示例代码:

public class JDBCUtils {
    static final String driver = "com.mysql.jdbc.Driver";
    static final String url = "jdbc:mysql:///yun?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false";
    static final String user = "root";
    static final String password = "123456";


    //默认加载块,只加载一次
    static{
        try{
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获取数据库连接
    public Connection getConnection() {
        Connection conn=null;
        try{
            conn= DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    //关闭数据库连接
    public void closeAll(ResultSet rs, PreparedStatement psmt,Connection conn){
        if(rs!=null){
            try{
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(psmt!=null){
            try{
                psmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try{
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //设置参数的方法
    public PreparedStatement setParam(PreparedStatement psmt,Object[]objects){
        if(objects!=null){
            for(int i=0;i0){
                System.out.println("操作成功");
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(null,psmt,conn);
        }
        System.out.println("操作失败");
        return false;
    }
    //通用的查询方法
    public List> executeQuery(String sql,Object[] objects){
        ResultSet rs=null;
        PreparedStatement psmt=null;
        Connection conn=null;

        List> list=new ArrayList<>();
        try{
            conn=getConnection();
            psmt=conn.prepareStatement(sql);
            psmt=this.setParam(psmt,objects);
            rs=psmt.executeQuery();

            //返回结果共有多少行
            ResultSetmetaData metaData = rs.getmetaData();
            int count= metaData.getColumnCount();

            //定义一个map集合封装一行数据
            Map rowMap=null;
            while(rs.next()){
                rowMap=new HashMap<>();
                for (int i = 1; i <=count ; i++) {
                    String columnName = metaData.getColumnName(i);
                    Object columnValue = rs.getObject(i);
                    rowMap.put(columnName,columnValue);
                }
                list.add(rowMap);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(rs,psmt,conn);
        }
        return list;
    }
}

2.2 使用properties集合创建工具类

示例代码:

public class JDBCUtils {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;
    //静态加载块,默认执行
    static {
        try {
            //创建properties集合
            Properties pro = new Properties();
            
            //获取src路径下文件的方法ClassLoader
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            URL res = classLoader.getResource("jdbc.properties");
            String path = res.getPath();
            pro.load(new FileReader(path));
            
            //3.获取数据,赋值
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
            driver = pro.getProperty("driver");
            Class.forName(driver);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }
    
    public static void close(Statement stmt,Connection conn){
        if( stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if( conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    
    public static void close(ResultSet rs,Statement stmt, Connection conn){
        if( rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if( stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if( conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
jdbc.properties文件 
#如果是locaclhost,可以省略不写,如下url
url=jdbc:mysql:///yun
user=root
password=123456
driver=com.mysql.jdbc.Driver

以上就是Jdbc的简单工具类和Properties集合的结合使用,欢迎收藏~

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/307748.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号