viernes, 29 junio 2007

Getting started with JasperReports // JDBC connections (Part II)

This is the second part of a series of articles to get people started with JasperReports. In the second part I'll explain how to access your database to run simple queries and show them in a very simple report created with iReport.

The first thing we need to do to get moving is to have iReport ready. So if you haven't got it installed do so. Next step is to have the database driver in your iReport library folder. This means that you will need a jdbc jar packaged driver copied to the ./lib/ directory of iReport's base folder. This step is really important not doing so will not allow you to connect to the database. Most databases provide their own jdbc driver, there are many tutorials and references that speak about jdbc.

Now is the time to configure access to your database. You have to go to the menu "Data -> Connections/Datasources". Then you must click the "New" button.

iReport Connection Dialog

When you get this dialog, the first thing to do is to give a name to the connection "Name". Next is to specify the driver to connect to your database.Then you'll have to modify the JDBC URL to access your database (You can use the wizard to do this too). Finally you must specify the username and password with permission to get access to the database. It's convenient to test the connection before saving.

With your connection ready, is time to query the database. In my case I will be using PostgreSQL and I will build a simple query to get the database table names. For this you must go to the menu "Data -> Report Query".

iReport Data -> Report Query

When you insert your SQL query, in my case "SELECT * FROM pg_tables ORDER BY 1" iReport automatically gets the metadata for your query and stores them as report fields so you can use them during the development of your report.

Finally with the retrieved report fields we can now create our report. This is the result:

iReport final Result

Technorati Tags:

Posted by admin at 9:05 AM in Java

martes, 19 junio 2007

Getting started with JasperReports // Reporting in Java (Part I)

This is the first of a series of articles to get you started with JasperReports. In first place, JasperReports is one of Java's reporting engines. It's very powerful and has lots of features that makes it comparable to commercial reporting solutions such as Crystal Reports.

The first thing you need to do to start using this great peace of software is go to the project page and download it (wait). In the project page there is some documentation that will help you getting started.

You must know that JasperReports is a reporting engine that integrates many capabilities. Reports are developed using JasperReports own markup language (jrxml). You can define datasources to access databases, report variables, fields and parameters. All of these can then be used to populate and display data in your report. Once the report is done you must compile it. Compiled reports can then be processed by JasperReport's engine to be output in different formats (html, rtf, pdf, xls, printer...)

Reports can be developed either by hand, editing the report's source in an editor (jrxml) or with an ide (GUI). There are several GUI editors for JasperReports there even is a NetBeans plugin (still beta). The one I use and in my opinion the best is iReport. Downloading iReport will be the easiest way to get started with jasper. There is some documentation available on the project's page too. There you will find very good tutorials and quick start articles. The iReport package includes every library/jar you need, so its not necessary to download JasperReports package.

iReport Main Frame

Now that you have iReport installed its time to practice report development (learn about bands, groups, fields, variables, parameters, datasources... In further articles I will show you some of the advanced capabilities of these components). If you just want to build reports for data stored in a database (JDBC accessible) you won't need anything else, just iReport, so don'T worry about integrating jasperreports with your application yet. By now you can get used to run your reports by using the menu option “Build -> Run Report”

Technorati Tags:

Posted by admin at 8:45 AM in Java

jueves, 7 junio 2007

Replacing Apostrophes from Strings // Cleaning String to pass them as SQL statements

Such a simple thing as replacing an apostrophe with an escape character from a string sometimes can become a very tedious process, more if you're using String's replaceAll(...) function. Replacing apostrophes from Strings may be very useful when creating Statements to pass to an SQL database, preventing SQL injection. Many may say that using PreparedStatements is easier and safer, but in many occasions you can't use prepared statements to accomplish what you need.

The problem with the replaceAll function comes because of the arguments the function expects. In first place you must pass a regular expression that suits what you want to replace. Secondly, the function expects another regular expression with the String's replacement. In postgreSQL when passing apostrophes and other special characters as text, you must use the backslash as an escape character, just like in java. So if you want to insert "John's hand" as an argument to an Insert statement you must write something such as 'John\'s hand'.

If you refer to Pattern javadoc you can read:

The backslash character ('\') serves to introduce escaped constructs, as defined in the table above, as well as to quote characters that otherwise would be interpreted as unescaped constructs. Thus the expression \\ matches a single backslash and \{ matches a left brace.

Here is the answer to the problem. When replacing ' for \' you need to place 4 backslashes before the apostrophe.

String replacedString = "John's hand";
replacedString.replaceAll("'","\\\\'");

Technorati Tags:

Posted by admin at 8:25 AM in Java

miércoles, 6 junio 2007

Detecting Tab Key Pressed Event in JTextField 's // Event.VK_TAB KeyPressed

Adding a KeyListener to a JTextField to detect KeyPressed events is pretty straight forward. Buy maybe you have encountered some problems when trying to detect special key such as TAB's. This issue is due to LowLevel keyEvents captured by Swing's default FocusTraversalKeys. What we need to do to capture the VK_TAB KeyEvent is to remove the default FocusTraversalKeys from the component.

        myJTextField.setFocusTraversalKeys(
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);

Once we've done this with the component, the tab KeyEvent will not be captured by swing's default focus traversal keys and we will be able to add events normally.

myJTextField.addKeyListener(new java.awt.event.KeyAdapter() {
    public void keyPressed(java.awt.event.KeyEvent evt) {
        if(evt.getKeyCode() == evt.VK_TAB){
            /* PUT YOUR STUFF HERE OR CALL A FUNCTION */
            doSomething();
            /* If you want to change the focus to the next component */
            nextJComponent.grabFocus();
    }
});

Technorati Tags:

Posted by admin at 12:50 PM in Java

Google
 
« June »
SunMonTueWedThuFriSat
     12
3456789
10111213141516
17181920212223
24252627282930