View Javadoc

1   package com.webstersmalley.mediacollection.database;
2   
3   import java.io.IOException;
4   import java.sql.Connection;
5   import java.sql.DriverManager;
6   import java.sql.PreparedStatement;
7   import java.sql.ResultSet;
8   import java.sql.SQLException;
9   
10  import org.apache.log4j.Logger;
11  import org.apache.log4j.PropertyConfigurator;
12  
13  public class DatabaseConnection
14  {
15  	private static Logger log = Logger.getLogger(DatabaseConnection.class);
16  	static
17  	{
18  		PropertyConfigurator.configure("log4j.properties");
19  	}
20  	
21  	public DatabaseConnection(String databaseName, boolean create) throws ClassNotFoundException, SQLException
22  	{
23  		String connectionStringStub = "jdbc:derby:" + databaseName + ";";
24  		this.databaseName = databaseName;
25          Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
26  		if (create)
27  		{
28  			this.connection = DriverManager.getConnection(connectionStringStub + "create=true");
29  		}
30  		else
31  		{
32  			this.connection = DriverManager.getConnection(connectionStringStub);
33  		}
34  	}
35  	
36  	private String databaseName;
37  	private Connection connection;
38  	
39      public static void drop(String databaseName) throws IOException
40      {
41      	shutdown(databaseName);
42      	Utilities.purgeDirectory(databaseName);
43      }
44      
45  	public void execute(String sql) throws SQLException
46  	{
47  		if (sql == null || (sql.trim().equals("")))
48  		{
49  			log.debug("Skipping null command");
50  			return;
51  		}
52  		log.debug("Running statement: " + sql);
53  		connection.prepareStatement(sql).execute();
54  	}
55      
56  	/**
57  	 * @return Returns the connection.
58  	 */
59  	public Connection getConnection() {
60  		return connection;
61  	}
62  
63  	/**
64  	 * @param connection The connection to set.
65  	 */
66  	public void setConnection(Connection connection) {
67  		this.connection = connection;
68  	}
69  
70  	public ResultSet executeQuery(String sql) throws SQLException
71  	{
72  		if (sql == null || (sql.trim().equals("")))
73  		{
74  			log.debug("Skipping null command");
75  			return null;
76  		}
77  		log.debug("Executing query: " + sql);
78  		PreparedStatement ps = connection.prepareStatement(sql);
79  		return ps.executeQuery();
80  	}
81  	
82      public void runScript(String filename) throws IOException, SQLException
83      {
84      	log.info("Running script: " + filename);
85  		ScriptReader sr = new ScriptReader(filename);
86  		String[] commands = sr.splitScript();
87  		for (int i = 0 ; i < commands.length ; i++)
88  		{
89  			execute(commands[i]);
90  		}    	
91      }
92      
93      public static void shutdown(String databaseName)
94      {
95      	try
96  		{
97      		DriverManager.getConnection("jdbc:derby:" + databaseName + "shutdown=true");
98  		}
99      	catch (SQLException se)
100 		{
101     		// Do nothing - we expect this!
102 		}
103     }
104     
105     public static String escapeString(String input) {
106     	return input.replaceAll("'", "''");
107     }
108     
109 }