JDBC   «Prev  Next»
Lesson 2The course project assists in learning JDBC
ObjectiveDescribe the course project purpose and how it will assist you in learning JDBC.

JDBC Course Project assists in learning JDBC

Using the Brazil Project source code

For the course project, you will need to download the Brazil Project source code.
This collection of code will form the basis for the exercises you will do in the course.
As you complete the subsequent lessons, you will learn to use the fundamental concepts and syntax to perform various database tasks with JDBC.
To begin, download the JDBC course Brazil Project source code to a student folder.

Fundamental Concepts and Syntax to perform Simple Database Tasks

Here are the fundamental concepts and syntax to perform simple database tasks with JDBC:
  1. Loading the JDBC driver: Before you can connect to a database using JDBC, you need to load the JDBC driver for the database you are using. You can load the driver using the Class.forName() method, as shown in the following example:
    Class.forName("com.mysql.jdbc.Driver");
    
  2. Creating a database connection: Once you have loaded the JDBC driver, you can create a database connection using the DriverManager.getConnection() method. You need to provide the URL of the database, the username, and the password as parameters to this method, as shown in the following example:
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase", "myuser", "mypassword");
    
  3. Creating a statement object: Once you have a database connection, you can create a statement object using the Connection.createStatement() method. You can use this statement object to execute SQL queries and updates, as shown in the following example:
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");
    
  4. Executing SQL queries and updates: You can use the statement object to execute SQL queries and updates using the executeQuery() and executeUpdate() methods, respectively. The executeQuery() method returns a ResultSet object that contains the results of the query, while the executeUpdate() method returns the number of rows affected by the update. Here are some examples:

    ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");
    int rowsAffected = stmt.executeUpdate("UPDATE mytable SET name='John' WHERE id=1");
    
  5. Retrieving data from the ResultSet: Once you have executed a query and obtained a ResultSet object, you can retrieve the data from the result set using the ResultSet.next() method to move the cursor to the next row, and the ResultSet.getXXX() methods to retrieve the values of the columns in the current row. Here's an example:
    ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");
    while (rs.next()) {
        int id = rs.getInt("id");
        String name = rs.getString("name");
        int age = rs.getInt("age");
        // process the data
    }
    
  6. Closing the database resources: Once you are done with the database resources, you should close them to release the database connections and statements. You can use the close() method to close the resources, as shown in the following example:
    rs.close();
    stmt.close();
    conn.close();
    

  7. These are the fundamental concepts and syntax for performing simple database tasks with JDBC. There are many more advanced features and techniques available in JDBC for more complex database tasks, but these basics should get you started with using JDBC to interact with databases from Java applications.
You now know the basics of the course project. Next, we will explore your case study and learn about your responsibilities as a member of a solution development team.
Click the Exercise link to download the course Brazil Project source code to your student folder.
Course Project - Exercise