[Tynstep-svn] r15 - in trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server: . db

ChrisBurrell at crosswire.org ChrisBurrell at crosswire.org
Tue Nov 3 15:39:17 MST 2009


Author: ChrisBurrell
Date: 2009-11-03 15:39:17 -0700 (Tue, 03 Nov 2009)
New Revision: 15

Added:
   trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/db/
   trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/db/DbProvider.java
   trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/db/RefDataDbCommand.java
Log:


Added: trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/db/DbProvider.java
===================================================================
--- trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/db/DbProvider.java	                        (rev 0)
+++ trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/db/DbProvider.java	2009-11-03 22:39:17 UTC (rev 15)
@@ -0,0 +1,29 @@
+package com.tyndalehouse.step.web.server.db;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+public class DbProvider {
+	//TODO: shove this in a properties file
+	private String dbConnectionString = "jdbc:derby:StepDB";
+
+	public DbProvider() {
+		//TODO: move this somewhere else, or make it span across the app's life
+		try {
+			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
+		} catch (ClassNotFoundException e) {
+			e.printStackTrace();
+		}
+	}
+
+	public Connection getConnection() throws SQLException {
+		return DriverManager.getConnection(dbConnectionString );
+	}
+
+	public void finaliseConnection(Connection connection) throws SQLException {
+		if(connection != null && !connection.isClosed()) {
+			connection.close();
+		}
+	}
+}

Added: trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/db/RefDataDbCommand.java
===================================================================
--- trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/db/RefDataDbCommand.java	                        (rev 0)
+++ trunk/step-web-app/src/main/java/com/tyndalehouse/step/web/server/db/RefDataDbCommand.java	2009-11-03 22:39:17 UTC (rev 15)
@@ -0,0 +1,47 @@
+package com.tyndalehouse.step.web.server.db;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.apache.commons.logging.Log;
+
+import com.google.inject.Inject;
+
+public class RefDataDbCommand {
+	
+	private final Log logger;
+
+	@Inject
+	public RefDataDbCommand(Log logger) {
+		this.logger = logger;
+		
+	}
+	
+	public ResultSet select(final String refId) {
+		DbProvider provider = new DbProvider();
+		Connection connection = null;
+		try {
+			connection = provider.getConnection();
+			Statement statement = connection.createStatement();
+			//	do select
+			
+			//TODO: get a sql framework to generate those queries
+			//TODO: prevent SQL Injection attack
+	        ResultSet rs = 
+	        	statement.executeQuery("SELECT ref_key, ref_text" +
+	        			"FROM refdata where bible_ref = '" + refId + "'");
+			return rs;
+		} catch (SQLException e) {
+			logger.error("A SQL exception has occurred", e);
+		} finally {
+			try {
+				provider.finaliseConnection(connection);
+			} catch (SQLException e) {
+				logger.error("A SQL exception has occurred", e);
+			}
+		}
+		return null;
+	}
+}




More information about the Tynstep-svn mailing list