sql读取加密后的配置文件

一,写一个DB连接工具类

package com.hooypay.tool;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.commons.codec.binary.Base64;

public class DB2 {
    
    public static Connection getConn() {
        Properties pro = new Properties();
    
        Connection conn = null;
                try {
                    try {
                        pro.load(DB2.class.getClassLoader().getResourceAsStream("conData.properties"));//读取配置文件
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    //64加密
//                    System.out.println(new String(Base64.encodeBase64("1234".getBytes())));
//                    System.out.println(new String(Base64.encodeBase64("root".getBytes())));
//                    System.out.println(new String(Base64.encodeBase64("com.mysql.jdbc.Driver".getBytes())));
//                    System.out.println(new String(Base64.encodeBase64("jdbc:mysql://localhost/hooy1platform?zeroDateTimeBehavior=convertToNull".getBytes())));
                    //64解密
                    String DRIVER=new String(Base64.decodeBase64(pro.getProperty("DRIVER").getBytes()));
                    //String URL=new String(Base64.decodeBase64(pro.getProperty("URL").getBytes()));
                    String URL="jdbc:mysql://localhost/hooy1platform?charset=utf-8&zeroDateTimeBehavior=convertToNull";
            //    String URL="jdbc:mysql://192.168.1.231/hooyplatform?charset=utf-8&zeroDateTimeBehavior=convertToNull";
                    String USER=new String(Base64.decodeBase64(pro.getProperty("USER").getBytes()));
                    String PASSWORD=new String(Base64.decodeBase64(pro.getProperty("PASSWORD").getBytes()));
                        Class.forName(DRIVER);
                        conn = DriverManager.getConnection(URL, USER,PASSWORD);
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        
        return conn;
    }
    
    public static Statement getStmt(Connection conn) {
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return stmt;
    }
    
    public static PreparedStatement getPstmt(Connection conn, String sql) {
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pstmt;
    }
    
    public static ResultSet getRs(PreparedStatement pstmt) {
        ResultSet rs = null;
        try {
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
    
    public static ResultSet getRs(Statement stmt, String sql) {
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
    
    public static void close(Connection conn) {
        try {
            if (conn != null)
                conn.close();
                conn = null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static void close(Statement stmt) {
        try {
            if (stmt != null)
                stmt.close();
                stmt = null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static void close(PreparedStatement pstmt) {
        try {
            if (pstmt != null)
                pstmt.close();
                pstmt = null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static void close(ResultSet rs) {
        try {
            if (rs != null)
                rs.close();
                rs = null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        DB2.getConn();
    }
}
写一个配置文件conData.properties放在src目录下
DRIVER=Y29tLm15c3FsLmpkYmMuRHJpdmVy
URL=amRiYzpteXNxbDovL2xvY2FsaG9zdC9ob295MXBsYXRmb3JtP3plcm9EYXRlVGltZUJlaGF2aW9yPWNvbnZlcnRUb051bGw=
USER=cm9vdA==
PASSWORD=cm9vdA==
#PASSWORD=MTIzNA\=\=
 
  

 



 

转载于:https://www.cnblogs.com/xiaomuv587/archive/2013/01/24/2875129.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值