Olá pessoal, não estou conseguindo conectar a minha aplicação com o SQLServer 2008 R2.
Coloco para Debugar com breakpoint no netbeans e depois desta linha:
con = java.sql.DriverManager.getConnection(url, id, pass);
aparecem esses erros:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: Connection refused: connect. Please verify the connection properties and check that a SQL Server instance is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:130)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1195)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(SQLServerConnection.java:1054)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:758)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:842)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at Main.Util.conectBD.main(conectBD.java:51)
Java Result: 1
BUILD SUCCESSFUL (total time: 10 seconds)
estou tetando a conexão a muito tempo, ja mudei algumas vezes de driver, mas creio que seja no localhost o problema
enfim, alguem tem alguma dica?
segue abaixo o código, caso tenha algum erro, por favor avisem!
java.sql.Connection con;
java.sql.Statement s;
java.sql.ResultSet rs;
java.sql.PreparedStatement pst;
con=null;
s=null;
pst=null;
rs=null;
// Remember to change the next line with your own environment
String url= "jdbc:sqlserver://localhost:1433;" + "databaseName=BD_GYMControl;";
String id= "sa";
String pass = "tranca93";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = java.sql.DriverManager.getConnection(url, id, pass);
}catch(ClassNotFoundException cnfex){
cnfex.printStackTrace();
}
String sql = "select * from Fornecedor";
try{
s = con.createStatement();
rs = s.executeQuery(sql);
while( rs.next() ){
rs.getString("CNPJ");
rs.getString("Endereco");
rs.getString("Numero");
}
}
catch(Exception e){e.printStackTrace();}
finally{
if(rs!=null) rs.close();
if(s!=null) s.close();
if(con!=null) con.close();
}