Java JDBC SQLServer Connection getConnection()

Here you can find the source of getConnection()

Description

get Connection

License

Apache License

Declaration

public static Connection getConnection() 

Method Source Code


//package com.java2s;
/*//from  w  w w  . java  2  s.  c om
 * Copyright 2012-2013 inBloom, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.Properties;

public class Main {
    private static String connectionString = "jdbc:jtds:sqlserver://10.81.1.33:1433/edfi;catalog=edfi";
    private static String userName = "sa";
    private static String password = "benerator";
    private static boolean propertiesLoaded = false;

    public static Connection getConnection() {
        if (!propertiesLoaded)
            getProperties();

        Connection conn = null;
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            conn = DriverManager.getConnection(connectionString, userName, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void getProperties() {
        Properties prop = new Properties();
        try {
            prop.load(new FileInputStream("exportTool.properties"));
            connectionString = prop.getProperty("connectionString");
            userName = prop.getProperty("userName");
            password = prop.getProperty("password");

        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        propertiesLoaded = true;
    }
}

Related

  1. GetConnection()
  2. getConnection()