Solved

Temporary table in java

Posted on 2014-04-16
7
2,980 Views
Last Modified: 2014-04-21
I am writing a java code to which retrieve values from a big table and stores in some other tables.
here is my question i want to create a temp table using java code the column entries for that temp table is from the big table i mentioned
also I want to add new column to this temp table...

how can i achieve this with out effecting the database - is it possible to create temp table using java
also can i perform sql queries on such temp table

if i can effect the database I will create a view but I don't want to create any new table or view just for this one program
so is there a way for me to create a temporary view or table which can be used with in the program and will never exist once control comes out of program.
0
Comment
Question by:shragi
7 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 40005643
This is not really the responsibility of Java. If you want to create a temporary table, that would be dependant on what type of database you are using. Having said that, most databases support temporary tables, so you should be able to do it. It's just that exactly how you do it will depend on the type of database. If you can tell us this information, I may be able to help further. Also can you tell us what you are currently using in Java to work with the database, ie. are you using JDBC, Spring, Hibernate, JPA, etc, etc?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40005958
>> i want to create a temp table using java code the column entries for that temp table is from the big table i mentioned
Why do you need that temp table?
>> also I want to add new column to this temp table...
Why? Since you also wrote:
>> temporary ... table which ... will never exist once control comes out of program

So you need part of the data of the big table + some extra inside the java program. That's all.
Why don't you query the data you need from the big table and keep that in memory (in a List or a Map)?
I just don't see it.
0
 

Author Comment

by:shragi
ID: 40007185
@zzynx the reason why I don't want to keep it in list or map or resultset is I want to query on that temp table which i want to create.... if i stored in any of the data structure you mentioned I cannot query on the results i mean i don't have flexibility to select the data i want....

@mccarl I am using sql server 2012 and using JDBC....
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

Author Comment

by:shragi
ID: 40007586
I found the below article which is close to what i want....but not exactly..
also I convinced my mgr to be able to create a temp table in DB for this program so my problem is fixed....all i do is store the data in the table during program and remove the data from table as process goes on....

http://www.javaworld.com/article/2075440/core-java/develop-a-generic-caching-service-to-improve-performance.html
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40008307
>> I convinced my mgr to be able to create a temp table in DB for this program
Given you  need to query, I think that's the most evident solution too.
>> so my problem is fixed
Good to hear.
0
 
LVL 27

Accepted Solution

by:
dpearson earned 500 total points
ID: 40010974
It sounds like you figured out a workaround, but you should at least be aware that "temp tables" are a fully supported database concept.

They exist just as long as the connection to the database exists.  When you break the connection, the table goes away.

You can read about the specifics of how SQL Server does this here:
http://www.codeproject.com/Articles/42553/Quick-Overview-Temporary-Tables-in-SQL-Server-2005

but the simple rule is you just add "#" to the front of the table name in your "CREATE TABLE" call (which you can happily send through the JDBC driver, just like any other statement - like a SELECT or UPDATE).

Here's a simple example, again for SQL Server:
http://www.sqlteam.com/article/temporary-tables

Hope that helps,

Doug
0
 

Author Comment

by:shragi
ID: 40012755
THanks Doug - that's helpful
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question