This Season
 

A Tutorial for Creating a Relational Database to a Java Application

A Tutorial for Creating a Relational Database to a Java Applicationthumbnail
Java applications using the JDBC driver provide reliable database connectivity.

Connecting to a database is a common task if you program in Java. Many applications store their data in a database and use Java to create a user interface. Using Relational Database design allows you to create an efficient, easily managed data store that is robust and structured in a way that aids visualization. Applications in the Java language achieve database connectivity using the JDBC API, which allows programs to query their chosen data source.

Related Searches:
    Difficulty:
    Moderately Challenging

    Instructions

    Things You'll Need

    • JDK (installed on your computer)
    • Access to a database
      • 1

        Create the relational database. Many available systems facilitate producing a relational database, some commercial and some free (for example MySQL). Relational database design involves visualizing the relationships between the different elements in your data. Start by working out exactly what information you need to store. For example, a database for transactions from a sales business could include tables for "Client" and "Order," storing the details of each client in one record of the "Client" table.

      • 2

        Work out the relationships between the different "entities" or tables in your data. For example, a single client may be associated with more than one order, whereas an order will generally only be linked to a single client; this implies a "one to many" relationship between the "Client" and "Order" tables. Using relational design means that your data retains a good level of integrity, meaning that the different pieces of data are linked within a well-defined structure, making it more reliable and consistent when operations are carried out on it.

      • 3

        Download the correct JDBC driver for your particular database system. The JDBC handles interaction between your Java application and the database, and so it must be the correct type in order to communicate effectively between the two. Install your chosen driver on your computer, following any included instructions. This allows Java applications running on your computer to use the JDBC facility.

      • 4

        Connect to the database. Create a new Java project in whichever development environment you use. In your Main Class, insert the following import statement at the top:

        import java.sql.*;

        Next, within the main method, include this code to use the JDBC to connect to your data, making any relevant alterations to suit your database, system and location:

        try

        {

        /*Create an instance of the jdbc driver

        * Alter this to suit your chosen database system

        * - this example works for a MySQL database

        */

        Class.forName("com.mysql.jdbc.Driver").newInstance();

        //Enter your username and password for the database as strings

        String username = "username";

        String password = "password";

        //Enter the URL for your database as a string

        String databaseURL = "jdbc:mysql://yourdomain.com/database?user=" + username + "&password=" + password;

        //Create a connection instance

        java.sql.Connection aConnection = DriverManager.getConnection(databaseURL);

        //Create a statement handle instance to execute your queries

        Statement stmt = aConnection.createStatement();

        }

        catch( Exception E )

        { System.out.println( E.getMessage() ); }

        The main code must be contained within the "try" block in case of errors connecting to the database.

      • 5

        Query the database in your Java program. Include the following code inside your try block (at the end), making any changes to suit your particular database:

        //Create your SQL query as a string

        String aQuery = "Select * from ClientTable";

        //Execute the query and get the results back

        ResultSet resultSet = stmt.executeQuery(aQuery);

        //Loop through the results and output them

        while (resultSet.next())

        {

        //For a table with a column called clientFName

        System.out.println("Client First Name: " + resultSet.getString("clientFName"));

        }

        //Close the connections when you're done

        resultSet.close();

        stmt.close();

        aConnection.close();

        Naturally the queries you use will relate both to your database and to the purpose of your Java application. Create a user interface within your Java code, allowing the user access to the data via the JDBC techniques.

    Tips & Warnings

    • For updating and inserting data into your database, the Java code is similar, except the syntax "executeUpdate" is used instead of "executeQuery," and instead of returning the query results, the update or insert success is returned as an integer.

    • Try to avoid starting to build your database without taking the time to create a good database design. Consider using tools such as Entity-Relationship diagrams to help model your data, as the result will likely be more efficient. Use Primary and Foreign Keys to implement the relationships in your data.

    Related Searches

    References

    Resources

    • Photo Credit data image by jeancliclac from Fotolia.com

    Read Next:

    Comments

    You May Also Like

    Follow eHow

    Related Ads