All pastes #2057062 Raw Edit

Unnamed

public text v1 · immutable
#2057062 ·published 2011-05-11 21:53 UTC
rendered paste body
public class JtdsConnectionProvider {
	
	private String username = "";
	private String password = "";
	private String db = "master";
	boolean trusted = false;
	private String ip = "";
	private int port = 1433;
	private String instance = "";
	private String ssl = "off";
	private Connection con;
	private boolean connected = false;
	private String lastError = "";
	
	/** 
	 * provide basic connection 
	 * SQL authentication
	 * connection
	 * 
	 * @param ip/host
	 * @param username
	 * @param password
	 */
	public JtdsConnectionProvider(String ip, String username, String password) {
		super();
		this.username = username;
		this.password = password;
		this.ip = ip;
	}
	
	/**
	 * Provide trusted
	 * connection
	 * @param ip/host
	 */
	public JtdsConnectionProvider(String ip) {
		super();
		this.ip = ip;
		this.trusted = true;
	}
	
	public void overridePort(int port){
		this.port = port;
	}
	
	public void overrideDB(String dbname){
		this.db = dbname;
	}
	
	/** SSL **/
	
	/** request - 	SSL is requested; if the server does not support it then a plain connection is used **/
	public static final String SSL_LEVEL_REQUEST = "request";
	/** require	- SSL is requested; if the server does not support it then an exception is thrown **/
	public static final String SSL_LEVEL_REQUIRE = "require";
	/** authenticate - 	Same as require except the server's certificate must be signed by a trusted CA **/
	public static final String SSL_LEVEL_AUTHENTICATE = "authenticate";
	/** DEFAULT - off	 - SSL is not request or used; this is the default **/
	public static final String SSL_LEVEL_OFF = "off";
	
	public void requestSSL(){
		this.ssl = SSL_LEVEL_REQUEST;
	}
	
	public void requireSSL(){
		this.ssl = SSL_LEVEL_REQUIRE;
	}
	
	public void forceSSL(){
		this.ssl = SSL_LEVEL_AUTHENTICATE;
	}
			
	public void setInstance(String instance){
		this.instance = instance;
	}
	
	/** CONNECTION STRING **/
	
	private static final String JDBC_CONNECTION_URL = "jdbc:jtds:sqlserver://";
    private static final String JDBC_DATABASE_ATTRIBUTE = "databaseName=";
    private static final String JDBC_INSTANCE_ATTRIBUTE = "instance=";
    private static final String JDBC_SSL_LEVEL = "ssl=";
	
	public String getConnectionString(){
		StringBuilder connectionUrl = new StringBuilder(JDBC_CONNECTION_URL);
        connectionUrl.append(this.ip);
        connectionUrl.append(":");
        connectionUrl.append(this.port);
        connectionUrl.append(";");
        connectionUrl.append(JDBC_DATABASE_ATTRIBUTE);
        connectionUrl.append(this.db);
        connectionUrl.append(";");
        
        /** decision entries **/
        addInstance(connectionUrl);        
        addSSL(connectionUrl);
        
        return connectionUrl.toString();
        
	}

	/** Add SSL **/
	private void addSSL(StringBuilder connectionUrl) {		
        if(!this.ssl.equals(SSL_LEVEL_OFF)){
            connectionUrl.append(JDBC_SSL_LEVEL);
            connectionUrl.append(this.ssl);
        	connectionUrl.append(";");
        }
	}

	/** Add instance **/
	private void addInstance(StringBuilder connectionUrl) {
		
        if(StringUtils.isNotEmpty(this.instance)){
            connectionUrl.append(JDBC_INSTANCE_ATTRIBUTE);
            connectionUrl.append(this.instance);
        	connectionUrl.append(";");
        }
	}
	
	/** CONNECTING **/
	
    private static final String JDBC_DRIVER_NAME = "net.sourceforge.jtds.jdbc.Driver";
    
    private Connection connect() throws Exception{
    	this.lastError = "";
        try {

            Class.forName(JDBC_DRIVER_NAME);

            con = DriverManager.getConnection(getConnectionString(), this.username, this.password);

        } catch (ClassNotFoundException e) {
            LoggingUtil.getGlobalLogger().log(Level.SEVERE,"Could not create database connection:: " + e.toString());
            lastError = "Wiring issue, class not found:" + JDBC_DRIVER_NAME + "::" + e.toString();
            throw e;
        }catch(SQLException e) {        	
    	   	LoggingUtil.getGlobalLogger().log(Level.SEVERE,"Could not create database connection error:: "+ e.toString());
    	   	lastError = "SQLException::" + e.toString();
    	   	throw e;
        }catch(Throwable e){
    	   	LoggingUtil.getGlobalLogger().log(Level.SEVERE,"Could not create database connection error:: "+ e.toString());
    	   	lastError = "Catch all failure::" + e.toString();
    	   	throw new Exception(e);
        }
        return con;
    }
    
    public void disconnect(){
        
		try {
			this.con.close();
		} catch (SQLException e) {
    	   	LoggingUtil.getGlobalLogger().log(Level.SEVERE,"Could not close database connection:: "+ e.toString());
    	   	lastError = "Failed to close connection::" + e.toString();
		}finally{
			this.con = null;
		}
    }
        
    
    public boolean isConnected(){
    	if(this.con != null){
    		return true;
    	}
    	return false;
    }
    
    private Connection getConnection() throws Exception{
    	if(isConnected()){
    		return this.con;
    	}
    	return connect();
    }
    
    public String getConnectionError(){
    	return this.lastError;
    }
    

    
    public static void main(String[] args) {
    	JtdsConnectionProvider test1 = new JtdsConnectionProvider("10.203.80.101", "sa", "firewall");    	
    	System.out.println("Start basic-------------");
    	System.out.println(test1.getConnectionString());
    	try {
			test1.getConnection();
			System.out.println("Sucess - disconnect-------------");
			test1.disconnect();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(test1.getConnectionError());
			e.printStackTrace();
		}
    	System.out.println("Add port-------------");
    	test1.overridePort(1433);
    	System.out.println(test1.getConnectionString());
    	try {
			test1.getConnection();
			System.out.println("Sucess - disconnect-------------");
			test1.disconnect();
		} catch (Exception e) {
			System.out.println(test1.getConnectionError());
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	/**
    	System.out.println("Add SSL-------------");
    	test1.requestSSL();
    	System.out.println(test1.getConnectionString());
    	System.out.println("Add Instance-------------");
    	test1.setInstance("websense");
    	System.out.println(test1.getConnectionString());
    	**/
    	System.out.println("Change DB-------------");
    	test1.overrideDB("wslogdb70");
    	System.out.println(test1.getConnectionString());
    	try {
			test1.getConnection();
			System.out.println("Sucess - disconnect-------------");
			test1.disconnect();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}