ProgrammerTutorials.com » Article Details
main
main
main

Database Operations in Java

Date Added: February 10, 2010 10:58:18 AM
Author: Darrin Koltow
Category: Java
 

By Darrin Koltow, for TopHost.gr, a Greek web hosting company specializing in shared and reseller hosting.

 


Among Java's many classes are those for database operations. Using those classes to execute select, insert, delete and update statements is a simple, intuitive process.

Work through these instructions to produce a sample Java project that performs the database operations just mentioned. You'll be able to adapt this project to your own database-related needs.

Project requirements

You'll need the open source Java IDE NetBeans for this project. The competing Eclipse IDE may also work.

Once you've downloaded NetBeans, open that application and create a new project: choose "Alt-File-New," then "Java application" in the "Projects" pane, and "Java" in the "Categories" pane. Name the project something appropriate to database work, and give the name "Main" to the main class.

In the Main.java file, type the following statement under the "package..." statement:

import java.sql.*;

This statement tells the Java compiler to refer to the portion of its library that lets us execute database functions.

Paste the following function in place of the existing main function:

 

public static void main(String[] args)  {
        try {
        Connection cx = DriverManager.getConnection("JDBC:ODBC:movies");
        Statement sm = cx.createStatement();
        String strq = "SELECT * from movies;";
        ResultSet rs = sm.executeQuery(strq);
        if ( rs.next() ) {
            String str = rs.getString("title");
            System.out.println(str);
        }
        rs.close();
        cx.close();
        } catch (SQLException e) {
            System.out.println(e);
        }
}//main

Create the data

Our code is ready, but the data isn't. Let's create some in Microsoft Access. After opening Access, create the following table of sample data. Type the first row of this data as the table's field headings.

title,director,year_released
star wars,lucas,1977
excalibur,boorman,1980

Save the table with any appropriate name, then close Access.

Although we've created a complete database, the Java database driver we're using (JDBC.ODBC) needs a bit more info before it can tap into the new database. We need to define a data source name (DSN) for the movie table.

Create a data source name

Begin creating that DSN by pressing the "Start" button on the Windows Taskbar, then typing "ODBC" to locate the Open Database Connectivity (ODBC) manager. Click the link for that application to open it, then press "Add" on the "User DSN" tab. Select "Microsoft Access Driver" in the new dialog box that appears, then the "Finish" button.

In the "ODBC Microsoft Access Setup" dialog box, type "movies" in the "Data Source Name" text box, then press the "Select" button to navigate to the actual database file you created in Access. Press "OK" on the "...Setup" dialog box after you've selected the database file, then press "OK" on the main ODBC application window to close the app.


Watch for the continuation of this article on ProgrammerTutorials.com

 
main
Ratings
main
Reviews
main