# Conectarse con Java

Guía:

[https://aripd.com/posts/mssql/](https://aripd.com/posts/mssql/)

Crear un proyecto Maven con Netbeans y en las dependencias colocar

```
<dependencies>
        <dependency>
            <groupId>net.sourceforge.jtds</groupId>
            <artifactId>jtds</artifactId>
            <version>1.3.1</version>
        </dependency>
    </dependencies>
```

Escribimos el programa que se conecte a la base de datos

```java
public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Class.forName("net.sourceforge.jtds.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/master;instance=SQLEXPRESS;", "sa", "12345aA-bB");
    Statement sta = conn.createStatement();

    String jdbcJtdsSql = "SELECT Name from sys.Databases";
    ResultSet rs = sta.executeQuery(jdbcJtdsSql);
    while (rs.next()) {
        String name = rs.getString("Name");
        System.out.println("name: " + name);
    }

    jdbcJtdsSql = "SELECT * FROM Inventory";
    rs = sta.executeQuery(jdbcJtdsSql);
    while (rs.next()) {
        long id = rs.getLong("id");
        String name = rs.getString("name");
        int quantity = rs.getInt("quantity");
        System.out.println("id: " + id + ", name: " + name + ", quantity: " + quantity);
    }
    }
```
