Tuesday, August 4, 2009

HIBERNATE INTERVIEW QUESTION & Practical Questions

HIBERNATE INTERVIEW QUESTION
Q1. Which statement is correct ?
1 session.contains() method to determine if an instance belongs to the session cache.
2 session.contains() method to determine if an instance belongs to the data base .
3 Both are correct
4 none of the above
You have choosen : 4Correct Answer is : 1You were wrong.

Q2. What does hibernate.show_sql true mean ?
1 show sql results on console.
2 show sql inputs on console.
3 show sql statement on console.
4 None
You have choosen : 3Correct Answer is : 3You were correct.
Explanations : null

Q3. What is the naming convention for Hibernate XML mapping file extensions?
1 .hbm.xml
2 .hhm.xml
3 .hb.xml
4 none of the above
You have choosen : 1Correct Answer is : 1You were correct.
Explanations : null

Q4. What does session.createCriteria().uniqueResult() return ?
1 Object
2 String
3 ResultSet
4 HibernateResultSet
You have choosen : 1Correct Answer is : 1You were correct.
Explanations : null

Q5. ____ forces hibernate to bypass the setter method and access the instance variable directly while initializing a newly loaded object.
1 By mapping the property with access="onlyfield" in Hibernate metadata
2 By mapping the property with access="variable" in Hibernate metadata
3 By mapping the property with access="field" in Hibernate metadata
4 none of the above
You have choosen : 1Correct Answer is : 3You were wrong.
Explanations : null

Q6. What does session.load() return if there is no matching database row ?
1 NULL
2 Empty Object
3 unrecoverable exception
4 None of the above
You have choosen : 1Correct Answer is : 3You were wrong.
Explanations : null

Q7. It is possible to re-load an object and all its collections at any time, using the xxxx() method. This is useful when database triggers are used to initialize some of the properties of the object. What is the xxxx() method ?
1 refresh();
2 flush();
3 fetch();
4 load()
You have choosen : 2Correct Answer is : 1You were wrong.
Explanations : null

Q8. What happens when both hibernate.properties and hibernate.cfg.xml are in the classpath?
1 The settings of the properties file will override the settings used in the XML configuration
2 The settings of the XML configuration file will override the settings used in the properties
3 DuplicateConfigurationException
4 None
You have choosen : 3Correct Answer is : 2You were wrong.
Explanations : null

Q9. What is the naming convention for Hibernate XML mapping file extensions?
1 .hibernate.xml
2 .hbm.xml
3 .hibernate_data.xml
4 none
You have choosen : 2Correct Answer is : 1You were wrong.
Explanations : null


Q10. What is dirty checking in Hibernate?
1 bject state changes in order to synchronize the updated state with the database
2 remove the dirty data from data base.
3 check the data when insert into data base.
4 None
You have choosen : 3Correct Answer is : 1You were wrong.
Explanations : null

Q11. If instances are not in the session or second-level cache which will give better performance?
1 Query.list()
2 Query.iterate()
3 Both are same
4 None of the above
You have choosen : 2Correct Answer is : 1You were wrong.
Explanations : null


Q12. Which fetching strategy tuned query automatically ?
1 session.createQuery();
2 session.createCriteria();
3 session.createSQLQuery();
4 None Of the above
You have choosen : 3Correct Answer is : 2You were wrong.
Explanations : null


Q13. Is Hibernate Session threadsafe?
1 threadsafe
2 not threadsafe
3 No relation to Thread
4 None of the above
You have choosen : 2Correct Answer is : 2You were correct.
Explanations : null

Q14. In the .bhm.xml file ? ] ]> What is correct way of executipon of the above query in the .bhm.xml file.
1 Query q = sess.getNamedQuery("abc");
2 Query q = sess.createQuery("abc");
3 Query q = sess.createSQLQuery("abc");
4 none of the above
You have choosen : 1Correct Answer is : 1You were correct.
Explanations : null

Q15. What is Configuration interface in hibernate?
1 used to create SessionFactory.
2 used to create Session.
3 used to create Transaction.
4 none of the above
You have choosen : 3Correct Answer is : 1You were wrong.
Explanations : null


Q16. By default, Hibernate uses _ for JVM-level caching.
1 OSCache
2 EHCache
3 JBOSSCache
4 None of the above
You have choosen : 1Correct Answer is : 2You were wrong.

Q17. Which Cache Strategy particularly useful in cases where underlying data may have been updated via a separate process (i.e., not modified through Hibernate)?
1 Query.setCacheMode(CacheMode.READ).
2 Query.setCacheMode(CacheMode.REFRESH).
3 Query.setCacheMode(CacheMode.NONE).
4 Query.setCacheMode(CacheMode.REFRESHDATABASE).
You have choosen : 1Correct Answer is : 2You were wrong.
Explanations : null


Q18. What does session.evict() method do ?
1 remove the object and its collections from the first level cache
2 remove the object and its collections from the second level cache
3 remove the object and its collections from the data base
4 None of the above
You have choosen : 2Correct Answer is : 1You were wrong.
Explanations : null

Q19. Is the below code work ? Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); session.save(customer); } tx.commit(); session.close();
1 true
2 false
3 can't say
4 none
You have choosen : 1Correct Answer is : 2You were wrong.
Explanations : null


Q20. SessionFactory represent which level of cache ?
1 2nd
2 Ist
3 3rd
4 4th
You have choosen : 1Correct Answer is : 1You were correct.
Explanations : null

Q21. How can get a session object ?
1 SessionFactory.getSession();
2 SessionFactory.openSession();
3 SessionFactory.get();
4 (session)SessionFactory.getObject();
You have choosen : 1Correct Answer is : 2You were wrong.

Q22. To completely evict all objects from the session cache which method is appropriate ?
1 Session.evict()
2 Session.clear()
3 Session.flush()
4 None of the above
You have choosen : 1Correct Answer is : 2You were wrong.
Explanations : null

Hibernate Mock Online test
Q23. What Session.flush() do ?
1 the state of that object will be synchronized with the database.
2 the state of that object will be synchronized with the SessionFactory.
3 remove state of that object from Session.
4 remove state of that object from SessionFactory.
You have choosen : 3Correct Answer is : 1You were wrong.
Explanations : null

Q24. How to create SessionFactory ?
1 configuration.buildSessionFactory();
2 configuration.createSessionFactory();
3 configuration.createNewSessionFactory();
4 none of the above
You have choosen : 3Correct Answer is : 1You were wrong.
Explanations : null

Q25. Difference between Emp e = session.load(Emp.class) and Emp e = session.createCriteria(Emp.class) ?
1 On session close session.load() Emp Object unavilabe to access.
2 On session close session.createCriteria() Emp unavilabe to access.
3 None
4 None
You have choosen : 1Correct Answer is : 1You were correct.
Explanations : null

Q26. Which statement is correct ?
1 A Session will not obtain a JDBC Connection (or a Datasource) unless it is needed
2 A Session will obtain a JDBC Connection (or a Datasource) on object create
3 A Session has no relation with JDBC Connection (or a Datasource)
4 none of the above
You have choosen : 1Correct Answer is : 1You were correct.

Q27. what is the default value in hibernate ?
1 lazy=false;
2 lazy=true;
3 lazy=yes;
4 lazy=no;
You have choosen : 1Correct Answer is : 2You were wrong.
Explanations : null

Q28. Which statement is correct
1 The CacheMode controls how a particular session interacts with the second-level cache.
2 The CacheMode don't controls session interaction with the second-level cache.
3 both
4 none
You have choosen : 1Correct Answer is : 1You were correct.
Explanations : null


Q29. Which statement is true about session.saveOrUpdate()?
1 if another object associated with the session has the same identifier, throw an exception
2 if the object has no identifier property, save() it
3 Both of the above
4 none of the above
You have choosen : 2Correct Answer is : 1You were wrong.
Explanations : null

Q30. Which statement is correct ?
1 Specifying join as the fetch strategy in the mapping document does affect HQL queries.
2 Specifying join as the fetch strategy in the mapping document does not affect HQL queries.
3 None
4 None
You have choosen : 1Correct Answer is : 2You were wrong.

Friday, July 31, 2009

Free project management Software Links

VIEW PATH

XML & XSL Advance Example

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes"/>

<xsl:template match="/">
<xsl:apply-templates select="GeneralReport/Carlines/Carline/Prices"/>
<xsl:apply-templates select="GeneralReport/Prices"/>
</xsl:template>

<xsl:template match="Prices">
<xsl:apply-templates select="LocalReport"/>
<xsl:apply-templates select="DomesticOptionPrice"/>
<xsl:apply-templates select="ExportModelPrice"/>
<xsl:apply-templates select="ExportOptionPrice"/>
</xsl:template>

<xsl:template match="DomesticOptionPrice">
<xsl:for-each select="Models/Model">
<xsl:apply-templates select="Code"/>
<xsl:apply-templates select="../../Code"/>
<xsl:apply-templates select="../../ORS"/>
<xsl:apply-templates select="Name"/>
<xsl:apply-templates select="../../Name"/>
<xsl:apply-templates select="../../MSRP"/>
<xsl:apply-templates select="../../MSRPNet"/>
<xsl:apply-templates select="../../DealerNet"/>
<xsl:apply-templates select="../../Taxes"/>
<xsl:apply-templates select="../../EffectiveDate"/>
<xsl:apply-templates select="../../MY"/>
<xsl:apply-templates select="../../MYS"/>
<xsl:apply-templates select="../../Carline"/>
<xsl:apply-templates select="../../DealerIndex"/>
<xsl:apply-templates select="../../Currency"/>
<xsl:if test="not(position()=last())">
<xsl:call-template name="newline"/>
</xsl:if>
</xsl:for-each>
<xsl:if test="not(position()=last())">
<xsl:call-template name="newline"/>
</xsl:if>
</xsl:template>

<xsl:template match="ExportOptionPrice">
<xsl:for-each select="Models/Model">
<xsl:apply-templates select="Code"/>
<xsl:apply-templates select="../../Code"/>
<xsl:apply-templates select="../../ORS"/>
<xsl:apply-templates select="Name"/>
<xsl:apply-templates select="../../Name"/>
<xsl:apply-templates select="../../PlantPrice"/>
<xsl:text>+000000000</xsl:text>
<xsl:text>+000000000</xsl:text>
<xsl:apply-templates select="../../Taxes"/>
<xsl:apply-templates select="../../EffectiveDate"/>
<xsl:apply-templates select="../../MY"/>
<xsl:apply-templates select="../../MYS"/>
<xsl:apply-templates select="../../Carline"/>
<xsl:apply-templates select="../../DealerIndex"/>
<xsl:apply-templates select="../../Currency"/>
<xsl:if test="not(position()=last())">
<xsl:call-template name="newline"/>
</xsl:if>
</xsl:for-each>
<xsl:if test="not(position()=last())">
<xsl:call-template name="newline"/>
</xsl:if>
</xsl:template>


<xsl:template match="LocalReport">
<xsl:apply-templates select="Models/Model/Code"/>
<!-- 10 blanks for Option Code -->
<xsl:text> </xsl:text>
<xsl:apply-templates select="ORS"/>
<xsl:apply-templates select="Models/Model/Name"/>
<!-- 40 blanks for Option Name -->
<xsl:text> </xsl:text>
<xsl:apply-templates select="MSRP"/>
<xsl:apply-templates select="MSRPNet"/>
<xsl:apply-templates select="DealerNet"/>
<xsl:apply-templates select="Taxes"/>
<xsl:apply-templates select="EffectiveDate"/>
<xsl:apply-templates select="MY"/>
<xsl:apply-templates select="MYS"/>
<xsl:apply-templates select="Carline"/>
<xsl:apply-templates select="DealerIndex"/>
<xsl:apply-templates select="Currency"/>
<xsl:call-template name="newline"/>
</xsl:template>

<xsl:template match="ExportModelPrice">
<xsl:apply-templates select="Models/Model/Code"/>
<!-- 10 blanks for Option Code -->
<xsl:text> </xsl:text>
<xsl:apply-templates select="ORS"/>
<xsl:apply-templates select="Models/Model/Name"/>
<!-- 40 blanks for Option Name -->
<xsl:text> </xsl:text>
<xsl:apply-templates select="PlantPrice"/>
<xsl:text>+000000000</xsl:text>
<xsl:text>+000000000</xsl:text>
<xsl:apply-templates select="Taxes"/>
<xsl:apply-templates select="EffectiveDate"/>
<xsl:apply-templates select="MY"/>
<xsl:apply-templates select="MYS"/>
<xsl:apply-templates select="Carline"/>
<xsl:apply-templates select="DealerIndex"/>
<xsl:apply-templates select="Currency"/>
<xsl:call-template name="newline"/>
</xsl:template>



<xsl:template name="fillBefore">
<xsl:param name="objectString"/>
<xsl:param name="numberOfChars"/>
<xsl:param name="fillCharacter"/>
<xsl:variable name="size" select="string-length($objectString)"/>
<xsl:choose>
<xsl:when test="$size < $numberOfChars">
<xsl:call-template name="fillBefore">
<xsl:with-param name="objectString"><xsl:value-of select="concat($fillCharacter, $objectString)"/></xsl:with-param>
<xsl:with-param name="numberOfChars"><xsl:value-of select="$numberOfChars"/></xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:value-of select="$fillCharacter"/></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="$size > $numberOfChars">
<xsl:value-of select="substring($objectString,1,$numberOfChars)"/>
<!-- todo : better solution needed here -->
<!-- <xsl:message terminate="yes">ObjectString is too long</xsl:message> -->
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$objectString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="fillAfter">
<xsl:param name="objectString"/>
<xsl:param name="numberOfChars"/>
<xsl:param name="fillCharacter"/>
<xsl:variable name="size" select="string-length($objectString)"/>
<xsl:choose>
<xsl:when test="$size < $numberOfChars">
<xsl:call-template name="fillAfter">
<xsl:with-param name="objectString"><xsl:value-of select="concat($objectString,$fillCharacter)"/></xsl:with-param>
<xsl:with-param name="numberOfChars"><xsl:value-of select="$numberOfChars"/></xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:value-of select="$fillCharacter"/></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="$size > $numberOfChars">
<xsl:value-of select="substring($objectString,1,$numberOfChars)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$objectString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>


<!-- leave this as it was made, it is essential for the newline -->
<xsl:template name="newline">
<xsl:text>
</xsl:text>
</xsl:template>



<xsl:template match="Code">
<xsl:call-template name="fillAfter">
<xsl:with-param name="objectString"><xsl:value-of select="."/></xsl:with-param>
<xsl:with-param name="numberOfChars">10</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text> </xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:template>

<xsl:template match="Model">
<xsl:call-template name="fillAfter">
<xsl:with-param name="objectString"><xsl:value-of select="."/></xsl:with-param>
<xsl:with-param name="numberOfChars">10</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text> </xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:template>

<xsl:template match="MSRP | MSRPNet | DealerNet | PlantPrice">
<xsl:choose>
<xsl:when test="@sign">
<xsl:variable name="price" select="."/>
<xsl:call-template name="getPrice">
<xsl:with-param name="value" select="substring-after($price,'-')"/>
</xsl:call-template>
<xsl:value-of select="@sign"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="getPrice">
<xsl:with-param name="value" select="."/>
</xsl:call-template>
<xsl:text>+</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="getPrice">
<xsl:param name="value"/>
<xsl:choose>
<xsl:when test="not(contains($value,'.'))">
<xsl:text>000000000</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="before">
<xsl:call-template name="fillBefore">
<xsl:with-param name="objectString"><xsl:value-of select="substring-before($value,'.')"/></xsl:with-param>
<xsl:with-param name="numberOfChars">7</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text>0</xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="after" select="substring-after($value,'.')"/>
<xsl:value-of select="concat($before, $after)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="getTax">
<xsl:param name="value"/>
<xsl:choose>
<xsl:when test="not(contains($value,'.'))">
<xsl:text>000000</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="before">
<xsl:call-template name="fillBefore">
<xsl:with-param name="objectString"><xsl:value-of select="substring-before($value,'.')"/></xsl:with-param>
<xsl:with-param name="numberOfChars">3</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text>0</xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<!-- BugFix #287 - eVPS-2006-03: format the Tax values to 3 digital places before the point and 3 after -->
<xsl:variable name="after_long">
<xsl:value-of select="substring-after($value,'.')"/>
</xsl:variable>
<xsl:variable name="after">
<xsl:value-of select="substring($after_long,1,3)"/>
</xsl:variable>
<xsl:value-of select="concat($before, $after)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template name="getSpecialTaxAmount">
<xsl:param name="value"/>
<xsl:choose>
<xsl:when test="not(contains($value,'.'))">
<xsl:text>+000000000</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="minsign">
<xsl:if test="(contains($value,'-'))">
<xsl:text>-</xsl:text>
</xsl:if>
<xsl:if test="not(contains($value,'-'))">
<xsl:text>+</xsl:text>
</xsl:if>
</xsl:variable>
<xsl:variable name="removeminus">
<xsl:if test="(contains($value,'-'))">
<xsl:value-of select="substring-after($value,'-')"/>
</xsl:if>
<xsl:if test="not(contains($value,'-'))">
<xsl:value-of select="string($value)"/>
</xsl:if>
</xsl:variable>
<xsl:variable name="before">
<xsl:call-template name="fillBefore">
<xsl:with-param name="objectString"><xsl:value-of select="substring-before($removeminus,'.')"/></xsl:with-param>
<xsl:with-param name="numberOfChars">7</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text>0</xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:variable>

<xsl:variable name="after_long">
<xsl:value-of select="substring-after($before,'.')"/>
</xsl:variable>
<xsl:variable name="after">
<xsl:value-of select="substring($before,1,2)"/>
</xsl:variable>
<xsl:value-of select="concat($minsign,$before, $after)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="EffectiveDate">
<xsl:variable name="date" select="."/>
<xsl:value-of select="substring($date, 1, 2)"/>
<xsl:value-of select="substring($date, 4, 2)"/>
<xsl:value-of select="substring($date, 7, 4)"/>
</xsl:template>


<xsl:template match="ORS">
<xsl:call-template name="fillAfter">
<xsl:with-param name="objectString"><xsl:value-of select="."/></xsl:with-param>
<xsl:with-param name="numberOfChars">25</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text> </xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:template>

<!--
<xsl:template match="Name">

</xsl:template>
-->

<xsl:template match="Name">
<xsl:call-template name="fillAfter">
<xsl:with-param name="objectString"><xsl:value-of select="."/></xsl:with-param>
<xsl:with-param name="numberOfChars">40</xsl:with-param>
<xsl:with-param name="fillCharacter"><xsl:text> </xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:template>

<xsl:template match="Taxes">
<xsl:call-template name="getTax">
<xsl:with-param name="value" select="./SpecialTax"/>
</xsl:call-template>
<xsl:call-template name="getTax">
<xsl:with-param name="value" select="./Tax"/>
</xsl:call-template>

<xsl:call-template name="getSpecialTaxAmount">
<xsl:with-param name="value" select="./SpecialTaxAmount"/>
</xsl:call-template>


</xsl:template>

<xsl:template match="Carline">
<xsl:value-of select="substring(.,1,2)"/>
</xsl:template>

</xsl:stylesheet>




<!-- Stylus Studio meta-information - (c)1998-2001 eXcelon Corp.
<metaInformation>
<scenarios ><scenario default="yes" name="toExport" userelativepaths="yes" url="..\results\Kopie von sample.xml" htmlbaseurl="" processortype="internal" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext=""/></scenarios><MapperInfo srcSchemaPath="" srcSchemaRoot="" srcSchemaPathIsRelative="yes" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" />
</metaInformation>
-->


============================
XML Data
============================
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="export.xsl"?>
<GeneralReport module="Domestic">
<Meta>
<User>TZG136</User>
<CreationDate>31.07.2009</CreationDate>
<CreationTime>15:16</CreationTime>
<Comment></Comment>
<SelectionCriteria>
<LegalEntityName>Austria</LegalEntityName>
<Carline>Agila</Carline>
<MY>2</MY>
<MYS>A</MYS>
<DealerIndex>000</DealerIndex>
<Currency>EUR</Currency>
<EffectiveDate>= 14.12.2001</EffectiveDate>
<ModelPrices>true</ModelPrices>
<Model>0HF68</Model>
<Option>*</Option>
<OptionIndicator>M</OptionIndicator>
<Active>true</Active>
<ORS apply="false">*</ORS>
</SelectionCriteria>
</Meta>
<AllModels>
<Model>0HF68</Model>
<Model>0HL68</Model>
<Model>0HN68</Model>
<Model>0HP68</Model>
</AllModels>
<Models>
<Model>0HF68</Model>
</Models>
<Prices>
<LocalReport>
<LegalEntityName>Austria</LegalEntityName>
<OptionIndicator>M</OptionIndicator>
<Carline>Agila</Carline>
<Code></Code>
<Name></Name>
<Models>
<Model>
<Code>0HF68</Code>
<Name>Agila Agila 5</Name>
</Model>
</Models>
<MY>2</MY>
<MYS>A</MYS>
<EffectiveDate>14.12.2001</EffectiveDate>
<DealerIndex>000</DealerIndex>
<MSRP sign="-">-90.00</MSRP>
<MSRPNet sign="-">-182.73</MSRPNet>
<DealerNet sign="-">-182.73</DealerNet>
<TransferPrice sign="-">-183.00</TransferPrice>
<Currency>EUR</Currency>
<ORS></ORS>
<Taxes>
<Tax>0.0000</Tax>
<SpecialTax>10.0000</SpecialTax>
<SpecialTaxAmount>500.00</SpecialTaxAmount>
</Taxes>
</LocalReport>
</Prices>
</GeneralReport>

Wednesday, July 29, 2009

PHP MYSQL APACHE Installation

Hi

Please Use the below link Which gives you the details information about PHP MYSQL APACHE Installation


Click Here Download Link - PHP MYSQL APACHE Installation instruction

After you install PHP - You can install HEIDSQL which is MYSQL front END.
Click Here Download Link : HEIDSQL - MYSQL Front End

Wednesday, July 15, 2009

Needs Answers :

1.How to retrieve values from Oracle's ref cursor using java Program.?

Thursday, July 9, 2009

Oracle Types access through Java Object

package com.oracleObjects.AcessOracleObjects;


import java.io.IOException;

import java.sql.*;
import java.util.List;
import java.util.Properties;

import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;
/**
* @author tzg136
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class AcessOracleObjectsUsingJava_EMP {
Connection conn = null;
String driverName ="";
String serverName = "";
String username ="";
String password = "";

String portNumber ="";
String sid = "";
String databaseName = "";

public AcessOracleObjectsUsingJava_EMP()
{
Properties configFile = new Properties();
try {
configFile.load(AcessOracleObjectsUsingJava_EMP.class.getClassLoader().getResourceAsStream("config.properties"));
driverName =configFile.getProperty("driverName");
serverName = configFile.getProperty("serverName");
username =configFile.getProperty("username");
password = configFile.getProperty("password");
portNumber =configFile.getProperty("portNumber");
sid = configFile.getProperty("sid");
databaseName = configFile.getProperty("databaseName");

} catch (IOException e) {
e.printStackTrace();
}


}
public Connection getConnection()
{
try
{
Class.forName(driverName);
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
conn = DriverManager.getConnection(url, username, password);
System.out.println("Connected Sucessfully");

}
catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException "+e.getMessage());

} catch (SQLException e) {
System.out.println("SQl Syntaxt Error "+e.getMessage());

}
catch (Exception e) {
System.out.println("Exception "+e.getMessage());

}
return conn;
}
public void insertTableObjectValue(Emp objEmp)
{
try {
Object objArray[] = new Object[4];
objArray[0] = objEmp.getEmp_id();
objArray[1] = objEmp.getEmp_Name();
objArray[2] = objEmp.getAge();
objArray[3] = new Integer(objEmp.getSalary());

StructDescriptor colStructDesc = StructDescriptor.createDescriptor("OBJ_TYPE_EMP", conn);

oracle.sql.STRUCT colStruct = new STRUCT(colStructDesc,conn, objArray);

PreparedStatement prepa_stmt= conn.prepareStatement( "INSERT INTO TABLE_EMP_OBJ VALUES (?)");
prepa_stmt.setObject(1,colStruct); // Bind the Java Object to the above Statement ps.executeUpdate();
prepa_stmt.executeUpdate();


} catch (SQLException e) {
System.out.println("SQLException"+ e.getMessage());
}
catch (Exception e) {
System.out.println("Exception"+ e.getMessage());
}
}



public void createTableWithObject()
{
/*
*
* CREATE TYPE OBJ_TYPE_EMP as OBJECT (
EMP_ID VARCHAR2(40),
EMP_NAME VARCHAR2(40),
AGE VARCHAR2(10) ,
SALARY NUMBER )

CREATE TABLE TABLE_EMP_OBJ (ObjEmp OBJ_TYPE_EMP )
*/
}

public void selectTableObjectValue()
{
try {



Statement stmt = conn.createStatement();

// Select rows from object1_table
ResultSet resultSet = stmt.executeQuery("SELECT * FROM TABLE_EMP_OBJ");

// OracleResultSet resultSet = (OracleResultSet)pst.executeQuery();

java.util.Hashtable mymap = new java.util.Hashtable();
Class obj = Class.forName("com.oracleObjects.AcessOracleObjects.Emp");
// Map the loaded java class to the object-type
mymap.put ("OBJ_TYPE_EMP", obj);

// Get the OBJECT values from each row
while (resultSet.next())
{
Emp objEmp;
objEmp = (Emp)resultSet.getObject(1, mymap);
System.out.println(""+objEmp.getEmp_Name());
System.out.println(""+objEmp.getAge());
}


}
catch (SQLException e) {
System.out.println("SQLException"+ e.getMessage());
e.printStackTrace();
}
catch (Exception e) {
System.out.println("Exception"+ e.getMessage());
}
}
public void closeConnection()
{
try
{
conn.close();
}
catch (Exception e) {
System.out.println("Exception"+ e.getMessage());
}

}



public static void main(String[] args) {
AcessOracleObjectsUsingJava_EMP objAccessJavaObject= new AcessOracleObjectsUsingJava_EMP();
Emp objEmp= new Emp();
objEmp.setEmp_id("1000");
objEmp.setEmp_Name("Andrew");
objEmp.setAge("34");
objEmp.setSalary(500);


Connection conn= objAccessJavaObject.getConnection();
if(args[0].equalsIgnoreCase("I"))
{
objAccessJavaObject.insertTableObjectValue(objEmp);
}
if(args[0].equalsIgnoreCase("S"))
{
objAccessJavaObject.selectTableObjectValue();
}

objAccessJavaObject.closeConnection();

}
}

Tuesday, July 7, 2009

Oracle Object Types - Java Implementation

Please find the code which has the implementation of inserting values in Oracle Objects and table.

CREATE TYPE product_detail_info AS object (product_detail_id VARCHAR(30), product_name VARCHAR(30),product_rate NUMBER)
CREATE TYPE product_type AS object (product_type_id VARCHAR(30), product_type_Name VARCHAR(30),obj_product_detail_info product_detail_info )
CREATE TABLE Electronics_Products(Electronics_Products_id VARCHAR(30), Electronics_Products_name VARCHAR(30),obj_product_type product_type)

Note :
While running this program Please give args[0] value as below
I- I - Insert
II- C - create Object and Table
III- S - Select

Java Code :


package com.oracleObjects.AcessOracleObjects;


import java.io.IOException;
import java.math.BigDecimal;
import java.sql.*;
import java.util.Properties;
/**
* @author tzg136
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class AcessOracleObjects_ElectronicsItem {
Connection conn = null;
String driverName ="";
String serverName = "";
String username ="";
String password = "";

String portNumber ="";
String sid = "";
String databaseName = "";

public AcessOracleObjects_ElectronicsItem()
{
Properties configFile = new Properties();
try {
configFile.load(AcessOracleObjects_ElectronicsItem.class.getClassLoader().getResourceAsStream("config.properties"));
driverName =configFile.getProperty("driverName");
serverName = configFile.getProperty("serverName");
username =configFile.getProperty("username");
password = configFile.getProperty("password");
portNumber =configFile.getProperty("portNumber");
sid = configFile.getProperty("sid");
databaseName = configFile.getProperty("databaseName");

} catch (IOException e) {
e.printStackTrace();
}


}
public Connection getConnection()
{
try
{
Class.forName(driverName);
String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
conn = DriverManager.getConnection(url, username, password);
System.out.println("Connected Sucessfully");

}
catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException "+e.getMessage());
} catch (SQLException e) {
System.out.println("SQl Syntaxt Error "+e.getMessage());
}
catch (Exception e) {
System.out.println("Exception "+e.getMessage());
}
return conn;
}
public void insertTableObjectValue()
{
try {
// Create an oracle.sql.ARRAY object to hold the values
Statement stmt = conn.createStatement();
//CREATE TYPE product_detail_info AS object (product_detail_id VARCHAR(30), product_name VARCHAR(30),product_rate NUMBER)
//product_detail_info('TV_FLAT21_001' , '21 Inch Flat TV ' ,2000 )
String product_detail_info_sql = "product_detail_info('D.Info TV_FLAT21_001' , '21 Inch Flat TV ' ,2000 )";

// CREATE TYPE product_type AS object (product_type_id VARCHAR(30), product_type_Name VARCHAR(30),obj_product_detail_info product_detail_info )
// product_type (product_type_id , product_type_Name,product_detail_info_sql )
String product_type_sql= "product_type( 'TV', 'This contains TV',("+product_detail_info_sql+"))";

//CREATE TABLE Electronics_Products(Electronics_Products_id VARCHAR(30), Electronics_Products_name VARCHAR(30),obj_product_type product_type)
//Electronics_Products(Electronics_Products_id, Electronics_Products_name ,product_type_sql)
String Electronics_Products_sql = "INSERT INTO Electronics_Products VALUES('ELE_item','this Elec items',"+product_type_sql+")";

// Insert a row with values for both the object1 and object2 types
System.out.println("SQL is = \n"+Electronics_Products_sql);
int row = stmt.executeUpdate(Electronics_Products_sql);
System.out.println("Row Count"+row);
//stmt.execute("INSERT INTO object1_table VALUES(1, object1('str1', object2('obj2str1', 123)))");

} catch (SQLException e) {
System.out.println("SQLException"+ e.getMessage());
}
catch (Exception e) {
System.out.println("Exception"+ e.getMessage());
}
}
public void createTableWithObject()
{
try {
// Create an oracle.sql.ARRAY object to hold the values
// Create a statement
Statement stmt = conn.createStatement();
String prod_de_in= "CREATE TYPE product_detail_info AS object (product_detail_id VARCHAR(30), product_name VARCHAR(30),product_rate NUMBER)";
// Create the object2 type
stmt.execute(prod_de_in);
String prod_type= "CREATE TYPE product_type AS object (product_type_id VARCHAR(30), product_type_Name VARCHAR(30),obj_product_detail_info product_detail_info )";
stmt.execute(prod_type);
String elect_Prod= "CREATE TABLE Electronics_Products(Electronics_Products_id VARCHAR(30), Electronics_Products_name VARCHAR(30),obj_product_type product_type)";
stmt.execute(elect_Prod);
// Create a table with a column to hold a number and the new object1 type
} catch (SQLException e) {
System.out.println("SQLException"+ e.getMessage());
}
catch (Exception e) {
System.out.println("Exception"+ e.getMessage());
}
}

public void selectTableObjectValue()
{
try {



Statement stmt = conn.createStatement();

// Select rows from object1_table
ResultSet resultSet = stmt.executeQuery("SELECT * FROM Electronics_Products");

// Get the OBJECT values from each row
while (resultSet.next()) {
// Get the integer from the first column col_integer of the row
String ele_prod_id = resultSet.getString("ELECTRONICS_PRODUCTS_ID");
String ele_prod_name = resultSet.getString("ELECTRONICS_PRODUCTS_NAME");

// Get the object1 value from the second column col_object1
oracle.sql.STRUCT obj_PRODUCT_TYPE = (oracle.sql.STRUCT)resultSet.getObject("OBJ_PRODUCT_TYPE");

// Get the object1 values from each row
Object[] obj_PRODUCT_TYPEValues = obj_PRODUCT_TYPE.getAttributes();

System.out.println("obj_PRODUCT_TYPEValues"+obj_PRODUCT_TYPEValues.toString());

int obj_PRODUCT_length = obj_PRODUCT_TYPEValues.length;

for(int i=0;i<obj_PRODUCT_length;i++)
{
System.out.println(i+" Product Type "+obj_PRODUCT_TYPEValues[0]);
System.out.println(i+"Product Type"+obj_PRODUCT_TYPEValues[1]);

// Product Detail Info
oracle.sql.STRUCT Obj_product_detail_info = (oracle.sql.STRUCT)obj_PRODUCT_TYPEValues[2];
Object[] obj_product_detail_info_Values = Obj_product_detail_info.getAttributes();
int obj_product_detail_info_length = obj_product_detail_info_Values.length;
for(int j=0;j<obj_product_detail_info_length;j++)
{
System.out.println(j+" Product Detail Info "+obj_product_detail_info_Values[0]);
System.out.println(j+" Product Detail Info "+obj_product_detail_info_Values[1]);
System.out.println(j+" Product Detail Info "+obj_product_detail_info_Values[2]);

}
}

// Get the first value of object1, which is a string
/* String str = (String)object1Values[0];

System.out.println(""+str);

// Get the second value of object1, which is of the type object2
oracle.sql.STRUCT object2 = (oracle.sql.STRUCT)object1Values[1];

// Get the values of object2
Object object2Values[] = object2.getAttributes();
str = (String)object2Values[0];
BigDecimal num = (BigDecimal)object2Values[1];
System.out.println(""+str);*/
}


}
catch (SQLException e) {
System.out.println("SQLException"+ e.getMessage());
}
catch (Exception e) {
System.out.println("Exception"+ e.getMessage());
}
}
public void closeConnection()
{
try
{
conn.close();
}
catch (Exception e) {
System.out.println("Exception"+ e.getMessage());
}

}



public static void main(String[] args) {
AcessOracleObjects_ElectronicsItem v= new AcessOracleObjects_ElectronicsItem();
Connection conn= v.getConnection();
if(args[0].equalsIgnoreCase("C"))
{
v.createTableWithObject();
}
else if(args[0].equalsIgnoreCase("I"))
{
v.insertTableObjectValue();
}
else if(args[0].equalsIgnoreCase("S"))
{
v.selectTableObjectValue();
}
else
{
System.out.println("No vlaida option is selected");
}
v.closeConnection();
}
}


Database Configuration :
driverName=oracle.jdbc.driver.OracleDriver
serverName=139.73.41.135
portNumber=1521
username=scott
password=tiger
databaseName=facisdev
sid=facisdev