# How can I connect to CrateDB using JDBC?

**URL:** <https://community.cratedb.com/t/how-can-i-connect-to-cratedb-using-jdbc/400>\
**Category:** 3rd Party Tools\
**Created:** [May 21, 2020, 6:16pm UTC](https://community.cratedb.com/t/how-can-i-connect-to-cratedb-using-jdbc/400 "2020-05-21T18:16:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![miguel.arregui](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/miguel.arregui/32/126_2.png) [@miguel.arregui](https://community.cratedb.com/u/miguel.arregui)\
**Post date:** [May 21, 2020, 6:16pm UTC](https://community.cratedb.com/t/how-can-i-connect-to-cratedb-using-jdbc/400/1 "2020-05-21T18:16:25Z")

</div>

1- how can I connect to crateDB using jdbcjar?  
2- do we have a jdbc connection string?

---

<div class="post-metadata">

**Author:** ![hackerwin7](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/hackerwin7/32/161_2.png) [@hackerwin7](https://community.cratedb.com/u/hackerwin7)\
**Post date:** [May 24, 2020, 1:16pm UTC](https://community.cratedb.com/t/how-can-i-connect-to-cratedb-using-jdbc/400/2 "2020-05-24T13:16:40Z")

</div>

```auto
        Class.forName("io.crate.client.jdbc.CrateDriver");
        System.out.println("staring to connect...");
        Properties props = new Properties();
        props.put("user", "crate");
        Connection conn = DriverManager.getConnection("crate://127.0.0.1:4200/", props);

```

this will hangs on `DriverManager.getConnection`  
crate versino is 4.1.5  
crate-jdbc version is 2.6.0

---

<div class="post-metadata">

**Author:** ![smu](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/smu/32/60_2.png) [@smu](https://community.cratedb.com/u/smu)\
**Post date:** [May 25, 2020, 7:54am UTC](https://community.cratedb.com/t/how-can-i-connect-to-cratedb-using-jdbc/400/3 "2020-05-25T07:54:32Z")

</div>

Use the correct port for the postgresql protocol `5432` like documented here: [https://crate.io/docs/jdbc/en/latest/connect.html](https://crate.io/docs/jdbc/en/latest/connect.html).

---

<div class="post-metadata">

**Author:** ![miguel.arregui](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/miguel.arregui/32/126_2.png) [@miguel.arregui](https://community.cratedb.com/u/miguel.arregui)\
**Post date:** [May 31, 2020, 5:41pm UTC](https://community.cratedb.com/t/how-can-i-connect-to-cratedb-using-jdbc/400/4 "2020-05-31T17:41:15Z")

</div>

Hi @hackerwin7,

I hope you are well.

Yes, the [reference](https://crate.io/docs/jdbc/en/latest/connect.html) is correct and you will need [our driver](https://github.com/crate/crate-jdbc).

[Latest release of our JDBC driver](https://github.com/crate/crate-jdbc/releases).

Just to make sure it is clear, I am going to walk you through the most generic postgres wire protocol centric way, and will I assume that you understand your way around a terminal command line and have a JDK installed and setup in your environment.

1. Take the following snippet and save it to a file `PostgresProtocolWireSolo.java`:

```auto
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSetMetaData;
import java.util.Locale;
import java.util.Properties;

public class PostgresProtocolWireSolo {

    public static final int SO_RCVBUF = 1024 * 1024; // 1 MB
    public static final int MAX_BATCH_SIZE = 20000;
    public static final int QUERY_EXECUTION_TIMEOUT_SECS = 60;

    public static void main(String[] args) throws Exception {

        // In CrateDB:
        //
        // create table t (c0 int primary key, c1 text);
        // insert into t values(0, 'zero'), (1, 'one'), (2, 'two');

        String connectionUrl = "jdbc:postgresql://localhost:5432/";

        Properties connectionProps = new Properties();
        // https://jdbc.postgresql.org/documentation/head/connect.html
        connectionProps.put("user", "crate");
        connectionProps.put("password", "");
        connectionProps.put("ssl", false);
        connectionProps.put("recvBufferSize", SO_RCVBUF);
        connectionProps.put("defaultRowFetchSize", MAX_BATCH_SIZE);
        connectionProps.put("loginTimeout", 5); // seconds, fail fast-ish
        connectionProps.put("socketTimeout", QUERY_EXECUTION_TIMEOUT_SECS);
        connectionProps.put("tcpKeepAlive", true);

        try (Connection sqlConnection = DriverManager.getConnection(connectionUrl, connectionProps)) {
            sqlConnection.setAutoCommit(true);
            if (sqlConnection.isClosed()) {
                System.out.println("Connection is not valid");
                return;
            }
            try(Statement stmt = sqlConnection.createStatement()) {
                boolean checkResults = stmt.execute("select * from t");
                if (checkResults) {
                    ResultSet rs = stmt.getResultSet();
                    while(rs.next()) {
                        ResultSetMetaData metaData = rs.getMetaData();
                        int columnCount = metaData.getColumnCount();
                        for (int i = 1; i <= columnCount; i++) {
                            System.out.printf(
                                    Locale.ENGLISH,
                                    ">> col %d: %s: %s\n",
                                    i,
                                    metaData.getColumnName(i),
                                    rs.getObject(i));
                        }
                    }
                }
            }
        }
    }
}

```

1. From a terminal window, at the same path where the file is, compile it:

```auto
javac PostgresProtocolWireSolo.java

```

Notice that I like to be up to date with my java:

```auto
javac --version
javac 14
java --version
java 14 2020-03-17
Java(TM) SE Runtime Environment (build 14+36-1461)
Java HotSpot(TM) 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)

```

1. Now you need the appropriate JDBC driver, in jar format. You can copy the url (in the code snippet below) and paste it in a browser to get the file, or you can run the curl command, the important thing is that the jar file ends up right next to the `PostgresProtocolWireSolo.class` file generated in step 2:

```auto
curl --output pgql42_2_12.jar https://repo1.maven.org/maven2/org/postgresql/postgresql/42.2.12/postgresql-42.2.12.jar

```

1. Make sure CrateDB is up and running. Notice that in the source code, it expects to find a socket listener at the other end, on port `5432`. Sometimes I have postgres running in my host, and when I launch CrateDB, the default port 5432 will be already taken, thus CrateDB will bind to 5433 instead. This information will be available in CrateDB’s start logs, something like:

```auto
[2020-05-31T10:41:57,403][INFO][psql] [Zwölferkogel] publish_address {127.0.0.1:5433}, bound_addresses {[::1]:5432}, {127.0.0.1:5432}

```

1. With CrateDB up and running, use your favourite client to create a table and insert some data:

```auto
create table t (c0 int primary key, c1 text);
insert into t values(0, 'zero'), (1, 'one'), (2, 'two');

```

1. Run the program:

```auto
java -cp pgql42_2_12.jar:. PostgresProtocolWireSolo
>> col 1: c0: 1
>> col 2: c1: one
>> col 1: c0: 2
>> col 2: c1: two
>> col 1: c0: 0
>> col 2: c1: zero

```

Once the steps above are understood and you have the show running, you should fall back to our [driver jar](https://crate.io/docs/jdbc/en/latest/index.html), as described in the reference.

Kind regards,

---

<div class="post-metadata">

**Author:** ![amotl](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/amotl/32/617_2.png) [@amotl](https://community.cratedb.com/u/amotl)\
**Post date:** [August 20, 2021, 10:50am UTC](https://community.cratedb.com/t/how-can-i-connect-to-cratedb-using-jdbc/400/5 "2021-08-20T10:50:12Z")

</div>

Hi again,

I took the code example provided by Miguel as a blueprint and just added it to [cratedb-examples/spikes/jdbc-basic at main · crate/cratedb-examples · GitHub](https://github.com/crate/cratedb-examples/tree/main/spikes/jdbc-basic), which now provides a full and concise example program plus some guidelines outlining how to connect to CrateDB using both the vanilla [PgJDBC Driver](https://jdbc.postgresql.org/) and the [CrateDB JDBC Driver](https://github.com/crate/crate-jdbc).

With kind regards,  
Andreas.
