String jpql = "from A where name in (:names)";
Query q = em.createQuery(jpql);
q.setParameter("names", l);
String hql = "from A where name in (:names)";
Query q = s.createQuery(hql);
q.setParameterList("names", l);
My Knowledges
String jpql = "from A where name in (:names)";
Query q = em.createQuery(jpql);
q.setParameter("names", l);
String hql = "from A where name in (:names)";
Query q = s.createQuery(hql);
q.setParameterList("names", l);
@FixMethodOrder
annotation. Instead of using custom solutions just upgrade your junit version and annotate test class with FixMethodOrder(MethodSorters.NAME_ASCENDING)
. Check the release notes for the details.import org.junit.runners.MethodSorters;
import org.junit.FixMethodOrder;
import org.junit.Test;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SampleTest {
@Test
public void firstTest() {
System.out.println("first");
}
@Test
public void secondTest() {
System.out.println("second");
}
}
System.setProperty("axis.socketSecureFactory", "org.apache.axis.components.net.SunFakeTrustSocketFactory");
InetSocketAddress socketAddr = new InetSocketAddress(url,port);
Socket socket = new Socket();
socket.connect(socketAddr,readTimeout);
socket.setSoTimeout(idealTimeout);
// Open a socket without any parameters. It hasn’t been binded or connected
Socket sock = new Socket();
// Bind to a local ephemeral port
sock.bind(null);
// Connect to google.com on port 80 with a timeout of 500 milliseconds
sock.connect(new InetSocketAddress(”www.google.com”, 80), 500);
// Your code goes here
// Close the socket.
sock.close();
Query query = session.createSQLQuery(
"CALL GetStocks(:stockCode)")
.addEntity(Stock.class)
.setParameter("stockCode", "7277");
List result = query.list();
for(int i=0; iStock stock = (Stock)result.get(i);
System.out.println(stock.getStockCode());
}
2. NamedNativeQuery in annotation
Declare your store procedure inside the @NamedNativeQueries annotation.
//Stock.java
...
@NamedNativeQueries({
@NamedNativeQuery(
name = "callStockStoreProcedure",
query = "CALL GetStocks(:stockCode)",
resultClass = Stock.class
)
})
@Entity
@Table(name = "stock")
public class Stock implements java.io.Serializable {
...
Call it with getNamedQuery().
Query query = session.getNamedQuery("callStockStoreProcedure")
.setParameter("stockCode", "7277");
List result = query.list();
for(int i=0; iStock stock = (Stock)result.get(i);
System.out.println(stock.getStockCode());
}
3. sql-query in XML mapping file
Declare your store procedure inside the “sql-query” tag.
<!-- Stock.hbm.xml -->
...
<hibernate-mapping>
<class name="com.mkyong.common.Stock" table="stock" ...>
<id name="stockId" type="java.lang.Integer">
<column name="STOCK_ID" />
<generator class="identity" />
</id>
<property name="stockCode" type="string">
<column name="STOCK_CODE" length="10" not-null="true" unique="true" />
</property>
...
</class>
<sql-query name="callStockStoreProcedure">
<return alias="stock" class="com.mkyong.common.Stock"/>
<![CDATA[CALL GetStocks(:stockCode)]]>
</sql-query>
</hibernate-mapping>
Call it with getNamedQuery().
Query query = session.getNamedQuery("callStockStoreProcedure")
.setParameter("stockCode", "7277");
List result = query.list();
for(int i=0; iStock stock = (Stock)result.get(i);
System.out.println(stock.getStockCode());
}
Conclusion
The above three approaches are doing the same thing, call a store procedure in database. There are not much big different between the three approaches, which method you choose is depend on your personal prefer.
Ref : link1
CREATE FUNCTION agenda RETURN SYS_REFCURSOR
AS
my_cursor SYS_REFCURSOR;
BEGIN
OPEN my_cursor FOR
SELECT id,person_name,person_surname,person_address
FROM Person;
RETURN my_cursor;
END;
<sql-query name="SP_agenda" callable="true">
<return alias="ev" class="Person">
<return-property name="id" column="id"/>
<return-property name="name" column="person_name"/>
<return-property name="surname" column="person_surname"/>
<return-property name="address" column="person_address"/>
</return>
{ ? = call agenda() }
</sql-query>
certificate expired ซะงั้นดูจากปีพ.ศ.แล้ว (2497) น่าจะเกิดตอนลง SDK แล้วเครื่องเราตั้งระบบวันที่ของไทย เลยได้ certificate expired มาผิด
วิธีแก้ตามที่หลายเว็บแนะนำคือให้ไปลบไฟล์ “debug.keystore” ถ้าเป็น vista หรือ 7 อยู่ที่ C:\Users\ชื่อเครื่อง\.android ถ้า xp ก็อยู่ที่ C:\Documents and Settings\ชื่อเครื่อง\.android จากนั้นให้ไปเปลี่ยนภาษาใน control panel >> Regional and Language Options เป็น English(United States) แต่ลองแล้วก็ error เหมือนเดิม ไปเจออีกวิธีนึงได้ผลชะงักเลยตามด้านล่างครับ
เปิด command prompt แล้วพิมพ์
keytool -genkey -keypass android -keystore debug.keystore -alias androiddebugkey -storepass android -validity 10000 -dname "CN=Android Debug,O=Android,C=US"
Ref : link1
XPath หมายถึงภาษานึงที่ใช้งานร่วมกับ XML เพื่อใช้ในการดึงข้อมูลออกจากเอกสาร XML เป็นภาษาง่าย ๆ ในการใช้งาน สมมติเรามีข้อมูล XML ดังนี้ (ไฟล์ Berryz_Kobo.xml ในตัวอย่าง)
ปล , สามารถโหลด Code ตัวอย่าง ไฟล์ข้อมูลทั้งหมดได้ที่นี่ -> Example - XPath with Java.rar
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
จากข้อมูล XML ที่ให้มา สมมติว่าต้องการจะอ่านข้อมูลชื่อ Single ทั้ง 15 ออกมา หากใช้ Java ปกติอ่านเข้ามา จะใช้ DOM (Document Object Model) อ่านเข้ามา ดัง Code ข้างล่าง (สร้างไฟล์อ่าน XML ชื่อ XMLRead.java)
--------------------------------------------------------------------------------------
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
public class XMLRead {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new File("Berryz_Kobo.xml"));
Node berryzNode = doc.getFirstChild();
System.out.println("ROOT NODE NAME = "+berryzNode.getNodeName());
NodeList childNodes = berryzNode.getChildNodes();
for(int i=0; i < childNodes.getLength() ; i++){
Node dataNode = childNodes.item(i);
if( "singles".equalsIgnoreCase(dataNode.getNodeName())){
NodeList singleNodes = dataNode.getChildNodes();
for(int j=0; j < singleNodes.getLength() ; j++){
Node singleNode = singleNodes.item(j);
if( "single".equalsIgnoreCase( singleNode.getNodeName()) ){
NodeList nameNodes = singleNode.getChildNodes();
for(int k=0; k < nameNodes.getLength() ; k++){
Node nameNode = nameNodes.item(k);
if( "name".equalsIgnoreCase( nameNode.getNodeName())){
NodeList textNodes = nameNode.getChildNodes();
for(int l=0; l < textNodes.getLength() ; l++){
Node textNode = textNodes.item(l);
if( textNode.TEXT_NODE == textNode.getNodeType() ){
System.out.println("SINGLE = "+textNode.getNodeValue());
}
}
}
}
}
}
}
}
}
}
--------------------------------------------------------------------------------------
เมื่อสั่งรันดูจะได้ผลดังนี้ (ต้องมีไฟล์ Berryz_Kobo.xml อยู่ในโฟลเดอร์เดียวกับไฟล์ XMLRead.java ด้วย)
รูปแบบการอ่านของ DOM จะเป็นแบบ Tree ที่เริ่มจาก Root Node เสมอ ดังนั้นเมื่อสั่ง doc.getFirstChild() จะได้ berryzNode มา และจาก Root Node เราสามารถเข้าถึง Node ย่อย ๆ ได้ โดยเรียก getChildNodes() method ซึ่งการที่เราจะเอาชื่อ Single ออกมา ก็ต้องเข้าไปถึง “name” node ที่อยู่ภายใต้ “single” node (แต่ละ tag ก็คือ node ย่อยนั่นเอง) มีข้อสังเกตนิดนึงว่าข้อความ Text ใน Node นั้น จะถูกถือเป็น childNode ของ node นั้น ๆ โดยมี Type เป็น TEXT_NODE (จะเห็นว่าไม่ใช่ getNodeValue() ของ Node “name” แต่เป็น getNodeValue() ของ childNode ของ “name” Node ที่มี Type เป็น TEXT_NODE แทน)
จะเห็นว่ากว่าจะได้ข้อความมาแต่ละอันนั้นช่างลำบากยากเย็นเสียเหลือเกิน วนลูปกันจนเหนื่อย XPath สามารถช่วยลดเวลาเราได้ ดังนี้
สร้างไฟล์ XpathRead.java ขึ้นมา มี Code ดังข้างล่าง (ไว้ที่เดียวกับ XMLRead.java และ Berryz_Kobo.xml)
--------------------------------------------------------------------------------------
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
public class XPathRead {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new File("Berryz_Kobo.xml"));
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//name/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList textNameNodes = (NodeList) result;
for (int i = 0; i < textNameNodes.getLength(); i++) {
Node textNode = textNameNodes.item(i);
System.out.println("Single = "+textNode.getNodeValue());
}
}
}
--------------------------------------------------------------------------------------
เมื่อสั่งรันโปรแกรม ได้ผลดังนี้
สังเกตว่าใช้ XPath Query เป็น “//name/text()” ในการดึง text node ออกมาจาก node “name” โดย // หมายถึงให้วิ่งหา node ทุกตัวภายใต้ XML ปัจจุบัน (เมื่อใส่ //name จึงหมายถึงหา Node ที่มีชื่อว่า “name” ออกมาจาก XML ) /text() คือให้เอา text node ภายใต้ node ก่อนหน้าออกมา
ในความจริงการใช้ // ค่อนข้างจะเปลือง Performance ของโปรแกรมพอสมควร (เพราะวิ่งหาทั้ง XML เลย) เราสามารถเขียนให้ยาวขึ้นหน่อยนึง แต่มีประสิทธิภาพดีขึ้น โดยใช้ XPath Query เป็น “/berryz/singles/single/name/text()” (ทดลองเปลี่ยนดูได้ จะพบว่าให้ผลเหมือนเดิมเป๊ะ)
อันนี้เป็นเรื่องปกติของ XML คนที่ศึกษาอาจจะรู้อยู่แล้ว ทีนี้มาถึงเรื่องหลักที่อยากเขียนจริง ๆ เกี่ยวกับการใช้ XPath กับ Java ก็คือ เมื่อคุณไปเจอ XML ที่มีการระบุ Namespace คุณจะพบว่าการอ้างชื่อของ XML นั้นด้วย XPath จะไม่สามารถทำได้ ดังนี้
1. สร้างไฟล์ XML ที่มีการระบุ Namespace ชื่อ Berryz_Kobo_NS.xml ข้างในมีเนื้อหาดังนี้
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
สังเกตว่ามีการใส่ xmlns=http://boris-chung.live.space.com เพิ่มเติมเข้าไปใน root node แบบนี้คือการเพิ่ม Namespace เข้าไป การเพิ่มเข้าไปแบบนี้จะทำให้ childNode ของ node ที่เพิ่ม มีผลอยู่ใน Namespace ที่กำหนดด้วย
2. ลองสร้างไฟล์ XPathReadNS.java ขึ้นมา ข้างในมีเนื้อหาเหมือนไฟล์ XPathRead.java เพียงแต่เปลี่ยนไปอ่านไฟล์ Berryz_Kobo_NS.xml แทน ดังนี้
--------------------------------------------------------------------------------------
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
public class XPathReadNS {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new File("Berryz_Kobo_NS.xml"));
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("/berryz/singles/single/name/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList textNameNodes = (NodeList) result;
for (int i = 0; i < textNameNodes.getLength(); i++) {
Node textNode = textNameNodes.item(i);
System.out.println("Single = "+textNode.getNodeValue());
}
}
}
--------------------------------------------------------------------------------------
เมื่อสั่งรันโปรแกรมดู จะได้ผลดังนี้
ไม่ปรากฏอะไรขึ้นมาเลย (อันนี้รันบน Editplus พอรันเสร็จมันจะหยุดให้กดคีย์ใด ๆ ให้อัตโนมัติ แต่ถ้ารันบน Command-Prompt จะผ่านไปเลย) ทั้งนี้เพราะ XPath ไม่สามารถเข้าถึง Node ใด ๆ ที่มีการกำหนด Namespace เอาไว้ได้ หากไม่กำหนด Namespace ให้กับ XPath ก่อน ซึ่งขั้นตอนในการที่จะทำให้ XPath สามารถใช้งานกับ Node ที่มีการกำหนด Namespace เอาไว้นั้น ทำได้ดังนี้
1) สร้าง Class ที่ implement class “NamespaceContext” ดังนี้ (ในที่นี้ใช้ชื่อไฟล์ NamespaceContextProvider.java)
--------------------------------------------------------------------------------------
import java.util.Iterator;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
public class NamespaceContextProvider implements NamespaceContext {
String boundPrefix, boundURI;
NamespaceContextProvider (String prefix, String URI) {
boundPrefix = prefix;
boundURI = URI;
}
public String getNamespaceURI (String prefix) {
if (prefix.equals(boundPrefix)) {
return boundURI;
}
else if (prefix.equals(XMLConstants.XML_NS_PREFIX)) {
return XMLConstants.XML_NS_URI;
}
else if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
}
else {
return XMLConstants.NULL_NS_URI;
}
}
public String getPrefix (String namespaceURI) {
if (namespaceURI.equals(boundURI)) {
return boundPrefix;
}
else if (namespaceURI.equals(XMLConstants.XML_NS_URI)) {
return XMLConstants.XML_NS_PREFIX;
}
else if (namespaceURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
return XMLConstants.XMLNS_ATTRIBUTE;
}
else {
return null;
}
}
public Iterator getPrefixes (String namespaceURI) {
return null;
}
}
--------------------------------------------------------------------------------------
ในการ implement class นี้ต้อง implement 3 method โดยเราจะกำหนด prefix ที่ต้องการใช้จับคู่กับ Namespace และค่า uri ของ Namespace ของ tag นั้น ๆ ตอนนี้ดู ๆ ไปอาจจะยังไม่เข้าใจ ให้เข้าใจง่าย ๆ ว่า Copy ไปเลยก็แล้วกัน ง่ายดี
2) ใน Code ที่จะเรียกใช้งาน XPath นั้น เพิ่มการกำหนด Namespace เข้าไปดังนี้
--------------------------------------------------------------------------------------
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
public class XPathReadNS {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new File("Berryz_Kobo_NS.xml"));
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
//Namespace Specify
NamespaceContext nsContext = new NamespaceContextProvider("sinName" , "http://boris-chung.live.space.com");
xpath.setNamespaceContext(nsContext);
XPathExpression expr = xpath.compile("/sinName:berryz/sinName:singles/sinName:single/sinName:name/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList textNameNodes = (NodeList) result;
for (int i = 0; i < textNameNodes.getLength(); i++) {
Node textNode = textNameNodes.item(i);
System.out.println("Single = "+textNode.getNodeValue());
}
}
}
--------------------------------------------------------------------------------------
ชื่อ prefix นั้นเราจะกำหนดเป็นอะไรก็ได้ (ตามใจเรา) แต่จะต้องใช้ให้ตรงกันกับใน XPath Query ด้วย จากตัวอย่างคือ sinName โดยใช้กับ Namespace “http://boris-chung.live.space.com” และสังเกตว่าใน XPath Query นั้นตอนที่ระบุชื่อ tag จะต้องใส่ prefix นำหน้าทุกตัว (อย่าลืมว่า childNode ของ Node ที่กำหนด Namespace นั้น ก็จะถือว่ามี Namespace นั้นไปด้วย)
เมื่อรันไฟล์ออกมาได้ผลลัพธ์เหมือนเดิมดังนี้
สุดท้ายจะแถมเกี่ยวกับการใช้ XPath Query แบบที่กำหนดเงื่อนไขของ Attribute ให้ดูเป็นตัวอย่างเผื่อจะเอาไปใช้กัน สมมติว่าจะ Query สมาชิกที่ graduated ไปแล้ว (จากข้อมูล XML คือ “Ishimura Maiha”) ออกมา จะใช้ XPath Query ดังนี้
“/berryz/member[@graduated=’true’]/text()”
ให้ลองเอา XPath Query นี้ไปแทนในไฟล์ XPathRead.java ดู จะได้ผลลัพธ์ดังนี้
การใช้ @ เป็นการอ้างอิงถึง Attribute ของตัว Node ซึ่งในที่นี้เราอ้างถึงเฉพาะ member ที่มีค่า graduated เป็น “true” เท่านั้น จึงได้ Ishimura Maiha ออกมา
Point # 6 was the culprit in our case and that was then resolved! So the lessson was You are likely to get "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here" if you do not adhere to any of the above pointers.
only looks for
@Transactional
on beans in the same application context it is defined in. This means that, if you putin a
WebApplicationContext
for aDispatcherServlet
, it only checks for@Transactional
beans in your controllers, and not your services. See Section 15.2, “The DispatcherServlet” for more information.
If you are using Spring to wrap a Hibernate SessionFactory
and you are not using Spring-managed transactions, you may run into an issue. The reason is that Spring by default will wrap Hibernate’s SessionFactory
implementation and delegate to its own transactional version. If you are just using the simple ThreadLocal
-based session-per-request functionality, then when you attempt to open the Session
, you will get an IllegalStateException
thrown, with the error message "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here"
. This happens because Spring’s SessionFactoryUtils
checks if the Session is bound to Spring’s transactional support, and by default throws an error if it is not.
The solution to this is to set the property
<property name="exposeTransactionAwareSessionFactory"><value>false</value></property>
SessionFactory
instead of the proxied one. A snippet of code from AbstractSessionFactoryBean
shows where the check is done:
/**
* Wrap the given SessionFactory with a transaction-aware proxy, if demanded.
* @param rawSf the raw SessionFactory as built bybuildSessionFactory()
* @return the SessionFactory reference to expose
* @see #buildSessionFactory()
* @see #getTransactionAwareSessionFactoryProxy
*/
protected SessionFactory wrapSessionFactoryIfNecessary(SessionFactory rawSf) {
if (isExposeTransactionAwareSessionFactory()) {
return getTransactionAwareSessionFactoryProxy(rawSf);
}
else {
return rawSf;
}
}
A sample Spring config is shown below.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost:9001"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="exposeTransactionAwareSessionFactory"><value>false</value></property>
<property name="annotatedPackages">
<list>
<value>uk.co.researchkitchen.hibernate</value>
</list>
</property>
<property name="annotatedClasses">
<list>
<value>uk.co.researchkitchen.hibernate.Product</value>
<value>uk.co.researchkitchen.hibernate.ProductDescription</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
hibernate.current_session_context_class=thread
</value>
</property>
</bean>
</beans>
// Read properties file.
Properties properties = new Properties();
try {
properties.load(new FileInputStream("filename.properties"));
} catch (IOException e) { }
// Write properties file.
try {
properties.store(new FileOutputStream("filename.properties"), null);
} catch (IOException e) { }
The properties files can be stored in the classpath. This way they can be put inside jar files and it’s really useful for web applications when the absolute location of the properties files is not known. When I tested this in an web application it didn’t work:
Properties properties = new Properties() ;
URL url = ClassLoader.getSystemResource("test.properties");
properties.load(new FileInputStream(new File(url.getFile())));
The following example works to load the properties files in a web application. This snippet was tested on Tomcat 5.5. The ‘/’ represents the root of the class path. Otherwise the properties file location is considered relatively to “this” class (or to MyClass for the second example):
Properties properties = new Properties() ;
properties.load(this.getClass().getResourceAsStream("/seoimproved.properties"));
Similar example to use in a static context:
Properties properties = new Properties() ;
properties.load(MyClass.class.getResourceAsStream("/seoimproved.properties"));
The properties files can be loaded from any location.
Properties properties = new Properties() ;
properties.load(new FileInputStream("C:\\tmp\\test.properties"));
String propFile = "propConfig.properties";
URL url = Thread.currentThread().getContextClassLoader().getResource(propFile);
Properties prop = new Properties();
prop.load( url.openStream() );
Properties prop = new Properties();
try
{
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("hello.properties"));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
Copyright © 2009 My Knowledges
Design by Design Disease for Smashing Magazine | Blogger Templates by Blog and Web