Error connecting Java to SQL Server with JDBC

1

Good morning, I have the following code

public class main {


    private static Connection cn;

    public static Connection getConexion() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            cn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Persona");

        } catch (ClassNotFoundException | SQLException e) {
            cn = null;
        }

        return cn;
    }


    public static void main(String[] args) {
        //menuPrincipal mp = new menuPrincipal();
        //mp.setVisible(true);

        Connection prueba = main.getConexion();

        if (prueba != null) {
            System.out.println("Conexion realizada con exito...");
        } else {
            System.out.println("Error en la conexion...");
        }
    }
}

Which returns "Error in the connection". I have managed to make the same through the "Services" panel as you can see below

But my method does not return what I'm looking for.

    
asked by Lucas. D 11.08.2017 в 22:29
source

1 answer

2

It is still necessary to add a property in which the user and the access password are added

Properties connectionProps = new Properties();
connectionProps.put("user", "userName");
connectionProps.put("password", "password");
DriverManager.getConnection(jdbc:sqlserver://localhost:1433;databaseName=Persona, connectionProps);
    
answered by 11.08.2017 / 22:44
source