Class GenericDataJet
GenericDataJet, 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, GenericDataJet 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 GenericDataJet 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
GenericDataJet, 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 GenericDataJet 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); GenericDataJet dj = new GenericDataJet(jetSet); dj.setFieldValue(1, "new value 1"); dj.setFieldValue(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); GenericDataJet dj = GenericDataJet.load(1, jetSet); //load record with id = 1; System.out.println(dj.getFieldValue(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 GenericDataJet:
import anderix.datajets.*; class MyDataJet extends GenericDataJet { 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.setFieldValue(1, "new value 1"); dj.setFieldValue(2, "new value 2"); dj.store();
- See Also:
ConnectionGenerator
,SqlBuilder
-
Constructor Summary
ConstructorDescriptionGenericDataJet(int id, JetSet jetSet)
Constructor that sets the id for GenericDataJet to use.GenericDataJet(JetSet jetSet)
Constructor that accepts a JetSet. -
Method Summary
Modifier and TypeMethodDescriptiongetFieldValue(int fieldIndex)
Returns the value of a field.getFieldValueAsObject(int fieldIndex)
Returns the value of a field.static GenericDataJet[]
loadAllJets(JetSet jetSet)
Loads an array of all GenericDataJets from the database.static GenericDataJet
Loads a GenericDataJet with identity field equal toid
from the database.static GenericDataJet[]
loadJetsBySql(SqlBuilder sql, JetSet jetSet)
Loads an array of GenericDataJets based on a SqlBuilder object.static GenericDataJet[]
loadJetsBySql(String sql, JetSet jetSet)
Loads an array of GenericDataJets based on an SQL statement.void
remove()
Removes the current GenericDataJet from the database.void
setFieldValue(int fieldIndex, boolean newvalue)
Sets the value of a field.void
setFieldValue(int fieldIndex, byte newvalue)
Sets the value of a field.void
setFieldValue(int fieldIndex, double newvalue)
Sets the value of a field.void
setFieldValue(int fieldIndex, float newvalue)
Sets the value of a field.void
setFieldValue(int fieldIndex, int newvalue)
Sets the value of a field.void
setFieldValue(int fieldIndex, long newvalue)
Sets the value of a field.void
setFieldValue(int fieldIndex, short newvalue)
Sets the value of a field.void
setFieldValue(int fieldIndex, Datum newvalue)
Sets the value of a field.void
setFieldValue(int fieldIndex, Object newvalue)
Sets the value of a field.void
store()
Stores the current GenericDataJet in the database.Methods inherited from class anderix.datajets.DataJet
clone, getId, getIdAsDatum
-
Constructor Details
-
GenericDataJet
Constructor that sets the id for GenericDataJet 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)
-
GenericDataJet
Constructor that accepts a JetSet.- Parameters:
jetSet
- JetSet containing properties for the DataJet
-
-
Method Details
-
setFieldValue
public void setFieldValue(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
-
setFieldValue
public void setFieldValue(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
-
setFieldValue
public void setFieldValue(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
-
setFieldValue
public void setFieldValue(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
-
setFieldValue
public void setFieldValue(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
-
setFieldValue
public void setFieldValue(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
-
setFieldValue
public void setFieldValue(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
-
setFieldValue
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
-
setFieldValue
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:
Datum
-
getFieldValue
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:
Datum
-
getFieldValueAsObject
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 GenericDataJet with identity field equal toid
from the database.- Parameters:
id
- the unique value of the identity fieldjetSet
- JetSet containing properties for the DataJet- Returns:
- GenericDataJet 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
-
loadJetsBySql
public static GenericDataJet[] loadJetsBySql(SqlBuilder sql, JetSet jetSet) throws DataJetExceptionLoads an array of GenericDataJets based on a SqlBuilder object.- Parameters:
sql
- SqlBuilder defining the records to load.jetSet
- JetSet containing properties for the DataJet- Returns:
- array of GenericDataJet 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
-
loadJetsBySql
Loads an array of GenericDataJets 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 GenericDataJet 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
-
loadAllJets
Loads an array of all GenericDataJets from the database.- Parameters:
jetSet
- JetSet containing properties for the DataJet- Returns:
- array of GenericDataJet 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 GenericDataJet in the database. This method can be used to add a new GenericDataJet 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 GenericDataJet 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)
-