OracleXE JDBC Example


I have Oracle XE 11g installed on my computer with the associated JDeveloper IDE. To develop this example, I used my preferred IDE which is NetBeans 7.0.1.

To begin with, start a new Java Application project.  I have called mine HumanResources as I am using the sample HR schema of OracleXE. My package is com.training. Add the following JAR files to your libraries folder: ojdl.jar, ojdbc6dms.jar, and dms.jar. These files are located at C:\Oracle\Middleware\oracle_common\modules on my computer.

The code below is for the file HumanResources.java.

package oraclexe;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class HumanResources 
{
    public static void main(String[] args) 
    {
        // 1. Load the database driver.
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        }
        catch (ClassNotFoundException e)
        {
            System.err.println(e);
            System.exit(-1);
        }
        try
        {
            // 2. Open a connection to the database.
            Connection connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:XE"
                    , "HR"
                    , "YourPassword"
            );
            // 3. Build the query.
            String query = "SELECT * FROM HR.COUNTRIES";
            // 4. Execute the query.
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(query);
            // 5. Show the query results.
            while(rs.next())
            {
                System.out.println("Country: " + rs.getString(1)
                        + " - " + rs.getString(2));
            }
            // 6. Close the connection.
            connection.close();
        }
        catch(java.sql.SQLException e)
        {
            System.err.println(e);
            System.exit(-1);
        }
        catch(Exception e)
        {
            System.err.println(e);
            System.exit(-1);   
        }
    }
}

Compile and run the project. In the Output -> Debugger Console window, you should see the following output:

run:
Country: AR - Argentina
Country: AU - Australia
Country: BE - Belgium
Country: BR - Brazil
Country: CA - Canada
Country: CH - Switzerland
Country: CN - China
Country: DE - Germany
Country: DK - Denmark
Country: EG - Egypt
Country: FR - France
Country: HK - HongKong
Country: IL - Israel
Country: IN - India
Country: IT - Italy
Country: JP - Japan
Country: KW - Kuwait
Country: MX - Mexico
Country: NG - Nigeria
Country: NL - Netherlands
Country: SG - Singapore
Country: UK - United Kingdom
Country: US - United States of America
Country: ZM - Zambia
Country: ZW - Zimbabwe
BUILD SUCCESSFUL (total time: 4 seconds)

If you encounter problems, make sure you have included the necessary JAR files in your project. Secondly make sure that the database services are running. Thirdly check your connection string. And finally check your database driver class name is spelt correctly with the correct capitalisation.

Advertisements

Sun Java Mobile Communications Technical Overview


This website provides the architecture of the Sun Java Mobile Communications gateway server and the Sun Java Mobile Communications Clients for Pocket PC, Windows Mobile, and Palm devices. The document also details how these components communicate with each other to provide end users with a seamless data synchronization experience. You can access the website here.

Convert Word Document To PDF


In the course of business, there will be times when you want to convert a Microsoft Word document into a PDF document. The easiest way to do this that I have come across is to install OpenOffice. When you open a Word document in OpenOffice, you can save it as a PDF. The conversion is very good.

However, what if you want to do this programmatically from VB.NET? Well, it is very straight forward.

Download and install OpenOffice. You will need a copy of the Java Runtime Edition (JRE). There is a version of OpenOffice that you can download that includes the JRE. Set the PATH environment variable to include the paths to the JRE bin folder, and the OpenOffice program folder. Then download and extract JODConverter. Restart your computer.

When your computer has rebooted, open up a command window. Type the command java -version and press enter. This should output the version of the JRE that is installed on your system. Now type the command soffice and press enter. This should start OpenOffice. Your OpenOffice installation is complete.

Now before you go any further, you have everything you need to convert a Word document to PDF via the command line. The commands to do this are:

"C:\Program Files\OpenOffice.org 3\program\soffice.exe" -headless -nologo -norestore -accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager
java -jar D:\Tools\jodconverter-2.2.2\lib\jodconverter-cli-2.2.2.jar "D:\Files\Test.doc" "D:\Files\Test.PDF"

To execute this code in a consistent and easy manner from VB.NET is a very simple process. Open up Visual Studio .NET 2010 and create a new VB.NET Console Application. Copy the code below into the Module1.vb file and rename the file to EntryPoint.vb.

Imports System
Imports System.Configuration
Imports System.IO
Imports System.Text
Imports System.Collections.Generic

Public Class EntryPoint

Public Shared Sub Main(ByVal args As String())

Dim batchFileCommandText As New StringBuilder
Try
' Prepare the batch file text.
batchFileCommandText.Append("""")
batchFileCommandText.Append(ConfigurationManager.AppSettings("OpenOffice"))
batchFileCommandText.Append("""")
batchFileCommandText.Append(" ")
batchFileCommandText.Append(ConfigurationManager.AppSettings("OpenOfficeParameters"))
batchFileCommandText.Append(vbNewLine)
batchFileCommandText.Append(ConfigurationManager.AppSettings("JODConverter"))
batchFileCommandText.Append(" ")
batchFileCommandText.Append("""")
batchFileCommandText.Append(args(0))
batchFileCommandText.Append("""")
batchFileCommandText.Append(" ")
batchFileCommandText.Append("""")
batchFileCommandText.Append(args(1))
batchFileCommandText.Append("""")
' Create the batch file and save the batch file text.
Using batchFile As New StreamWriter(ConfigurationManager.AppSettings("BatchFile"))
batchFile.Write(batchFileCommandText.ToString())
End Using
' Execute the batch file.
Process.Start(ConfigurationManager.AppSettings("BatchFile"))
' The conversion is a success, print a message and exit.
System.Console.WriteLine("Conversion successful...")
Catch ex As Exception
System.Console.WriteLine(ex)
End Try
End Sub
End Class

Add an app.config file to your project and include the following application settings.

<appSettings>
<clear />
<add key="OpenOffice" value="C:\Program Files\OpenOffice.org 3\program\soffice.exe" />
<add key="OpenOfficeParameters" value="-headless -nologo -norestore -accept=socket,host=localhost,port=8100;urp;StarOffice.ServiceManager" />
<add key="JODConverter" value="java -jar D:\Tools\jodconverter-2.2.2\lib\jodconverter-cli-2.2.2.jar" />
<add key="BatchFile" value="D:\Files\convert.bat" />
</appSettings >

Note: If your applications are installed in different locations to the one described above, the you will need to change your app.config file to point to the right locations.

Execute the code now passing in for the parameters the input file followed by the output file. If all goes according to plan, you should now have a generated PDF.

Creating a Java Shell Application


Dr. R. K. Ghosh, Professor at the Department of Computer Science & Engineering at the Indian Institute of Technology Kanpur in India has created a Java Shell application.

You can read about the application and download the source code here, and the list of commands you can use with the Java Shell can be found here.

Java Shell is a very good starting point for creating your own Java-based command windows.

Learning the Java Programming Language


The best way to get started learning Java is to download and install Java and the NetBeans IDE via the Oracle Technology Network for Java Developers. Once you have your IDE up and running, the next step is to work your way through The Java Tutorial.

Once you have worked your way through the tutorial you will have enough knowledge to develop your own applications including desktop applications, class libraries, database-driven applications, and applications distributed over networks.