Where do I have a bug? A mistake? Why can’t I connect? My code,
package conexiones;
import java.sql.DriverManager;
import java.sql.Connection;
public class miconexion {
static String db = "futbol";
static String url = "jdbc:sqlserver://localhost;databaseName="+db+";";
//(local)\SQLEXPRESS
//String conn;
public Connection conn;
public miconexion() {
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection(url);
System.out.println("conexion exitosa");
}
catch(Exception e)
{
System.out.println(e);
System.out.println("no conectado");
}
}
public Connection getConnection()
{
return conn;
}
public void desconectar()
{
conn=null;
}
public static void main(String [] ar )
{
miconexion con = new miconexion();
con.getConnection();
}
}
You may need to enable basic authentication. By default MS SQL Server only accepts trusted (aka Windows login) connections. You’ll also need to create a user and specify the username and password in the connection string.