Solved

Temporary table in java

Posted on 2014-04-16
7
3,421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 36

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 28

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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.
Six Sigma Control Plans

717 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