Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Hibernate Basics Getting Started with Hibernate Using the Builder Pattern

How to solve this challenge

The question is:

In Application.java, replace the call to the Connection constructor with code that leverages the ConnectionBuilder in the Connection class. Look at the source code in Connection.java to see what's available. Your resulting Connection object should have the same values as the one provided.

com/teamtreehouse/builder/Connection.java
package com.teamtreehouse.builder;

public class Connection {
  private String jdbcVendor;
  private String dbName;
  private String username;
  private String password;

  public Connection(String jdbcVendor, String dbName, String username, String password) {
    this.jdbcVendor = jdbcVendor;
    this.dbName = dbName;
    this.username = username;
    this.password = password;
  }

  public Connection(ConnectionBuilder builder) {
    this.jdbcVendor = builder.jdbcVendor;
    this.dbName = builder.dbName;
    this.username = builder.username;
    this.password = builder.password;
  }

  public static class ConnectionBuilder{
    private String jdbcVendor;
    private String dbName;
    private String username;
    private String password;

    public ConnectionBuilder(){}

    public ConnectionBuilder withJdbcVendor(String jdbcVendor) {
      this.jdbcVendor = jdbcVendor;
      return this;
    }

    public ConnectionBuilder withDb(String dbName) {
      this.dbName = dbName;
      return this;
    }

    public ConnectionBuilder withUser(String username, String password) {
      this.username = username;
      this.password = password;
      return this;
    }

    public Connection build() {
      return new Connection(this);
    }
  }
}
Application.java
import com.teamtreehouse.builder.Connection;
import com.teamtreehouse.builder.Connection.ConnectionBuilder;

public class Application {
  public static void main(String[] args) {
    // TODO: User the builder here instead
    Connection c = new Connection("sqlite","languages","th_user","mysuperlongpassword");
  }
}

2 Answers

Simon Coates
Simon Coates
28,694 Points

seemed to pass with:

    Connection c = (new ConnectionBuilder())
      .withJdbcVendor("sqlite").withDb("languages").withUser("th_user","mysuperlongpassword" ).build();
Steve Fielding
Steve Fielding
7,287 Points

@Simon why is the new Object declared in bracket

Simon Coates
Simon Coates
28,694 Points

brackets are typically used for clarity or to force execution order different to precedence rules. I'm a bit of a novice coder, so i use brackets maybe too often (i can't be bothered to remember the precedence rules). The challenge passes without the brackets, so seems like Java knows how the new applies to the class, and the methods are executed subsequently. It seems to be unnecessary. Sorry if i confused things.