|
From: fredrik <fre...@us...> - 2005-04-28 11:51:16
|
Update of /cvsroot/test-manager/main/src/testmanager/database/jdbc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14164/src/testmanager/database/jdbc Added Files: Utilities.java Log Message: Reorganizing servlets & adding bug tracking --- NEW FILE: Utilities.java --- package testmanager.database.jdbc; import java.sql.Connection; import java.sql.Date; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Calendar; /** * @author Fredrik Fornwall */ public abstract class Utilities { /** * A general utility method for a query that only returns a single string. * * @param connection * The connection to the database. * @param query * The SQL query that is to result in a single string. * @return The string result of the query or null if the query had an empty result. * @throws SQLException * If one occurs while executing the Query. * @throws IllegalArgumentException * If the query results in more than one result. */ public static String stringQuery(Connection connection, String query) throws SQLException { ResultSet result = connection.createStatement().executeQuery(query); if (result.next()) { String returnValue = result.getString(1); if (result.next()) { // The result contains more than throw new IllegalArgumentException("In Database.stringQuery() the query \"" + query + "\" returned several rows"); } else { return returnValue; } } else { return null; } } /** * Create a java.sql.Date. TODO: Move from web utilities to database. * * @param year * The year of the date to create. * @param month * The month of the date to create. NOTE: January = 0 * @param day * The day of the month of the date to create. * * @return The java.sql.Date created from the above parameters. */ public static Date createSQLDate(int year, int month, int day) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DATE, day); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); return new Date(cal.getTime().getTime()); } } |