I am trying to find out a SQL query through which i can get to know if i am connected to database or not. I will be using JAVA program for executing this query. I am in search of a generic query that when executed from my java program can give me a result if i am connected to database. I am not aware of the database structure(tables in database), i just know database name, to which i am trying to connect.
I am in search of a generic SQL query which can work on any database irrespective of the underlying database such as SQL Server, MySql, Oracle or any other.
Till now i have got some queries but they are database specific they cannot be used with every database.
select count(*) as TablesCount from sys.tables :- this query is
SQL server specific.
select 1 :- works for every database but according to me this
is not a good aproach.
show tables :- i cannot use show tables as thiquery displays
the name of tables in the database
but i am just concerned whether i can access database or not.
question 1:- Please suggest if i can make a query that can execute irrespective of underlying database(I am not aware of database structure).
question 2:-
can i store result of query (query - show tables) in resultset.
Related
I want to update a column of a table. But the required Data is available in another oracle DB.Which is the best way to copy data from remote DB to my DB.
Should I go with Java program or can I achieve it in PL/SQL itself?
If I correctly understood the question, you need to set the value of a column with a value extracted from another table of another DB. The two DBs have different structure.
In this case you can do it with just SQL and a database link.
Here's how to create an Oracle database link: Oracle documentation for database links
Then you can write a query like following:
UPDATE local_table
SET local_column = (SELECT remote_column FROM remote_table#remote_db WHERE ...)
WHERE ...
I have the following query which is being used in Java JDBC
INSERT INTO account_photos (token, image, image_id, order_count) SELECT ?, ?, ?, ? WHERE NOT EXISTS (SELECT 1 from account_photos where image_id = ?)
It looks like it is causing a very high server load and bandwidth usage as it gets the image even if image_id is in the table.
Is there anyway to not to get the image if the NOT EXISTS is not true?
In this query, the image data will be transferred to the server before the query starts. Query processing happens in phases:
Parse: Read the query text from the client, validate its syntax and turn it into an internal structure that describes the query
Bind: Read the query parameters from the client and match them up to placeholders in the parsed query
Execute: Actually run the query
The IF NOT EXISTS (SELECT ...) happens in the execute phase. The bind phase has already finished, and it's the bind phase where the image data is transferred across the network to be stored temporarily in the database server's memory.
You cannot make PostgreSQL run part of the query before binding parameters. PostgreSQL does not support lazy parameter binding, where it calls back to the client to ask for parameter data only when the query needs it. There's nothing fundamentally impossible about that, it's just not supported by the current PostgreSQL protocol, client drivers, or database server.
Thus, you have to do this in two phases, as two separate queries. Check if the image exists, and only if it does not exist, insert it.
Note: Your query can try to insert the image twice because two concurrent queries can both return false for not exists (select...), and thus can both proceed to insert.
How to insert data in different tables using MySql database?
like i want to insert cid=1,cname=sahil,bid=12345,amount=12000
customer
cid,cname
bankb
bid,cid,amount
MySQL Does not support multiple table data insert using single query,
however Oracle does it.
for MySQL you have to use multiple insert query
like
INSERT INTO NAMES VALUES(...)
INSERT INTO PHONES VALUES(...)
Source : sql - insert into multiple tables in one query
Its not efficient with simple query and so use Stored procedures. Refer the below links.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/77ce4b34-581b-47c8-aad6-96910ecd8ab5/correct-way-to-insert-data-into-multiple-tables-stored-procedure?forum=sqlgetstarted
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;
my table is:
[tableAbc]
A B C
------------
1 a b
2 c e
3 e r
...
run:
show create table tableAbc;
to get the create table sql
but how to get insert sql?
like:
insert into `tableAbc` ('A','B','C') VALUES(1,'a','b');
...
any idea? (or any java library to do this)
thanks all!
BTW:
i want show the "insert sql" to web brower.
so,i think i need get "insert sql" under java, or sql commands.
If you use Squirrel (A SQL tool) it can generate this for you.
I use this tool daily (it is free and it works well)
As per their description
SQuirreL SQL Client is a graphical
Java program that will allow you to
view the structure of a JDBC compliant
database, browse the data in tables,
issue SQL commands etc,
To get the insert statement for a table just right click on a table and select "data scripts"
Your question is difficult to understand due to everything being in sentence-fragments, but I think you're basically just looking for a way to be able to send SQL statements to your SQL database from Java, specifically an insert statement?
If this is the case, Java has an sql package. You can create a connection to your database, create a statement, and execute it. This will then return a ResultSet which is the result of the executed statement. Start here perhaps? http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Connection.html#createStatement%28%29 And there is part of an example on the documentation for ResultSet http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSet.html
What do you mean by "i want to show the insert sql in the browser"? Do you mean you want to show what the result of the executed statement is?