Class Jet
Jet, like all classes that extend DataJet, must be initialized with connection, table and field information. The connection information can be supplied as connection string, driver and connection string, or as an object that implements ConnectionGenerator. Table and fields must both be defined. If any of this initialization information is missing or incorrect, Jet will throw exceptions when calling its various methods -- ConnectionException if the connection information is incorrect and DataJetException if the table or field information is incorrect.
When working with Jet by composition, initialize the class by
either calling a static jetSet
method, or by instantiating
the object with a constructor that also initializes the DataJet. When extending
Jet, it is usually a good idea to create a new jetSet
method that encapsulates the initialization information, and explicitly call
that method in the default constructor.
The examples below show how to work with Jet by composition.
To create a new record, follow this example:
//Example: Creating a new record String connectionString = [insert connection url]; String table = "my_table"; String[] fields = new String[]{"id", "field1", "field2"}; JetSet jetSet = new JetSet(connectionString, table, fields); Jet dj = new Jet(jetSet); dj.setValue(1, "new value 1"); dj.setValue(2, "new value 2"); dj.store();To load an existing record and then delete it, follow this example:
//Example: Load an existing record, then delete it String connectionString = [insert connection url]; String table = "my_table"; String[] fields = new String[]{"id", "field1", "field2"}; JetSet jetSet = new JetSet(connectionString, table, fields); Jet dj = Jet.load(1, jetSet); //load record with id = 1; System.out.println(dj.getValue(1)); //display a field from the record dj.remove(); //deletes the record from the databaseThe following listing shows how to create a basic class that extends Jet:
import anderix.datajets.*; class MyDataJet extends Jet { public static JetSet jetSet() { String driver = [insert database driver]; String connectionString = [insert connection url]; String table = "my_table"; String[] fields = new String[]{"id", "field1", "field2"}; return new JetSet(driver, connectionString, table, fields); } public MyDataJet() { super(jetSet()); } public MyDataJet load(int idToLoad) throws DataJetException { return (MyDataJet)load(idToLoad, new MyDataJet(), jetSet()); //call load method from DataJet } }The above DataJet will be able to create, read, update and delete records. For example:
//Example: create new record MyDataJet dj = new MyDataJet(); dj.setValue(1, "new value 1"); dj.setValue(2, "new value 2"); dj.store();
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiongetValue
(int fieldIndex) Returns the value of a field.getValueAsObject
(int fieldIndex) Returns the value of a field.static Jet[]
Loads an array of all Jets from the database.static Jet[]
loadBySql
(SqlBuilder sql, JetSet jetSet) Loads an array of Jets based on a SqlBuilder object.static Jet[]
Loads an array of Jets based on an SQL statement.static Jet
Loads a Jet with identity field equal toid
from the database.void
remove()
Removes the current Jet from the database.void
setValue
(int fieldIndex, boolean newvalue) Sets the value of a field.void
setValue
(int fieldIndex, byte newvalue) Sets the value of a field.void
setValue
(int fieldIndex, double newvalue) Sets the value of a field.void
setValue
(int fieldIndex, float newvalue) Sets the value of a field.void
setValue
(int fieldIndex, int newvalue) Sets the value of a field.void
setValue
(int fieldIndex, long newvalue) Sets the value of a field.void
setValue
(int fieldIndex, short newvalue) Sets the value of a field.void
Sets the value of a field.void
Sets the value of a field.void
store()
Stores the current Jet in the database.Methods inherited from class anderix.datajets.DataJet
clone, getId, getIdAsDatum
-
Constructor Details
-
Jet
Constructor that sets the id for Jet to use. Please note that this creation method does not load the record into the DataJet from the database. Use the staticloadJet
method for this, or overrideDataJet.load
.- Parameters:
id
- integer representing the identity field valuejetSet
- JetSet containing properties for the DataJet- See Also:
-
loadJet(int, anderix.datajets.JetSet)
DataJet.load(int, anderix.datajets.DataJet, anderix.datajets.JetSet)
-
Jet
Constructor that accepts a JetSet.- Parameters:
jetSet
- JetSet containing properties for the DataJet
-
-
Method Details
-
setValue
public void setValue(int fieldIndex, int newvalue) Sets the value of a field.- Parameters:
fieldIndex
- zero-based ordinal position of field infieldList
(defined when initializing the DataJet)newvalue
- int value to set the field to
-
setValue
public void setValue(int fieldIndex, boolean newvalue) Sets the value of a field.- Parameters:
fieldIndex
- zero-based ordinal position of field infieldList
(defined when initializing the DataJet)newvalue
- boolean value to set the field to
-
setValue
public void setValue(int fieldIndex, short newvalue) Sets the value of a field.- Parameters:
fieldIndex
- zero-based ordinal position of field infieldList
(defined when initializing the DataJet)newvalue
- short value to set the field to
-
setValue
public void setValue(int fieldIndex, long newvalue) Sets the value of a field.- Parameters:
fieldIndex
- zero-based ordinal position of field infieldList
(defined when initializing the DataJet)newvalue
- long value to set the field to
-
setValue
public void setValue(int fieldIndex, float newvalue) Sets the value of a field.- Parameters:
fieldIndex
- zero-based ordinal position of field infieldList
(defined when initializing the DataJet)newvalue
- float value to set the field to
-
setValue
public void setValue(int fieldIndex, double newvalue) Sets the value of a field.- Parameters:
fieldIndex
- zero-based ordinal position of field infieldList
(defined when initializing the DataJet)newvalue
- double value to set the field to
-
setValue
public void setValue(int fieldIndex, byte newvalue) Sets the value of a field.- Parameters:
fieldIndex
- zero-based ordinal position of field infieldList
(defined when initializing the DataJet)newvalue
- byte value to set the field to
-
setValue
Sets the value of a field.- Parameters:
fieldIndex
- zero-based ordinal position of field infieldList
(defined when initializing the DataJet)newvalue
- Object value to set the field to
-
setValue
Sets the value of a field.- Parameters:
fieldIndex
- zero-based ordinal position of field infieldList
(defined when initializing the DataJet)newvalue
- Datum value to set the field to- See Also:
-
getValue
Returns the value of a field.- Parameters:
fieldIndex
- zero-based ordinal position of field infieldList
(defined when initializing the DataJet)- Returns:
- Datum representation of the current value.
- See Also:
-
getValueAsObject
Returns the value of a field.- Parameters:
fieldIndex
- zero-based ordinal position of field infieldList
(defined when initializing the DataJet)- Returns:
- Object representation of the current value
-
loadJet
Loads a Jet with identity field equal toid
from the database.- Parameters:
id
- the unique value of the identity fieldjetSet
- JetSet containing properties for the DataJet- Returns:
- Jet containing data from the database
- Throws:
DataJetException
- if the connection to the database can not be established or if the DataJet has not been initialized
-
loadBySql
Loads an array of Jets based on a SqlBuilder object.- Parameters:
sql
- SqlBuilder defining the records to load.jetSet
- JetSet containing properties for the DataJet- Returns:
- array of Jet objects representing all records in the database
- Throws:
DataJetException
- if the connection to the database can not be established or if the DataJet has not been initialized
-
loadBySql
Loads an array of Jets based on an SQL statement.- Parameters:
sql
- String containing SQL specifying the records to load.jetSet
- JetSet containing properties for the DataJet- Returns:
- array of Jet objects representing all records in the database
- Throws:
DataJetException
- if the connection to the database can not be established or if the DataJet has not been initialized
-
loadAll
Loads an array of all Jets from the database.- Parameters:
jetSet
- JetSet containing properties for the DataJet- Returns:
- array of Jet objects representing all records in the database
- Throws:
DataJetException
- if the connection to the database can not be established or if the DataJet has not been initialized
-
store
Stores the current Jet in the database. This method can be used to add a new Jet or to update an existing one.- Specified by:
store
in interfaceStorable
- Throws:
DataJetException
- if the connection to the database can not be established or if the DataJet has not been initialized- See Also:
-
DataJet.store(java.sql.Connection)
-
remove
Removes the current Jet from the database.- Specified by:
remove
in interfaceRemovable
- Throws:
DataJetException
- if the connection to the database can not be established if the DataJet has not been initialized- See Also:
-
DataJet.remove(java.sql.Connection)
-