/*
 * DBObject.java
 * Base object template for Database
 * objects. This object defines the basic
 * functions for connecting to the database
 * that all the other object classes use. 
 */
package base;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;




public class DBObject {
   //Database Variables all these

   //are set and used in the getConn()

   //function.

   private Connection con;
   private String url;
   private String user;
   private String pass;
   protected Statement stmt;
   protected boolean DBWrite;
   protected int id;
   
   public DBObject(){
      //Connection url for test DB on Localhost

      url = "jdbc:mysql://localhost:3306/test";
      
      //Set username and Password

      user = "sheller";
      pass = "apassword!";
      
      //Default behavior is to write to database

      DBWrite = true;
   }
   public void getConn(){

      //Register MySQL Driver

      try {Class.forName("com.mysql.jdbc.Driver");}
         catch (Exception e){ e.printStackTrace(); }
      
      //Connect to Database

      try { con = DriverManager.getConnection(url, user, pass);}
         catch(Exception e){ e.printStackTrace(); }
         
      //Create a statement object for use

      try { stmt = con.createStatement(); }
         catch(Exception e){e.printStackTrace(); }
   }
   
   //Query function to create new 

   //database entries, this returns the

   //ID of the created object

   protected int create(String q){
      int id = 0;
      try {
         //execute the statement

         stmt.execute(q);
         //get the id of the created object

         stmt.execute("SELECT LAST_INSERT_ID()");
         
         //get the result set 

         ResultSet f = stmt.getResultSet();
         //get the ID

         while(f.next()){id = f.getInt(1);}
         return(id);
      }
         catch(Exception e){ 
            e.printStackTrace(); 
            return(id);
         }
   
   }
   
   //General purpose query function 

   //returns a ResultSet object with 

   //the data returned

   protected ResultSet query(String q){
      ResultSet f = null;

      try {
         //execute the statement

         stmt.execute(q); 
         //get the result set and return it

         f = stmt.getResultSet();
         return(f);
      }
         catch(Exception e){ 
            e.printStackTrace(); 
            return(f);
         }
   }
   
   //Function to set the DBWrite Boolean. This 

   //is used to determine whether or not the

   //database gets written.

   public boolean Write(boolean b){
      DBWrite = b;
      return DBWrite;
   }

   //Function to return the status of 

   //the DBWrite boolean

   public boolean Write(){
      return DBWrite;
   }
   
   //******NOT YET WRITTEN********//

   //Function to write all current

   //active data to the database. Used

   //when changes are made with DBWrite

   //set to false.

   public boolean SaveDB(){
      if (true){ return true; }
      else { return false; }
   }
   
   public int getID(){
      return id;
   }
}

syntax highlighted by Code2HTML, v. 0.93pm6