Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Hibernate Basics Getting Started with Hibernate Using the Builder Pattern

Mohamed Mandour
Mohamed Mandour
7,783 Points

I do not know why my answer is still wrong. since the connectionBulder class has a default constructor please help

here is the my answer 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"); Connection c = new Connection.ConnectionBuilder().withJdbcVendor("sqlite").withDb("languages").withUser("th_user","mysuperlongpassword").build();

} }

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");
      Connection c = new Connection.ConnectionBuilder().withJdbcVendor("sqlite").withDb("languages").withUser("th_user","mysuperlongpassword").build();

  }
}

2 Answers

Mihai Craciun
Mihai Craciun
13,520 Points

your Connection object declaration should look like this

     Connection c = new ConnectionBuilder()
                                   .withJdbcVendor("sqlite")
                                   .withDb("languages")
                                   .withUser("th_user","mysuperlongpassword")
                                   .build();
Mohamed Mandour
Mohamed Mandour
7,783 Points

Thank you very much for your answer appreciated.