how can i retrieve data from multiple databases in a single query? - java

if i have multiple database with same Tables and Columns how can i retrieve Data from those Databases using single Query in Java.
Done this for single Database, i am newbie in java, please suggest.
public class MultipleDBTest{
public void dbConnect(String db_connect_string, String db_userid, String db_password){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
System.out.println("connected");
Statement statement = conn.createStatement();
String queryString = "select <Col1>, <Col2> from <Table>";
ResultSet rs = statement.executeQuery(queryString);
while(rs.next()){
System.out.println(rs.getString(1) + " | " + rs.getString(2));
}
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
ConnectMSSQLServer connServer = new ConnectMSSQLServer();
connServer.dbConnect("jdbc:sqlserver://localhost;databaseName=<Database1>","<Username>","<Password>");
}
}

The easiest way to get data from multiple servers is linking them, querying the data from each table using the fully qualified table name, i.e. Server.Database.Schema.Table, and make the union of all.
Yo can only specify the desired server in the fully qualified name, Server, if you link the other servers to the server where you're making the query.
You'd end up with something like this
select * from Server1.Database1.dbo.Table
union
select * from Server2.Database2.dbo.Table
union
select * from Server3.Database2.dbo.Table
Please, see this article to understand what are linked servers and how you set them up: Linked Servers (Database Engine).

Let Sql Server do the work for you. Create a view in one of the databases that references the data from the tables in the other databases. That way your code need only access one object in one database, the view.
This is easiest if the databases are on the same server. If the databases are on separate servers you will need to link them.

If by "multiple databases" - you mean multiple schemas in the same database, then you can use the schema name and make the JOIN. Also, ensure that you have sufficient privileges to read both the schemas. SQL query would be of the form:
select S1T1.Col1, S1T1.Col2, S2T1.Col1, S2T1.Col2
from Schema1.T1 S1T1, Schema2.T1 S2T1
where S1T1.Col1=S2T1.Col1
And if you mean multiple databases on different database instances, then you may have to create links between the database instances. Refer to this SO post for more information:
Querying data by joining two tables in two database on different servers
If the information helps, don't forget to vote. Thanks! :)

Database.Schema.Table when referencing tables
and
Database.Schema.Table.Column when referencing columns
You can write joins between databases this way and deffinately pull data from more than one database.
USE [DatabaseA]
SELECT * FROM DatabaseA.dbo.DSNA_tblMaiin
INNER JOIN DatabaseB.dbo.DSNB_tblMaiin ON DatabaseA.dbo.DSNA_tblMaiin.Serialnumber = DatabaseB.dbo.DSNB_tblMaiin.Serialnumber
INNER JOIN DatabaseB.dbo.DSNC_tblMaiin ON DatabaseA.dbo.DSNA_tblMaiin.Serialnumber = DatabaseC.dbo.DSNC_tblMaiin.Serialnumber

What you are looking for is the federation layer. the layer will parse the SQL, Queries per DB will be created. Those independent queries will get fired on DB and result will be joined based on where clause. There are some Antlr based SQL Grammars available on the net, so you use them for parsing the SQL and generating DB specific SQLs.

Related

Will ResultSet be updated with the underlying database?

Before I explain my problem I would like to say that I know the basics of JDBC but not really used to it.
I am using an updatable result set to hold data from 2 different tables, as in the following sample code:
searchQry = "SELECT ct.CustomerName, ct.Email, ct.PhoneNo, ot.ItemName
FROM CUSTOMER_TABLE ct JOIN ORDER_Table ot
ON ct.OrderID = ot.OrderID";
prestmt = dbcon.prepareStatement(searchQry, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
uprs = prestmt.executeQuery();
uprs.updateLong("ut.PhoneNo", 7240987456L);
uprs.updateString("otItemName", "GTA5");
uprs.updateRow();
I would like to know if I will update the database from somewhere else (not using the same result set object) while the result set, upsr, connected to the database, whether uprs will get updated with it or it will throw an error or it will go with the old data itself. Sorry if it a newbie question but I can't really test that on my DB without knowing the outcomes and safe measures.
Please, suggest me if there is any better way to update the underlining db along with the data in the ResultSet without having any transaction issues when changing from different places.
Using:
Oracle Database for JDBC connection.

UpdateString not implemented by SQLite JDBC driver

I have a table PERSON with more than 5 millions rows and I need to update field NICKNAME on each one of them based on the field NAME inside the same table.
ResultSet rs = statement.executeQuery("select NAME from PERSON");
while(rs.next())
{
// some parsing function like:
// Nickname = myparsingfunction(rs.getString("NAME"));
rs.updateString( "NICKNAME", Nickname );
rs.updateRow();
}
But I got this error:
not implemented by SQLite JDBC driver
I'm using sqlite-jdbc-3.8.11.2.jar downloaded at https://bitbucket.org/xerial/sqlite-jdbc/downloads.
I know I could use the following SQL query:
statement.executeUpdate("update PERSONS set NICKNAME = Nickname where ID = Id");
But that would take forever and I understand updating ResultSet would be faster. So what options do I have to update the table on the fastest way? Any other driver available? Should I move out of Java?
UPDATE
I was able to find a fast solution using below syntax. The block between CASE and END was a concatenated string that I built before executing the SQL query, so I could send all updates at once.
update PERSON
set NICKNAME= case ID
when 173567 then 'blabla'
when 173568 then 'bleble'
...
when 173569 then 'blublu'
end
where ID in (173567, 173568, 173569)
As you have encountered, the SQLite JDBC driver does not currently support the updateString operation. This can be seen in the source code for this driver.
I can think of three options:
As you stated in your question, you can select the name and ID of the person and then update the person by its ID. Those updates could be done in a batch (using PreparedStatement.addBatch()) to improve performance (tutorial).
Implement the method myparsingfunction in pure SQL so that the query could become UPDATE PERSONS SET NICKNAME = some_function(NAME).
Create an user-defined function (using org.sqlite.Function), implemented in Java, and call it inside the SQL. Example, taken from this answer:
Function.create(db.getConnection(), "getNickName", new Function() {
protected void xFunc() throws SQLException {
String name = value_text(0);
String nickName = ...; // implement myparsingfunction here
result(nickName);
}
});
and use it like this: UPDATE PERSONS SET NICKNAME = getNickName(NAME);
SQLite does not support stored procedures so that option is out of the table.
I'm not sure which of these options would provide the best performance (certainly using pure SQL would be faster but that may not be a viable solution). You should benchmark each solution to find the one that fits you.

Java Connection String to query from two database

I am having a problem. I have a query that checks one database table and updates another database table. I am using MySQL 5.1
UPDATE dldd.temp,test.temp
SET test.temp.name = dldd.temp.word
WHERE dldd.temp.id = test.temp.id
this is my SQL statement that is working fine. Now I want to execute this statement using Java PreparedStatement . The problem is I don't know how to write the Connection String to select two database i.e
"jdbc:mysql://localhost:3306/"+dbname+"?characterEncoding=UTF-8"
What should come in place of dbname. Can I select multiple db there.
Have a look at http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html.
If the database is not specified, the connection is made with no default database. In this case, either call the setCatalog() method on the Connection instance, or fully specify table names using the database name (that is, SELECT dbname.tablename.colname FROM dbname.tablename...) in your SQL. Opening a connection without specifying the database to use is generally only useful when building tools that work with multiple databases, such as GUI database managers.

How to connect two database through JDBC? Is it possible?

I have tried to connect the mysql database with the frontend with the help of JDBC driver. But i dont know how acna we implement connectivity to connect the two different databases with each other with the help of JDBC driver.
You can create two connections. One for the first database and the other for the second database. You can send commands to the first database using the first connection and you can send commands to the second database using the second connection. Your application will serve the purpose of connecting the two databases as you can select rows from one database, parse them and insert the resulting records into the other.
There is no magic in JDBC that allows you to 'connect the two database with each other'. You need to code this yourself. You create two connections, one for each database and then you write the queries and transformations to get your data from database 1 to database 2.
try (
Connection connectionToDb1 = DriverManager.getConnection(
"jdbc:firebirdsql://serverA/database1", "username", "password");
Connection connectionToDb2 = DriverManager.getConnection(
"jdbc:firebirdsql://serverB/database2", "username", "password");
Statement selectFrom1 = connectionToDb1.createStatement();
ResultSet rsFrom1 = selectFrom1.executeQuery(
"SELECT columnA, columnB FROM tableX");
PreparedStatement insertTo2 = connectionToDb2.prepareStatement(
"INSERT INTO tableY(column1, column2) VALUES (?, ?)");
) {
while (rsFrom1.next()) {
insertTo2.setString(1, rsFrom1.getString("columnA"));
insertTo2.setString(2, rsFrom1.getString("columnB"));
insertTo2.executeUpdate();
}
}
Note that this isn't a complete example: for production purposes you would disable auto commit, and use batch updates.
There are tools that can do this for you, but tool and library suggestions are off topic on SO, but I'd suggest you search for ETL (or extract, transform, load), or maybe for datapump.

How to search and insert a value using java code?

String link = "http://hosted.ap.org";
I want to find whether the given url is already existing in the SQL DB under the table name "urls". If the given url is not found in that table i need to insert it in to that table.
As I am a beginner in Java, I cannot really reach the exact code.
Please advise on this regard on how to search the url in the table.
I am done with the SQL Connection using the java code. Please advise me on the searching and inserting part alone as explained above.
PreparedStatement insert = connectin.preparedStateme("insert into urls(url) vlaues(?)");
PreparedStatement search = connectin.preparedStateme("select * from urls where url = ?");
search.setString(1, <your url value to search>);
ResultSet rs = search.executeQuery();
if (!rs.hasNext()) {
insert.setString(1, <your url value to insert>);
insert.executeUpdate();
}
//finally close your statements and connection
...
i assumed that you only have one field your table and field name is url. if you have more fields you need to add them in insert query.
You need to distinguish between two completely separate things: SQL (Structured Query Language) is the language which you use to communicate with the DB. JDBC (Java DataBase Connectivity) is a Java API which enables you to execute SQL language using Java code.
To get data from DB, you usually use the SQL SELECT statement. To insert data in a DB, you usually use the SQL INSERT INTO statement
To prepare a SQL statement in Java, you usually use Connection#prepareStatement(). To execute a SQL SELECT statement in Java, you should use PreparedStatement#executeQuery(). It returns a ResultSet with the query results. To execute a SQL INSERT statement in Java, you should use PreparedStatement#executeUpdate().
See also:
SQL tutorial
JDBC tutorial

Categories