Retrieving a row and column from Access Database - java

In my project are two matrices using Microsoft Access database, sourced from two different tables (for example TABLE_A and TABLE_B). I need to retrieve a row and a column from that database.
Selecting COL1 from one table means selecting a corresponding row from the other.
I would like to retrieve COL1 and the corresponding row at the same time.
How is this possible using SQL and a JDBC call?
Thank you!

In order to work with two separate tables of the same database you will need one Connection and a Statement and a ResultSet for each database table.
Since you do not clarify how you have stored your matrix data in the database i can not give you more details here. But, whatever you do, in order to access the contents of the first matrix from the database table you will have to replicate for the second. Just change the table name in your SQL select.
However it will not happen "at the same time", one query will follow the other.

Related

How can I copy an sqlite database to memory?

I wanted to copy a ".db" file from an SQLite database to memory using JAVA code. How can I do this ?
What you could do is :-
Open an in-memory database and attach the database you want to copy.
Create the tables and copy the data for each table (you can ascertain the table and other entity names from sqlite_master (name, type and table columns))
You could use CREATE TABLE x AS SELECT * FROM attached_database_identifier.x
noting limitations as per [SQL As Understood By SQLite - CREATE TABLE - CREATE TABLE ... AS SELECT Statements](https://www.sqlite.org/lang_createtable.html#rowid))
the above is for the table name x
Alternately you could create the tables based upon the SQL that is stored in sqlite_master in the sql column.
Create the other entities Indexes, Views and Triggers (again the sql column of sqlite_master will hold the SQL to create the entities).
Indexes would then be built accordingly.
Triggers would then work (you wouldn't want to create them before the data has been copied from the original tables as the triggers would have done their work).
Views extract the resultant data from the tables when used.
detach the attached database.
You now have the in-memory copy.

Efficiant way to check large number string existing in database

I have a very large table in the database, the table has a column called
"unique_code_string", this table has almost 100,000,000 records.
Every 2 minutes, I will receive 100,000 code string, they are in an array and they are unique to each other. I need to insert them to the large table if they are all "good".
The meaning of "good" is this:
All 100,000 codes in the array never occur in the database large table.
If one or more codes occur in the database large table, the whole array will not use at all,
it means no codes in the array will insert into the large table.
Currently, I use this way:
First I do a loop and check each code in the array to see if there is already same code in the database large table.
Second, if all code is "new", then, I do the real insert.
But this way is very slow, I must finish all thing within 2 minutes.
I am thinking of other ways:
Join the 100,000 code in a SQL "in clause", each code has 32 length, I think no database will accept this 32*100,000 length "in clause".
Use database transaction, I force insert the codes anyway, if error happens, the transaction rollback. This cause some performance issue.
Use database temporary table, I am not good at writing SQL querys, please give me some example if this idea may work.
Now, can any experts give me some advice or some solutions?
I am a non-English speaker, I hope you see the issue I am meeting.
Thank you very much.
Load the 100,000 rows into a table!
Create a unique index on the original table:
create unique index unq_bigtable_uniquecodestring on bigtable (unique_code_string);
Now, you have the tools you need. I think I would go for a transaction, something like this:
insert into bigtable ( . . . )
select . . .
from smalltable;
If any row fails (due to the unique index), then the transaction will fail and nothing is inserted. You can also be explicit:
insert into bigtable ( . . . )
select . . .
from smalltable
where not exists (select 1
from smalltable st join
bigtable bt
on st.unique_code_string = bt.unique_code_string
);
For this version, you should also have an index/unique constraint on smalltable(unique_code_string).
It's hard to find an optimal solution with so little information. Often this depends on the network latency between application and database server and hardware resources.
You can load the 100,000,000 unique_code_string from the database and use HashSet or TreeSet to de-duplicate in-memory before inserting into the database. If your database server is resource constrained or there is considerable network latency this might be faster.
Depending how your receive the 100,000 records delta you could load it into the database e.g. a CSV file can be read using external table. If you can get the data efficiently into a temporary table and database server is not overloaded you can do it very efficiently with SQL or stored procedure.
You should spend some time to understand how real-time the update has to be e.g. how many SQL queries are reading the 100,000,000 row table and can you allow some of these SQL queries to be cancelled or blocked while you update the rows. Often it's a good idea to create a shadow table:
Create new table as copy of the existing 100,000,000 rows table.
Disable the indexes on the new table
Load the delta rows to the new table
Rebuild the indexes on new table
Delete the existing table
Rename the new table to the existing 100,000,000 rows table
The approach here is database specific. It will depend on how your database is defining the indexes e.g. if you have a partitioned table it might be not necessary.

How to get column from other tables, to form a new table?

I'm relatively new to working with JDBC and SQL. I have two tables, CustomerDetails and Cakes. I want to create a third table, called Transactions, which uses the 'Names' column from CustomerDetails, 'Description' column from Cakes, as well as two new columns of 'Cost' and 'Price'. I'm aware this is achievable through the use of relational databases, but I'm not exactly sure about how to go about it. One website I saw said this can be done using ResultSet, and another said using the metadata of the column. However, I have no idea how to go about either.
What you're probably looking to do is to create a 'SQL View' (to simplify - a virtual table), see this documentation
CREATE VIEW view_transactions AS
SELECT Name from customerdetails, Description from cakes... etc.
FROM customerdetails;
Or something along those lines
That way you can then query the View view_transactions for example as if it was a proper table.
Also why have you tagged this as mysql when you are using sqlite.
You should create the new table manually, i.e. outside of your program. Use the commandline 'client' sqlite3 for example.
If you need to, you can use the command .schema CustomerDetails in that tool to show the DDL ("metadata" if you want) of the table.
Then you can write your new CREATE TABLE Transactions (...) defining your new columns, plus those from the old tables as they're shown by the .schema command before.
Note that the .schema is only used here to show you the exact column definitions of the existing tables, so you can create matching columns in your new table. If you already know the present column definitions, because you created those tables yourself, you can of course skip that step.
Also note that SELECT Name from CUSTOMERDETAILS will always return the data from that table, but never the structure, i.e. the column definition. That data is useless when trying to derive a column definition from it.
If you really want/have to access the DB's metadata programatically, the documented way is to do so by querying the sqlite_master system table. See also SQLite Schema Information Metadata for example.
You should read up on the concept of data modelling and how relational databases can help you with it, then your transaction table might look just like this:
CREATE TABLE transactions (
id int not null primary key
, customer_id int not null references customerdetails( id )
, cake_id int not null references cakes( id )
, price numeric( 8, 2 ) not null
, quantity int not null
);
This way, you can ensure, that for each transaction (which is in this case would be just a single position of an invoice), the cake and customer exist.
And I agree with #hanno-binder, that it's not the best idea to create all this in plain JDBC.

insert into a local table from a distant table in Oracle

i would like to insert into a local table (in a local database), all the rows from a distant table. here's what i'm looking for :
insert into LocalTable (Column1,Column2,...,ColumnN) values (select * from DistantTable);
does anybody knows how could i do this (if there is a way)??
i'm aweare that there is a way using a java program, by copying the DistantTable rows in a file, then extracting those rows using a StringTokenizer then putting them to LocalTable. but it would be really good if i can perform this using only SQL queries.
You can create a database link in the local database, pointing at the remote database, and then type:
INSERT INTO LocalTable SELECT * FROM RemoteTable#DBLink;

How to select data of one table and insert in other table?

I have a project in java where i want that certain data from one table (that is in Sql management studio) is selected and that is inserted in other table. So that i can access data on a jsp page from second table. How to do this?
One method would be to iterate through the table while writing the values into an array. Once the data has been stored into the array you can re-iterate through the array but this time inserting the values into a new table.
This may not be the most efficient method, I am sure someone else will chime in if so.
Another method which does not require Java would be to use the Select As statement in SQL, see example.
CREATE TABLE suppliers
AS (SELECT *
FROM companies
WHERE id > 1000);
Or if you already have a table created you can do the following,
INSERT INTO suppliers
(supplier_id, supplier_name)
SELECT account_no, name
FROM customers
WHERE city = 'Newark';
If you use SQL, you can use SELECT INTO statements to achieve this easily:
SELECT Column1,Column2
INTO SecondTable
FROM FirstTable
WHERE Column3='Whatever'
This will copy the data from FirstTable into SecondTable.
See This Link for more examples

Categories

Resources