May 18, 08 by the programmer
If you want to read a text resource file that is inside the jar file this is how you do it:
ClassLoader loader = ClassLoader.getSystemClassLoader();
InputStream inStream = loader.getResourceAsStream(”com/minanov/text_resource.txt”);
With the above code you get the Input stream of the file “text_resource.txt” that is in the package com.minanov
Now we need to convert this to a String. We can do this by using the function from my previous article about converting Input stream to a String.
Click on the above link to get the function.
May 18, 08 by the programmer
If you have a InputStream and you want to convert it to a String than this function will do that for you. Put this function in some of your Utility classes and that’s it.
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + “\n”);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
May 11, 08 by the programmer
Have you ever wanted to test your JPA queries without recomiling and running your application everytime you make a change. Now there is a way for doing that.
There is an open source project called “jpaquerytool“. This is basically a application that allows you to browse your JPA persistance units and execute queries and make updates to the database using JPA query language.
This is the project description taken from the website:
JQT is a query editor for Java Persistence API. The project’s main mission is to create a JPA implementation independent tool that can be used to browse pertsistence unit entities and properties, edit and run queries in a user friendly editor, browse queries results, persist and remove objects using visual editors, and provide an extensible architecture to help JPA developers add more functions. JQT parses persistence units located in its classpath, creating a entity metadata using a simple plug-in architecture to support multiple JPA implementations. Current version has support to latest versions of Hibernate Entity Manager, TopLink Essentials and OpenJPA as JPA implementations.
This is the URL of the project. Check it out
https://jpaquerytool.dev.java.net/
April 25, 08 by the programmer
I wanted to try to change the column name or add/drop new column using Toplink Essentials but it seems that there is no way of doing that using JPA.
TopLink Essentials can only drop and create tables, while OpenJPA by default
only adds/removes columns if the table already exist.
So you cant change the structure of your database using JPA. You have to use native queries to that or you can use some of the database tools to do that for you
April 25, 08 by the programmer
So I took my calculator to check and I noticed that I am not seeing things
So this is what I did
I took 2 numbers and tried to divide them to check the results
Here is what the results were
int i = 7;
int j = 2;
int k = i / j;
double res1 = (double) i / ( double) j;
double res2 = (double) ( i / j );
double res3 = ((double) i ) / j;
So if you take 2 integer numbers and divide them then the result will be an integer number
So be carefull what you divide in JAVA 