一,写一个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\=\=