CreateCityDB.java @ http://www.datafilehost.com/download-39868b74.html Compile a
ID: 3530920 • Letter: C
Question
CreateCityDB.java @ http://www.datafilehost.com/download-39868b74.html
Compile and run the CreateCityDB program. The program will create a Java DB database named cityDB. The CityDB database will have a table named City with the following columns:
Column Name Data Type
CityName CHAR (50)
Primary Key
Population Double
The CityName column stores the name of a city and the Population column stores the population of that city. After you run the CreateCityDB.Java program, the City table will contain 20 rows with various cities and their populations.
Next, write a program that connects to the CityDB database, and allows the user to select any of the following operations:
Sort the list of cities by population, in ascending order.
Sort the list of cities by population, in descending order.
Sort the list of cities bv name.
Get the total population of all the cities.
Get the average population of all the cities.
Get the highest population.
Get the lowest population.
Explanation / Answer
this will help you Now that you have access to the database, you can save objects via the save() method. For example: var db = ScriptDb.getMyDb(); var ob = {type: "employee", employee_id: 1, name: {first: "Fatima", initial: "S", last: "Pauli"}, address: {street: "4076 Washington Avenue", city: "Jackson", state: "MS", zip: "39201"}, department_id: 52}; var stored = db.save(ob); Note: Currently, attribute names cannot contain any special characters like dots or square brackets. Attribute values do not have this restriction. Modifying Data Let's take a look at the stored object from the section above. In order to make changes to an object, you must have an object that has been fetched from the database by a call to load() or query(), or returned from save(). Objects loaded from the database are aware of their id, which you can get via a call to getId() and then use later via a call to load(). For example, starting from the above code snippet, you could modify the data you just saved: stored.foo='bar'; db.save(stored); Alternatively, you could query and modify those objects. For example: var ob = db.query({employee_id: 29}).next(); ob.name.first = 'Fred'; db.save(ob); Loading By Id You can load an object via a call to load if you know the id of the object. var loaded = db.load(stored.getId()); Object ids can be used as foreign key values from other objects. Depending on your needs, you may want to load more than one object, such as a case where you have an array of ids, var loaded = db.load([id_1, id_2, id_3]); Here, id_1, id_2, and id_3 are variables which hold the string values of the ids (specifically, the ids here are not literals). Deleting Data There are two ways to delete objects from the database. db.remove(some_object_you_loaded); Or, if you have object id available: db.remove(object_id); Bulk Operations If you know in advance that there are a number of objects you want to work on all at once, ScriptDb also allows for bulk operations. For example, there's db.saveBatch(). var saveResults = db.saveBatch(someArrayOfObjects, false); A few things here need to be explained. First the false argument at the end. This is the atomicity flag, which if true, says to store all of the objects together or not at all. Currently, if the array of objects contains two or more items, this is required to be false. The second thing is the save results. Because there is a possibility, though rare, of some objects being saved, but not others, the results may contain MutationResult objects indicating failure to save for the corresponding item in the someArrayOfObjects array. Because it's infrequent for this to occur, there is the db.allOk() method you can use to quickly check for success. For example: var results = db.saveBatch(array_of_obs, false); if (db.allOk(results)) { // everything went swimmingly, proceed } else { // partial or no success for (var i = 0 ; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.