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 ?