Revision as of 16:32, 22 March 2011 by Han84 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
/**
 * Get the SQL Connection.
 * @param drivers
 * @param url
 * @param username
 * @param password
 * @return
 */
public static java.sql.Connection sqlGetConnection(String drivers, String url, String username, String password) {
	java.sql.Connection conn = null;
	if (drivers != null) {
		System.setProperty("jdbc.drivers", drivers);
	} else {
		Ts.printErr("This method does not allow the null value for driver");
	}
	try {
		Class.forName(drivers).newInstance();
	} catch (ClassNotFoundException ex) {
		Ts.printErr(ex);
	} catch (InstantiationException ex) {
		Ts.printErr(ex);
	} catch (IllegalAccessException ex) {
		Ts.printErr(ex);
	}
	try {
		conn = java.sql.DriverManager.getConnection(url, username, password);
	} catch (java.sql.SQLException ex) {
		System.err.println("SQL connection fails!");
		System.err.println("drivers: " + drivers);
		System.err.println("url: " + url);
		System.err.println("user name: " + username);
		System.err.println("password: " + password);
		Ts.printErr(ex);
	}
	return conn;
}
/**
 * Get the SQL Connection using property file.
 * @param file the property file that stores every information for SQL connection
 * @return
 */
public static java.sql.Connection sqlGetConnection(java.io.File file) {
	java.util.Properties props = Ts.propertiesGet(file);
	String drivers = props.getProperty("jdbc.drivers");
	String url = props.getProperty("jdbc.url");
	String username = props.getProperty("jdbc.username");
	String password = props.getProperty("jdbc.password");
	return Ts.sqlGetConnection(drivers, url, username, password);
}
public static java.sql.Connection sqlGetConnection(String fileName) {
	return Ts.sqlGetConnection(new java.io.File(fileName));
}
/**
 * Get the SQL Connection using default property file.
 * @return
 */
public static java.sql.Connection sqlGetConnection() {
	return Ts.sqlGetConnection("./src/pit30z/util/core/Oracle.properties");
}
public static void printErr(java.sql.SQLException ex) {
	System.out.println("SQL State: " + ex.getSQLState());
	System.out.println("Error Code: " + ex.getErrorCode());
	Ts.printErr((java.lang.Exception) ex);
}
public static void printErr(java.lang.Exception ex) {
	ex.printStackTrace();
	System.exit(-1);
}


Back to JavaHowTo

Alumni Liaison

Sees the importance of signal filtering in medical imaging

Dhruv Lamba, BSEE2010