This Season
 

MySQL Java Tutorial

The Java Developers Kit includes a package known as the Java Database Connectivity API, or the JDBC for short. This provides a single interface for interacting with any database package that provides a JDBC driver. Most major database solutions, including MySQL, provide a JDBC driver either on their install discs or freely downloadable from their web page.

Related Searches:
    1. The MySQL JDBC Driver

      • The JDBC driver for MySQL is named MySQL :: Connector/J, and it is a very thorough implementation of JDBC API. The driver will be a JAR file which must be located on the Java classpath. The most common Java IDE's, particularly Eclipse and Netbeans, will do this for you, provided you reference the driver file as an library. In addition, in order to use commands from JDBC API, you'll need to insert the following command at the top of any class that will directly use the JDBC.

        import java.sql.*;

      Setup

      • While the JDBC attempts to make using your databases in your Java programs as simple as possible, there are a number of steps to follow to set up the database connection. Since it is possible to write a program that includes multiple database drivers, before connecting to a database you must inform the API of which driver you will be using. For this, you run the following:

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

        This will return an instance of the Driver class (which is included in java.sql), and you can store the reference to that instance for later use if you like. However, it's not necessary in most circumstances. Should you receive an error, this most likely means that your class path is not configured correctly.

      Creating a Connection

      • The database connection must be made using a URL or an IP address. If the database is running on the same machine as the Java program, this is easy: The URL is localhost, with one significant addition: the protocol (or url) identifier. A familiar one for the Web is "http://". For a MySQL JDBC database, the correct protocol identifier is "jdbc:mysql://".

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost", username, password);

      Dealing With Errors

      • Any error concerning a connection, a statement or a result set will be of the SQLException type. For more details on the specific error, you should catch SQLException and print its error message to the console. One potential source of confusion is speed at which connections time out. There is plenty of time to connect, run a few queries and get results, but almost certainly no time to wait for user input after a connection. If you must wait for user input, it is usually a better practice to disconnect, retrieve user input and then reconnect again to run the queries.

      Queries and Results

      • There are two key classes for interacting with the database once connected: the Statement and the ResultSet.

        Statement s = conn.createStatement();
        s.executeQuery("SELECT * FROM employees");
        ResultSet rs = s.getResultSet();

        Statement simply sends the SQL string to the database, so queries will need to be written in the native dialect of SQL for the database used. An important quality of a ResultSet is that it retrieves data from the server one row at a time, and only retrieves the data when the row is asked for. The above code has not downloaded any results yet from the server.

        Because it is impossible on most systems to know the size of a result set before all the results have been retrieved, it is best to store a result set, at least temporarily, within a data structure that can be efficiently resized. Java's LinkedList class is perfect for this.

        LinkedList results = new LinkedList();
        while (rs.next()) {
        results.add(rs.getString("firstName"));
        }

        And, it is always a good practice to close connections explicitly rather than let them timeout:

        rs.close();
        s.close();
        conn.close();

    Related Searches

    References

    Resources

    Read Next:

    Comments

    You May Also Like

    • A Tutorial for NetBeans Java 6.5 on MySQL

      The NetBeans interactive development environment is a product created by Sun Microsystems. It supports programming development in Java, C++, PHP and other...

    • MySQL JDBC Tutorials

      The Java Database Connectivity (or JDBC) API is a set of standard classes designed to provide a single unified interface for connecting...

    • MySQL ConnectorJ Tutorial

      The MySQL Connector/J driver is used to connect Java applications to a MySQL database. The driver is completely Java driven and does...

    • Java Sun SQL Tutorial

      Accessing a relational database from Java is a straightforward process. Many applications use Java interfaces for accessing data from a desktop environment....

    • How to Get Column Names in a Database Table in Java

      Java comes with a powerful set of database classes called the JDBC. These tools allow a Java developer, with only a few...

    • A Tutorial for Creating a Relational Database to a Java Application

      Connecting to a database is a common task if you program in Java. Many applications store their data in a database and...

    • NetBeans JDBC MySQL Tutorial

      Java Database Connectivity (JDBC) enables software developers to connect Java applications with MySQL, an open source database server which allows you to...

    • Step-by-Step Hibernate Tomcat MySQL Tutorial

      Hibernate is an open-source framework that supports persistence of middle-tier data objects written in Java. A middle-tier object is a unit of...

    • A Tutorial on SQL Server in JDBC

      The JDBC (Java Database Connectivity) driver is provided free by Microsoft for use with any SQL Server. Drivers help programs run and...

    • MySQL Sum Tutorial

      The MySQL SUM() statement is an aggregate or group function that operates on a set of values. The SUM() statement only works...

    • MySQL PHP Insert Tutorial

      MySQL and PHP are part of the LAMP (Linux, Apache, MySQL, PHP) software stack. Combining PHP and MySQL allows you to create...

    • List of Databases Used With Java

      MySQL is a popular database solution. MySQL continues to be popular due to its affordable price (free for home users), and the...

    • MySQL PHP Apache Tutorial

      Combining the open source MySQL database, PHP scripting language and Apache Web Server allows you to create dynamic web sites for free....

    • How to Connect OpenOffice Base to MySQL

      OpenOffice Base is a free database software that can create its own databases as well as connect to pre-existing databases. If you...

    • Tutorial on Enterprise Java Bean

      Enterprise Java Beans, or EJB, are the server-side components of Java technology, implementing an enterprise-level business logic, such as in transaction processing...

    • JDBC ResultSet Example

      The Java Database Connectivity (JDBC) Application Programmer Interface (API) provides classes for connecting to and accessing tabular data, including relational ...

    • How to Create a Java Applet in Netbeans

      Netbeans is an IDE for Java, PHP and Apache. IDE, which stands for Integrated Development Environment, is basically a fancy way of...

    • How to Create Database Web Applications

      Databases are the core component of modern communications. They store information of any kind and provide the ability to quickly query the...

    • How to Test a JDBC Driver

      The JDBC driver is used in applications to call procedures and query tables in mySQL. When programming a database connection, it's important...

    • Java Exception Handling Tutorials

      Java Exception Handling Tutorials. Writing applications in Java allows you to make easy and effective use of different resources. The Java language...

    Follow eHow

    Related Ads