zoukankan      html  css  js  c++  java
  • Java jdbc 连接oracle之三(封装工具类)

    driver = oracle.jdbc.driver.OracleDriver
    url = jdbc:oracle:thin:@192.168.10.105:1521:orcl
    user = LF
    password = LF
    package com.lf.testdatabase;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;
    
    public class JdbcTools {
        //properties文件名
        private static String defaultName = "jdbc.properties";
        /**
         * 获取Connection
         * @return connection
         * @throws Exception
         */
        public static Connection getConnection(){
            Connection connection = getConnection(defaultName);
            return connection;
        }
        /**
         * 通过properties文件名获取Connection
         * @param fileName  properties文件名
         * @return          Connection
         * @throws Exception
         */
        public static Connection getConnection(String fileName) {
            //IO流读取jdbc.properties文件
            InputStream in = JdbcTools.class.getClassLoader().getResourceAsStream(fileName);
            // 读取参数
            Properties p = new Properties();
            try {
                p.load(in);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String url = p.getProperty("url");
            String user = p.getProperty("user");
            String password = p.getProperty("password");
            String driver=p.getProperty("driver");
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            Connection connection=null;
            try {
                connection = DriverManager.getConnection(url, user, password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            try {
                if (in!=null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        
            return connection;
        }
        
    }
  • 相关阅读:
    趁热讲讲skin.xml支持的标签和attributes
    如何配置和编译ogre 1.7.0 + cegui 0.7.1
    关于OGRE基础教程6中CEGUI的layout文件can not locate的问题
    skin.xml皮肤配置讲解
    OCX控件注册相关(检查是否注册,注册,反注册)
    重回博客园继续我的 GUI库
    窗口类的定义
    UI库需要完成的任务
    屏幕截图代码
    深入C++的默认构造函数1
  • 原文地址:https://www.cnblogs.com/lantu1989/p/6201374.html
Copyright © 2011-2022 走看看