I am using the embedded h2 database inside of the play framework. I am trying to insert some test data into it, then retrieve that data.
as you can see, the right side shows me that there is indeed a suscribers table, but when being run, I get this:
What am I doing wrong?
Try with dbName.tblName, for example:
insert into dbName.tblName ...
and also:
You are inserting 2 columns out of 3 columns per table. I think you have to mention column names as well.
Related
I am currently trying to push some data from Java into BigQuery, in order to be able to make use of it only for the next query and then to get rid of it.
The data consists of 3 columns which exists in the table I want to query. Therefore, by creating a temporary table containing this data, I could make a left join and get the query results I am in need of.
This process will happen on a scheduled basis, having different sets of data.
Can you please tell me if that can be done ?
Many thanks !
Using the jobs.query API, you can specify a destinationTable as part of configuration.query. Does that help? You can control the table expiration time using the tables.update API and setting expirationTime.
Alternatively, you can "inline" the table as part of the query that you want to run using a WITH clause in standard SQL rather than writing to a temporary table.
I have a jrxml file (sample shown below) which has the database query embedded in it. Now this query will return different columns against different databases. Since the columns are varying, I am planning to programmatically load the jrxml file, read the fields returned from the query (embedded in jrxml) and then place them on the jrxml
Have 2 questions
How do I get the field names returned from the query (embedded in jrxml)
How do we iterate through those fields so that they can be placed on the jrxml
Amy sample code would be appreciated.
Please note my preference is to use Jasper API's only.
How do you intend to query the database if you do not know the column names? The only case I can think of is that you are always going to select all the columns.
What I think you need is a parameterized query which will allow you to pass column names as parameters. See this page on using report parameters.
If you really want to always select all the table columns then before filling the report you will have to retrieve the table metadata and pass the column names to the report as parameters. If you are using JDBC then you simply need to call java.sql.Connection.getMetaData() and query the MetaData object for column names. However, hardcoding SELECT * is potentially dangerous because your result sets will keep growing in size as new columns get inserted into the table.
I have 2 DBs, Database A and Database B.
What I want to achieve:
build records from Database A and insert them to Database B
Process those records in my java app
What I'm currently doing:
I use two separate queries:
For (1) I use INSERT INTO ... SELECT ...
For (2) I perform another SELECT.
My solution works but it isn't optimal since I'm getting the records from Database A twice (instead of just one time).
Is there a way to execute the INSERT INTO ... SELECT ... and get the inner select result as a ResultSet?
I know I can perform only a SELECT and then insert the records in a batch, but thats a bit cumbersome and I want to find out if there's a cleaner solution.
Your cleaner solution look more cumbersome than simple read and write operation.
As you have to manipulate data in database B. You simply do this
Read Data from A to your app
Process data
Write data to B from your app
Then you have singe read single write and is simple.
You can not gain the result of INSERT INTO as Result set as this is INSERT statement
Sadly, I do not think that this is possible. What you are trying to achieve are two distinct operations i.e. an INSERT and a SELECT. However you cut it you are still going have to do at least one INSERT and one SELECT.
use this for two database
INSERT INTO Database2 (field1,field2,field3){
SELECT * FROM Database1;);
Both the database have the same field name.
I am developing android application. In my application i have used sql server2008 R2. I am inserting the row in the sql server on some click event. Now i want to fetch the first column's value from the newly inserted row. The first column's value of that schema is auto generated.I am inserting the value from the second column in my insert query. I am using jdbc. In .net to achieve this functionality method ExecuteScalar() is der. But what in java. I have done lots of googling but haven't found any thing. Help me if you the solution.
As per request to move comment to an answer.
You may find this question helpful.
How to get the insert ID in JDBC?
I'm using displaytag to build tables with data from my db. This works well if the requested list isn't that big but if the list size grows over 2500 entries, fetching the result list takes very long (more than 5 min.). I was wondering if this behavior is normal.
How you handle big list / queries which return big results?
This article links to an example app of how to go about solving the problem. Displaytag expects to be passed a full dataset to create paging links and handle sorting. This kind of breaks the idea of paging externally on the data and fetching only those rows that are asked for (as the user pages to them). The project linked in the article describes how to go about setting this type of thing up.
If you're working with a large database, you could also have a problem executing your query. I assume you have ruled this out. If not, you have the SQL as mentioned earlier - I would run it through the DB2 query analyzer to see if there are any DB bottlenecks. The next step up the chain is to run a test of the Hibernate/DAO call in a unit test without displaytag in the mix. Again, from how you've worded things, it sounds like you've already done this.
The Displaytag hauls and stores everything in the memory (the session). Hibernate also does that. You don't want to have the entire DB table contents at once in memory (however, if the slowdown already begins at 2500 rows, it more look like a matter of badly optimized SQL query / DB table; 2500 rows should be peanuts for a decent DB, but OK, that's another story).
Rather create a HTML table yourself with little help of JSTL c:forEach and a shot of EL. Keep one or two request parameters in the background in input type="hidden": the first row to be displayed (firstrow) and eventually the amount of rows to be displayed at once (rowcount).
Then, in your DAO class just do a SELECT stuff FROM data LIMIT firstrow OFFSET rowcount or something like that depending on the DB used. In MySQL and PostgreSQL you can use the LIMIT and/or OFFSET clause like that. In Oracle you'll need to fire a subquery. In MSSQL and DB2 you'll need to create a SP. You can do that with HQL.
Then, to page through the table, just have a bunch buttons which instructs the server side code to in/decrement the firstrow with rowcount everytime. Just do the math.
Edit: you commented that you're using DB2. I've done a bit research and it appears that you can use the UDB OLAP function ROW_NUMBER() for this:
SELECT id, colA, colB, colC
FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY id) AS row, id, colA, colB, colC
FROM
data
) AS temp_data
WHERE
row BETWEEN 1 AND 10;
This example should return the first 10 rows from the data table. You can parameterize this query so that you can reuse it for every page. This is more efficient than querying the entire table in Java's memory. Also ensure that the table is properly indexed.