Conectar SQL server 2005 com JDBC erro de servidor!

1 resposta
T

HelloWorld

Membro desde: 19/10/2006 15:59:13
Mensagens: 15
Online
Pessoal eu estou tentando testar um banco de dados com Java usando NetBeans :

meu codigo :

import java.sql.*; 
import com.microsoft.sqlserver.jdbc.*; 
public class TesteSql { 


public TesteSql() { 
} 

public static void main(String[] args) { 


// Declare the JDBC objects. 
Connection con = null; 
CallableStatement cstmt = null; 
ResultSet rs = null; 

try { 

// Establish the connection. 
SQLServerDataSource ds = new SQLServerDataSource(); 
ds.setUser("meu usuario"); 
ds.setPassword("meu password"); 
ds.setServerName("10.2.0.121"); 
ds.setPortNumber(1433); 
ds.setDatabaseName("Northwind"); 
con = ds.getConnection(); 

// Execute a stored procedure that returns some data. 
cstmt = con.prepareCall("{call dbo.SalesByCategory(?)}"); 
cstmt.setInt(1, 50); 
rs = cstmt.executeQuery(); 

// Iterate through the data in the result set and display it. 
while (rs.next()) { 
System.out.println("teste: " + rs.getString("LastName") + 
", " + rs.getString("FirstName")); 
System.out.println("MANAGER: " + rs.getString("ManagerLastName") + 
", " + rs.getString("ManagerFirstName")); 
System.out.println(); 
} 


} 

// Handle any errors that may have occurred. 
catch (Exception e) { 
e.printStackTrace(); 
} 

finally { 
if (rs != null) try { rs.close(); } catch(Exception e) {} 
if (cstmt != null) try { cstmt.close(); } catch(Exception e) {} 
if (con != null) try { con.close(); } catch(Exception e) {} 
} 
} 
}

Seguinte ele da o seguinte erro:

init:

deps-jar:

Compiling 1 source file to C:\Tarcisio\Java\Cadastro\testeSQL\build\classes

compile:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host has failed. java.net.ConnectException: Connection refused: connect

at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)

at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(Unknown Source)

at com.microsoft.sqlserver.jdbc.SQLServerConnection.loginWithoutFailover(Unknown Source)

at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)

at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnectionInternal(Unknown Source)

at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnection(Unknown Source)

at TesteSql.main(TesteSql.java:29)

debug:

BUILD SUCCESSFUL (total time: 3 seconds)

Pelo que entendi ele nao consegue conectar pois nao encontra o banco !

Como é que eu indico o banco para ele ? eu tenho que digitar o ip da minha maquina ou o nome do servidor SQL ou simplismente localhost ?

[size=“11”][color=“red”]* Editado: Lembre-se de utilizar BBCode em seus códigos - Carneiro[/color][/size] :joia:

1 Resposta

A

Se o banco está na sua máquina eu diria que o correto seria localhost

Criado 27 de abril de 2007
Ultima resposta 27 de abr. de 2007
Respostas 1
Participantes 2