Listing 1817
Submitted by paul, 3 December 2008
package util; /** * * @author Paul Bonnevie */ import java.util.*; import java.sql.*; import javax.sql.rowset.CachedRowSet; import business.*; import com.sun.rowset.CachedRowSetImpl; public class SQLUtil { /********************************************************************* * Takes a SQL SELECT query (as a string) and returns the result * as a CachedRowSet which can then be manipulated. * * The cachedrowset allows one to examine the data even after closing * the database connection (something that a ResultSet does not allow) * * IT SHOULD ONLY BE CALLED FROM OTHER CLASSES IN THE SQL PACKAGE! * there is no where near enough error checking and monitoring to allow * direct sql queries to be run by any other classes. *********************************************************************/ public static CachedRowSet getSQLResult(String query) throws SQLException // doesn't catch it so I can see which method caused the error { //System.out.println(query); - used for testing. ResultSet result = null; CachedRowSet cachedresult; //placeholder for Paul's db connection information try { Class.forName("com.mysql.jdbc.Driver").newInstance();} // Forces the instantiation of a new Driver class. catch (Exception e) {System.out.println("error");} // I have no idea why, but without it the code fails // (found on the internet) String dbURL = "jdbc:mysql://localhost/MovieInfo?user=root&password=root"; // the database info is hard coded String username = "root"; String dbpass = "root"; Connection connection = DriverManager.getConnection(dbURL, username, dbpass); //defining the initial dashboard queries Statement statement = connection.createStatement(); result = statement.executeQuery(query); // executes the query cachedresult = new CachedRowSetImpl(); cachedresult.populate(result); // puts the ResultSet in the more usable CachedResultSet result.close(); // closes the result connection.close(); // closes the connection return cachedresult; }
