/* * DataAccess.fx * * Created on Sep 27, 2007, 9:46:04 AM */ package tablesample; /** * @author Radhika Gehant */ import javafx.ui.*; import java.lang.Thread; import java.lang.Exception; import java.sql.*; public class DatabaseAccess { private attribute driverName: String; private attribute jdbcUrl : String; private attribute user : String; private attribute password : String; public attribute driver : Driver; public attribute conn : Connection; private attribute rs : ResultSet; private attribute stmt : Statement; public operation connect(); public operation shutdown(); public operation tableExists(table: String); public operation processDBQuery(qry: String): ResultSet; }// Database attribute DatabaseAccess.conn = null; attribute DatabaseAccess.stmt = null; attribute DatabaseAccess.rs = null; operation DatabaseAccess.connect() { // Load driver class using context class loader var thread = Thread.currentThread(); var classLoader = thread.getContextClassLoader(); var driverClass = classLoader.loadClass(this.driverName); // Instantiate and register JDBC driver this.driver = (Driver) driverClass.instantiate(); // JavaFX Class DriverManager.registerDriver(driver); // Connect to database this.conn = DriverManager.getConnection(this.jdbcUrl, this.user, this.password); }// Database.connect operation DatabaseAccess.processDBQuery(qry: String) :ResultSet { // process the query string try{ if(null <> this.conn) { if (this.stmt <> null) { this.stmt.close(); this.stmt = null; } if (this.rs <> null) { this.rs.close(); this.rs = null; } stmt = this.conn.createStatement(); rs = stmt.executeQuery(qry); } } catch(e:SQLException) { e.printStackTrace(); } return rs; }//processDB