zoukankan      html  css  js  c++  java
  • Java通用oracle和mysql数据库连接

    Java中oracle数据库连接写一个通用类UBUtil(){}

    import java.io.InputStream;
    import java.sql.*;
    import java.util.Properties;
    
    public class DBUtil {
        private static Connection con;
        private static String url;
        private static String user;
        private static String pwd;
    
        public DBUtil() {
    
        }
        static {
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");/*如果是MySQL就改为Class.forName("com.mysql.jdbc.Driver");*/
                InputStream is = DBUtil.class.getResourceAsStream("/db.properties");//db.properties 是一个用户配置文件传用户名密码
                Properties prop=new Properties();
                prop.load(is);
                url=prop.getProperty("url");
                user=prop.getProperty("user");
                pwd=prop.getProperty("password");
                con = DriverManager.getConnection(url, user, pwd);
            }catch (Exception e){
            }
        }
        public static ResultSet find(String sql){
            con=getCon();
            try {
                Statement smt=con.createStatement();
                ResultSet rs=smt.executeQuery(sql);
                return rs;
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
        public static ResultSet find(String sql,Object ...pram){//...pram数组
            con=getCon();
            try {
                PreparedStatement smt=con.prepareStatement(sql);
                for (int i=0;i<pram.length;i++){
                    smt.setObject(i+1,pram[i]);
                }
                ResultSet rs=smt.executeQuery();
                return rs;
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
        public static void insert(String sql,Object ...pram){//...pram数组
            con=getCon();
            try {
                PreparedStatement smt=con.prepareStatement(sql);
                for (int i=0;i<pram.length;i++){
                    smt.setObject(i+1,pram[i]);
                }
                smt.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        public static Connection getCon(){
            try {
                if(con==null||con.isClosed())
                    con = DriverManager.getConnection(url, user, pwd);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return con;
        }
    }

     最后补一下db.properties中格式:

    #db.properties
    #数据配置文件
    url=jdbc:oracle:thin:@localhost:1521:XE #或者url=::thin:@localhost:1521:xe
    #url=jdbc:mysql://127.0.0.1:3306/diarydb?useUnicode=true&characterEncoding=UTF-8  #mysql的url
    user=root
    password=password
    

      

      

      

  • 相关阅读:
    1 < 2 < 3为true, 3 > 2 > 1为false
    我的第五代选择器Icarus
    浮动不换行
    教你游泳,不会游的看了包你学会!!! 分享
    node.js 一个简单的页面输出
    天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能
    setTimeout和setInterval的使用
    JS window.open()属性
    车牌识别及验证码识别的一般思路
    苹果开发者账号注册流程
  • 原文地址:https://www.cnblogs.com/feipengting/p/7606042.html
Copyright © 2011-2022 走看看