• 6 hours
  • Medium

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 4/15/20

Persist Java Object Into a Relational Database Using JDBC and Hibernate

Evaluated skills

  • Persist Java object into a relational database using JDBC and Hibernate
  • Question 1

    When objects are persisted in relational databases, what is each class normally mapped to?

    • Database

    • Table

    • Column

    • Row

    • Squirrel

  • Question 2

    Take a look at this code: 

    Connection conn = DriverManager.getConnection("jdbc:sqlite:./RobotShop.db");
    
    Statement cstmt = conn.createStatement();
        
    cstmt.execute("CREATE TABLE IF NOT EXISTS DeliveryVehicles (Registration varchar(10), Make varchar(20), Model varchar(20), RangeKm int);");
    
    Statement istmt = conn.createStatement();
    
    istmt.executeQuery("insert into DeliveryVehicles (Registration, Make, Model, RangeKm) values ('AB 12 CDE', 'Van', 'BigWhite', '300');");
    

     If you were to create a table from this code, what would this table look like? 

    • Table: Vehicles

      Column Name -> Registration Make Model RangeKm

       

    • Table: RobotShop

      Column Name -> DeliveryVehicle Make Model RangeKm
      Row 1 -> AB 12 CDE Van BigWhite 300

       

    • Table: DeliveryVehicles

      Column Name -> Registration Make Model RangeKm
      Row 1 -> AB 12 CDE Van BigWhite 300

       

    • Table: RobotShop

      Column Name -> DeliveryVehicles
      Row 1 ->

      Registration: AB 12 CDE

      Make: Van

      Model: BigWhite

      RangeKm: 300

       

  • Question 3

    Take a look at this table:

    Table: Supplier

    Column Names -> Company Address Telephone
    Row 1 -> Unusual Systems Leighton Buzzard, UK 01525 601260
    Row 2 ->  OpenClassrooms Paris, France  

    What would the code created from this table look like?

     

    Careful, there are several correct answers.
    • public class JdbcSupplierCrudPersister {
      
          Connection conn = null;
          
          public JdbcSupplierCrudPersister(String url) throws SQLException {
              conn = DriverManager.getConnection(url);
      
              Statement cstmt = conn.createStatement();
              cstmt.execute("CREATE TABLE IF NOT EXISTS Suppliers (Company varchar(10), Address varchar(50), Telephone varchar(20));");
          }
              
          public void createSupplier(String company, String address, String telephone) throws SQLException {
      
              PreparedStatement statement = conn.prepareStatement(
                      "insert into Suppliers (Address, Company, Telephone) values (?,?,?)");
              
              statement.setString(1,company);
              statement.setString(2,address);
              statement.setString(3,telephone);
      
              statement.executeUpdate();
          }
      
          public static void main(String[] args) throws SQLException {
              JdbcSupplierCrudPersister persister = new JdbcSupplierCrudPersister("jdbc:sqlite:./RobotShop.db");
              persister.createSupplier("Unusual Systems","Leighton Buzzard, UK", "01525601260");
              persister.createSupplier("Open Classrooms","Paris, France", "");        
          }
          
      }
      
      

       

    • public class JdbcSupplierCrudPersister {
      
          Connection conn = null;
          
          public JdbcSupplierCrudPersister(String url) throws SQLException {
              
              Statement cstmt = conn.createStatement();
              cstmt.execute("CREATE TABLE IF NOT EXISTS Suppliers (Company varchar(10), Address varchar(50), Telephone varchar(20));");
          }
              
          public void createSupplier(String company, String address, String telephone) throws SQLException {
      
              PreparedStatement statement = conn.prepareStatement(
                      "insert into Suppliers (Company, Address, Telephone) values (?,?,?)");
              
              statement.setString(1,company);
              statement.setString(2,address);
              statement.setString(3,telephone);
      
              statement.executeUpdate();
          }
      
          public static void main(String[] args) throws SQLException {
              JdbcSupplierCrudPersister persister = new JdbcSupplierCrudPersister("jdbc:sqlite:./RobotShop.db");
              persister.createSupplier("Unusual Systems","Leighton Buzzard, UK", "01525601260");
              persister.createSupplier("Open Classrooms","Paris, France", "");        
          }
          
      }
      
      

       

    • Cpublic class JdbcSupplierCrudPersister {
      
          Connection conn = null;
          
          public JdbcSupplierCrudPersister(String url) throws SQLException {
              conn = DriverManager.getConnection(url);
      
              Statement cstmt = conn.createStatement();
              cstmt.execute("CREATE TABLE IF NOT EXISTS Suppliers (Company varchar(10), Address varchar(50), Telephone varchar(20));");
          }
              
          public void createSupplier(String company, String address, String telephone) throws SQLException {
      
              PreparedStatement statement = conn.prepareStatement(
                      "insert into Suppliers (Company, Address, Telephone) values (?,?,?)");
              
              statement.setString(1,company);
              statement.setString(2,address);
              statement.setString(3,telephone);
      
              statement.executeUpdate();
          }
      
          public static void main(String[] args) throws SQLException {
              JdbcSupplierCrudPersister persister = new JdbcSupplierCrudPersister("jdbc:sqlite:./RobotShop.db");
              persister.createSupplier("Unusual Systems","Leighton Buzzard, UK", "01525601260");
              persister.createSupplier("Open Classrooms","Paris, France", "");        
          }
      }
      

       

    • Connection conn = DriverManager.getConnection("jdbc:sqlite:./RobotShop.db");
      
      Statement cstmt = conn.createStatement();
          
      cstmt.execute("CREATE TABLE IF NOT EXISTS Suppliers (Company varchar(10), Address varchar(50), Telephone varchar(20));");
      
      Statement istmt1 = conn.createStatement();
      
      istmt1.executeQuery("insert into Suppliers (Company, Address, Telephone) values ('Unusual Systems', 'Leighton Buzzard, UK', '01525 601260');");
      
      Statement istmt2 = conn.createStatement();
      
      istmt2.executeQuery("insert into Suppliers (Company, Address, Telephone) values ('OpenClassrooms', 'Paris, France', '');");
      

       

    • Connection conn = DriverManager.getConnection("jdbc:sqlite:./RobotShop.db");
      
      Statement istmt1 = conn.createStatement();
      
      istmt1.executeQuery("insert into Suppliers (Company, Address, Telephone) values ('Unusual Systems', 'Leighton Buzzard, UK', '01525 601260');");
      
      Statement istmt2 = conn.createStatement();
      
      istmt2.executeQuery("insert into Suppliers (Company, Address, Telephone) values ('OpenClassrooms', 'Paris, France', '');");
      

       

Ever considered an OpenClassrooms diploma?
  • Up to 100% of your training program funded
  • Flexible start date
  • Career-focused projects
  • Individual mentoring
Find the training program and funding option that suits you best