/**
 * Return the string array for specific column. First colum is 1 rather than 0.
 * @param rs
 * @param colNum start from one
 * @return
 */
public static String[] sqlGetColStr(java.sql.ResultSet rs, int colNum) {
	int len = Ts.sqlResultSetLength(rs);
	String[] result = new String[len];
	try {
		for (int i = 0; i < len; i++) {
			rs.next();
			result[i] = rs.getString(colNum);
		}
		rs.beforeFirst();
	} catch (java.sql.SQLException ex) {
		Ts.printErr(ex);
	}
	return result;
}
 
/**
 * Return the integer array for specific column.
 * @param rs
 * @param colNum start from one
 * @return
 */
public static int[] sqlGetColInt(java.sql.ResultSet rs, int colNum) {
	int len = Ts.sqlResultSetLength(rs);
	int[] result = new int[len];
	try {
		for (int i = 0; i < len; i++) {
			rs.next();
			result[i] = rs.getInt(colNum);
		}
		rs.beforeFirst();
	} catch (java.sql.SQLException ex) {
		Ts.printErr(ex);
	}
	return result;
}
 
public static double[] sqlGetColDouble(java.sql.ResultSet rs, int colNum) {
	int len = Ts.sqlResultSetLength(rs);
	double[] result = new double[len];
	try {
		for (int i = 0; i < len; i++) {
			rs.next();
			result[i] = rs.getDouble(colNum);
		}
		rs.beforeFirst();
	} catch (java.sql.SQLException ex) {
		Ts.printErr(ex);
	}
	return result;
}
 
/**
 * This method is similar to sqlGetColDouble but it does not use sqlResultSetLength method.
 * @param rs
 * @param colNum
 * @return
 */
public static double[] sqlGetColDouble2(java.sql.ResultSet rs, int colNum) {
	java.util.List<Double> list = new java.util.LinkedList<Double>();
	try {
		while (rs.next()) {
			list.add(rs.getDouble(colNum));
		}
		rs.beforeFirst();
	} catch (java.sql.SQLException ex) {
		Ts.printErr(ex);
	}
	return Ts.listToArrayDouble(list);
}
 
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

Basic linear algebra uncovers and clarifies very important geometry and algebra.

Dr. Paul Garrett