Java mysql數據庫連接Demo1


public class MysqlUtil {
    /**
     * 鏈接數據庫
     */
    /**
     * 方法一:
     * 加載驅動的方法不止一種,但這種最常用
      */
    public static Connection getConnectionOne(String database,String username,String password){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+database,username,
                    password);
            return connection;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 方法二:
     * 利用properties文件
     * ::::: 在Web 編程時 文件難以定位
      */
    public static Connection getConnectionTwo() {
        /**
         * 建立文件
         */
        Properties pro = new Properties();

        InputStream in = MysqlUtil.class.getClassLoader().getResourceAsStream("mysqllog.properties");
        try {
            pro.load(in);
            Class.forName(pro.getProperty("driver"));
            String username = pro.getProperty("user");
            String password = pro.getProperty("password");
            String database = pro.getProperty("database");
            String url = pro.getProperty("url");

            Connection connection = DriverManager.getConnection(url+database,username,password);
            return connection;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
    // Connection ,Statement, ResultSet 這幾個資源的關閉是有順序的
    public static void close (Object...objects) throws MysqlCloseException {
        Map<String,Object> map = new HashMap();
        for(Object o : objects){
            if(o instanceof ResultSet){
                map.put("ResultSet",o);
            }else if(o instanceof Connection){
                map.put("Connection",o);
            }else if(o instanceof Statement){
                map.put("Statement",o);
            }else if(o instanceof PreparedStatement){
                map.put("PreparedStatement",o);
            }else{
                throw new MysqlCloseException("關閉異常,不能處理");
            }
        }
        Object obj = map.get("ResultSet");
        if(obj!=null){
            ResultSet r = (ResultSet)obj;
            try {
                r.close();
                map.remove("ResultSet");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        obj = map.get("PreparedStatement");
        if(obj!=null){
            PreparedStatement p = (PreparedStatement)obj;
            try {
                p.close();
                map.remove("PreparedStatement");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        obj = map.get("Statement");
        if(obj!=null){
            Statement s = (Statement)obj;
            try {
                s.close();
                map.remove("Statement");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        obj = map.get("Connection");
        if(obj!=null){
            Connection c = (Connection)obj;
            try{
                c.close();
                map.remove("Connection");
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM