Database: Oracle 11gr2;
Web application : JSP(Java)
I have two tables:
Cities(***Id***, Name); Id is the Primary Key
Persons(***IdPers***, Name, IdCity_FK); IdCity is Foreign Key
IdCity_FK reffers to column Id from Cities.
The question: how to insert data from a insert.jsp page which has the following fiels:Person's name, City.
I want the user to enter his name and to select a city from some ListBox dynamically generated (using Cursor Oracle or a other method). For instance, one does not enter : Andrew, 12, but enters Andrew and select New York.
I managed to do this by using Oracle REF CURSOR, but when clicking submit I get error. I think it does not pass an int value as parameter.
The code
create or replace
FUNCTION GET_CITIES
RETURN CITYPKG.ref_cursor
AS cities_cursor CityPkg.ref_cursor;
BEGIN
OPEN cities_cursor FOR
SELECT id, nume FROM CITY;
RETURN CITIES_cursor;
END;
In the JSP page for editing I have a jspforward tag which sends me to a page which handles the form with request.getParameter... and calls a function which has 2 parameters: one is String the other one is int. The int one inserts the corresponding foreign key in the database.
Thank you in advance.
There was an error at JSP file(parameter).
Related
I am creating a simple android application in android studio with java.
I have a roomdatabase db with a table named user that has four columns
name, profession, age, description and I have prepopulated using a database that i made in sqlite studio.
Now, I want to add a column surname on the table but I want to delete all the prepopulated data and prepopulate again the table with a new database that contains also surname.
At first I thought to use auto migrations and to just add a new column. But i don't know how delete all the existing data and prepopulate again the database.
I want to delete the existing data because i want to change all the info that exists in the column description. Also as concern as the column name now it contains the fullname and in some cases is written line "name surname" and other times like "surname name" e.x "Will Smith" "Smith Will". Now I want to have the name and the surname in separate columns name and surname
Could someone recommend me something? Thank you in advance
AutoMigration will not cope with amending the data itself. Thus you will have to do that manually and thus use a destructive migration.
Here's a example (as can be seen actually undertaken)
Note this assumes no version or version 1 was assigned to the original pre-populated database and also that 1 was used for the version passed to the #Database annotation.
an SQLite database has, as part of it's header, a user_version number (offset 60 for 4 bytes). It is comparing this to the version passed to Room that determines the migration/auto migration. As is seen changing this is critical if migrating.
If developing you could just start from the changed database by making the changes to the database and the App and uninstalling the App. No need to play with version numbers.
Amend the User class by adding the new surname column. e.g.
:-
#Entity
class User {
#PrimaryKey
#NonNull
String name;
String profession;
int age;
String description;
/* ADDED FOR V2 */
String surname;
}
Compile (Ctrl + F9) and then from Android View located the generated Java and then the class that is then same as the #Database class but suffixed with _Impl.
locate the createAllTables method and then the SQL for the User table. Make a note of the SQL and the definition for the new column e.g. -surname TEXT
In SQLite Studio, run the following SQL:- ALTER TABLE user ADD COLUMN surname TEXT; where the text/code after the COLUMN key word is EXACTLY as per the SQL noted above (you can include or omit the enclosing `'s around the column name, they aren't easy to display in SO)
Look at the Data e.g. it will now be :-
note that the surname column is populated with nulls.
Edit the data accordingly. e.g. :-
Then run the following SQL PRAGMA user_version; (to check the current version)
Then run the following SQL PRAGMA user_version = 2; ( to change the version (guessing 2))
Then run the following SQL PRAGMA user_version; (to check that the version is now 2)
Quit SQLite Studio
Replace the file/asset in the project with the new database. e.g. :-
In the #Database class (NOT the generated java) :-
change the database version to 2
add the following to the the databaseBuild :-
either .fallbackToDestructiveMigrationFrom(1) if going from 1 to 2
or .fallbackToDestructiveMigration() (not as safe but more encompassing)
Run the App and e.g. :-
I'm new to PostgreSQL and Java Swing.
In PostgreSQL company database, I have a users table and it has 4 fields: user_id, username, phone, and address.
CREATE TABLE users
(
user_id serial primary key,
username VARCHAR(40) not null,
phone VARCHAR(14) not null,
address VARCHAR(50)
);
I'm trying to load all fields from users table to JTable in Java Swing using Bound (Figure 1).
Then I bind the elements from the table (Figure 2).
As you can see in Figure 2, it shows only 3 fields except user_id. I need to load this user_id field as well because I need to perform CRUD data.
How can I achieve that?
I finally got the solution.
When I import the data from the database to the form, the Model (for example; Users.java) file is generated.
The problem was I firstly generated that file and later I added the user_id column to the database, so guess what, the Model file is somehow not updated and so the user_id is not there.
Therefore, I had manually added getter and setter for the user_id field, and now ok.
I have a Spring new web application(Java), and after having the user login (using username and password), I want to make the user able to add a new user, till then all is good. Each user have a primary Key iduser which autoincrimented (AI), and a foreign key idprofile. The table Profile contain informations about the user, and it's idprofile is the primary key (which have to be the same as the ipdrofile in User table).
As you may know, MySQL don't let you make more than one AI per table (MySQL 5 workbench as platform), and when my code add's an user, it has to add a profile in the same time, but with three fields that are unknown, which are iduser, idprofile (user table) and idprofile (profile table). As the ipdrofile can't be an AI, how can I manage to put the right key on it when saving the User and Profile (Adding) ?
You insert the profile first, returning the new profile id, then you insert the user, using the returned profile id, optionally returning the new user id if you need it.
You'll need to use these two methods to get the auto-generated key returned:
Connection.prepareStatement(String, String[])
Statement.getGeneratedKeys()
I'm trying to create a stored procedure that will be used by a client application to search for customers in a customers table and then return all the info about the customer if he's found. I've created a procedure to add a customer to the table:
DELIMITER $
CREATE PROCEDURE `add_cust` (IN custF VARCHAR(100), IN custL VARCHAR(100))
BEGIN
INSERT INTO customers (cFirst, cLast) VALUES(custF, custL);
END $
And it works.
My customers table is very simple - an auto-incrementing primary key, fist and last name columns.
I can't wrap my head around this other search procedure. The way I see it is to have two procedures for searching. One procedure uses First Name to search for customer, and the second one will use Last Name.
I assume I'll have to use a cursor FOR SELECT with WHERE clause, and a WHILE loop. But how do I return the result to the client application? Do I declare one of the parameters in the stored procedure as OUT? Or do I just declare one parameter as INOUT?
So far this is where I am at:
DELIMETER $
CREATE PROCEDURE `searchCustByFirst` (IN custF VARCHAR(100))
BEGIN
END $
It is very simply -
CREATE PROCEDURE searchCustByFirst(IN custF VARCHAR(100))
BEGIN
SELECT cFirst, cLast FROM customers WHERE cFirst = custF;
END
This procedure will return dataset, just read it in the application.
Another solution is to use OUT parameters, for example -
CREATE PROCEDURE searchCustByFirst(IN custF VARCHAR(100), OUT custL VARCHAR(100))
BEGIN
SELECT cLast INTO custL FROM customers WHERE cFirst = custF;
END
In this case SELECT statement cannot return more then one record, so criteria field cFirst should be unique.
I have a table xxx with id (id_xxx int AUTO_INCREMENT ) and name (name_xxx varchar (50)),
When I insert a new row in the table I made:
INSERT INTO xxx VALUES ("name for test");
and the result (int=1) of insertion is returned, then I display in my java interface a message "succseed!", until now it's a very basic and simple operation...
BUT,
when I want to return the inserted id_xxx,I have to do another query to the database:
INSERT INTO xxx VALUES ("name for test");
//after the insert response I made:
SELECT MAX (id_xxx) FROM xxx;
and I display in my java interface "succseed $$$ is your id_xxx "....
the second version can easily cause a serious error during concurrent access to multiple users:
imagine a case when a user1 makes an insert... and then H2DB interrupt operations of this user then executes the insert of user2.
when user1 executes a select max (id_xxx) the H2DB return A FALSE id_xxx...
(I hope that my example is clear otherwise I will schematize this problem).
how to solve this problem?
You should be able to retrieve keys generated by insert query, see 5.1.4 Retrieving Automatically Generated Keys.