(New page: <source lang="java"> * * Get the SQL Connection. * @param drivers * @param url * @param username * @param password * @return: public static java.sql.Connection sqlGetConnection(...)
 
 
Line 36: Line 36:
 
return conn;
 
return conn;
 
}
 
}
 +
 
/**
 
/**
 
  * Get the SQL Connection using property file.
 
  * Get the SQL Connection using property file.
Line 49: Line 50:
 
return Ts.sqlGetConnection(drivers, url, username, password);
 
return Ts.sqlGetConnection(drivers, url, username, password);
 
}
 
}
 +
 
public static java.sql.Connection sqlGetConnection(String fileName) {
 
public static java.sql.Connection sqlGetConnection(String fileName) {
 
return Ts.sqlGetConnection(new java.io.File(fileName));
 
return Ts.sqlGetConnection(new java.io.File(fileName));
 
}
 
}
 +
 
/**
 
/**
 
  * Get the SQL Connection using default property file.
 
  * Get the SQL Connection using default property file.
Line 59: Line 62:
 
return Ts.sqlGetConnection("./src/pit30z/util/core/Oracle.properties");
 
return Ts.sqlGetConnection("./src/pit30z/util/core/Oracle.properties");
 
}
 
}
 +
 
public static void printErr(java.sql.SQLException ex) {
 
public static void printErr(java.sql.SQLException ex) {
 
System.out.println("SQL State: " + ex.getSQLState());
 
System.out.println("SQL State: " + ex.getSQLState());
Line 64: Line 68:
 
Ts.printErr((java.lang.Exception) ex);
 
Ts.printErr((java.lang.Exception) ex);
 
}
 
}
 +
 
public static void printErr(java.lang.Exception ex) {
 
public static void printErr(java.lang.Exception ex) {
 
ex.printStackTrace();
 
ex.printStackTrace();

Latest revision as of 17:32, 22 March 2011

/**
 * 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

ECE462 Survivor

Seraj Dosenbach