1
I’m trying to connect to a sql server database, but I’m not succeeding. Follows connection class and test method:
public class ConexaoBanco {
public static final String user = "sa";
public static final String pswd = "**********";
public static Connection conexao() {
Connection con = null;
final String jdbcDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
final String caminho= "jdbc:microsoft:sqlserver://localhost:1433;databaseName=cliente";
try {
Class.forName(jdbcDriver);
con = DriverManager.getConnection(caminho, user, pswd);
} catch (SQLException e) {
e.printStackTrace();
System.out.println(e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println();
}
return con;
}
public static void main(String[] args) {
try {
Connection conex = ConexaoBanco.conexao();
if (conex != null ) {
System.out.println("conectado");
} else {
System.out.println("não conectado");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
Post the mistake you’re having.
– R.Santos
No error. When running, it displays "not connected", which message is programmed in the return test of the connection method.
– Flávio Ribeiro
Take the condition from inside the
try
to see the error message returned in yourcatch
– R.Santos
I did not understand it well. I should take only the instantiation of the object from within the Try, or the if/Else as well?
– Flávio Ribeiro
try {
 Connection conex = ConexaoBanco.conexao();
 } catch (Exception e) {
 e.printStackTrace();
 System.out.println(e.getMessage());
 }
– R.Santos
Returns nothing, console goes blank.
– Flávio Ribeiro