How to Retrieve Data from Two Different MySQL Database using JDBC? - java

How to retrieve data from two different MySQL database in a single query using jdbc?
SELECT
a.`driver_id`,
a.`state`,
a.`lat`,
a.`long`,
MAX(a.`time_stamp`) AS timestamp,
b.vehicle_type
FROM `rl_driv_location` AS a, `rl_driver_info` AS b
WHERE a.`state` = 1 AND a.`driver_id` = b.`user_id`
AND b.`vehicle_type` = '"+ cartype +"'
GROUP BY driver_id
location database having the rl_driv_location table.
core database having the rl_driver_info table.

Just specify the name of the databases where you want to retrieve data. See below.
SELECT
a.`driver_id`,
a.`state`,
a.`lat`,
a.`long`,
MAX(a.`time_stamp`) AS timestamp,
b.vehicle_type
FROM location.`rl_driv_location` AS a, core.`rl_driver_info` AS b
WHERE a.`state` = 1 AND a.`driver_id` = b.`user_id`
AND b.`vehicle_type` = '"+ cartype +"'
GROUP BY driver_id

Related

How to retrieve data and count of data at same time in hql?

I want to retrieve count and list of data in one query only which I want to write on JPA repository. I wrote it using a constructor and executed using entity manager, but it didn't work. It gave me a QuerySyntaxException. Here is my query:
String hql = "select new core.abc(select count(*) from abc as m where m.Id in :Ids and m.Type = :Type,"
+ "select max(m.modificationTime) from abc as m where m.Id in :Ids and m.Type = :Type )";
How can I write such kind of query in JPA repository?
I just figure out your use case senario is that you want to get all records from table as well as total-count of records and max value of a specific column , you named that column as modificationTime. So onething will be happen in this case, if you want to intract with table with single query, Than you will get useless data for both column named as max and count.
Try This for JPA,
#Query(value="SELECT cb from abc cd where cd.Id in (?1) and cd.Type=?2 , (SELECT MAX(m.modificationTime) as maxModificationTime , COUNT(*) as count FROM abc m where m.Id in (?3) and m.Type=?4) as m",nativeQuery=true)

Fetch data from mysql using EBean

I am trying to make a simple program to fetch data from a table.
I am following http://www.avaje.org/ebean/getstarted_props.html#iud but am unable to get data. I have created new Entity Class from Database from Netbeans (which creates classes from relations). Here is what I am using:
ebean.properties
ebean.ddl.generate=true
ebean.ddl.run=true
ebean.debug.sql=true
ebean.debug.lazyload=false
ebean.logging=all
ebean.logging.logfilesharing=all
ebean.logging.directory=D:\\logs
ebean.logging.iud=sql
ebean.logging.query=sql
ebean.logging.sqlquery=sql
ebean.logging.txnCommit=none
datasource.default=h2
datasource.h2.username=sa
datasource.h2.password=
datasource.h2.databaseUrl=jdbc:h2:mem:tests;DB_CLOSE_DELAY=-1
datasource.h2.databaseDriver=org.h2.Driver
datasource.h2.minConnections=1
datasource.h2.maxConnections=25
datasource.h2.heartbeatsql=select 1
datasource.h2.isolationlevel=read_committed
datasource.mysql.username=root
datasource.mysql.password=kalsym#123
datasource.mysql.databaseUrl=jdbc:mysql://127.0.0.1:3306/wsp
datasource.mysql.databaseDriver=com.mysql.jdbc.Driver
datasource.mysql.minConnections=1
datasource.mysql.maxConnections=25
datasource.mysql.isolationlevel=read_committed
Table Data
Insert into routing_algo_type (name, description) values ('LCR', 'Least Cost Routing');
Code to fetch data
RoutingAlgoType routingObj = new RoutingAlgoType();
routingObj.setName("LCR");
RoutingAlgoType routingObj2 = Ebean.find(RoutingAlgoType.class, routingObj);
System.out.println("Got "+routingObj2.getDescription());
Now find returns null, which means it cant find the data?
I used following code to test connection
String sql = "select count(*) as count from dual";
SqlRow row = Ebean.createSqlQuery(sql).findUnique();
Integer i = row.getInteger("count");
System.out.println("Got " + i + " - DataSource good.");
Result from above code is
Got 1 - DataSource good.
Is there any way to check the connection?

SQL - Return row if Boolean is True

Im trying to create an SQL Statement that will differentiate between employees types. For example if the Boolean Type Manager Column is checked it will return. Im using the info to fill a Manager on Duty JCombo in Java.
Im trying
String sql = "SELECT Employees.Name FROM Employees WHERE Manager = 'true' ORDER BY Name ASC";
Cant seem to get it right.
In SQL the boolean field will be a bit so your SQL statement will need to be
String sql = "SELECT Employees.Name FROM Employees WHERE Manager = 1 ORDER BY Name ASC";
in SQL a boolean field is a bit field (0 or 1) so you have to check as:
String sql = "SELECT Employees.Name FROM Employees WHERE Manager = 1 ORDER BY Name ASC"

PostgreSQL: Update my user table via Java

In PostgreSQL user is a reserved keyword that is used in an internal table, however I also have a separate user table in my own database that I need to use. Whenever I try to execute INSERT or UPDATE statements on the table, it generates the following error: The column name 'id' was not found in this ResultSet.
This is the Java code I am currently using:
PreparedStatement stat1 = conn.prepareStatement("SELECT id FROM user;");
PreparedStatement stat2 = conn.prepareStatement("UPDATE user SET date_created = ? , last_updated = ? , uuid = ? WHERE id = ?;");
ResultSet rs = stat1.executeQuery();
while(rs.next()){
UUID uuid = UUID.randomUUID();
String tempId = uuid.toString();
stat2.setTimestamp(1, curDate);
stat2.setTimestamp(2, curDate);
stat2.setString(3, tempId);
stat2.setLong(4,rs.getLong("id"));
stat2.executeUpdate();
}
So my question is, how could I insert or update the values in my personal user table without interfering with the keyword restriction?
Use this:
prepareStatement("UPDATE \"user\" set date_created = ?")
Or, better yet, rename your user table to something else, like users:
ALTER TABLE "user" RENAME TO users;
Escape the table name like this
select * from "user";

Java, MySQL updating table using ID (primary key)

I have a program in which working with MySql and Java JDBC.
My question is:
I have a table (TEMP) with ID as only 1 column and another table with user personal details like ID, name, age etc..
Im trying to retrieve the ID from TEMP table and update unfilled information like name, age, address etc in USER table.
This is the query I wrote:
update m_auth_info
set name = '"+name+"',
addr = '"+addr+"',
email = '"+email+"',
affiliation = '"+affil+"',
status = '"+1+"'
where a_id = '"+ResultSet+"'";
and when my getIdMethod retrieves ID into ResultSet from TEMP table. I couldn't able to update USER TABLE.
But it works if I give ID directly.. example.
update m_auth_info
set name = '"+name+"',
addr = '"+addr+"',
email = '"+email+"',
affiliation = '"+affil+"',
status = '"+1+"'
where a_id = '"+8989+"'";
Please tell me if I have to write which step to write in my getIdMethod, so that ill get the value into ResultSet.
int id=0;
String sql = "Select ID from Temp";
ResultSet rs = statement.executeQuery(sql);
while(rs.next(){
id = rs.getInt(1);
/* pass your update query here and use like this it should work for you
update m_auth_info set name = '"+name+"',
addr = '"+addr+"', email = '"+email+"',
affiliation = '"+affil+"', status = '"+1+"' where a_id = '"+id+"'";*/
}
I believe what you are trying to do is to get the result from ResultSet, which you can do with something rs.getLong(0), your ResultSet variable.getYourIdType(0) or columnName

Categories

Resources