Sto imparando Java e ho bisogno di eseguire semplicemente qualcosa di semplice per recuperare alcuni dati da MSSQL tramite JDBC. L'esempio nel mio libro non funziona (ma è diversi anni) e questo esempio di seguito da MS non funziona neanche per me:JDBC: esempio di connessione MSSql semplice non funzionante
http://msdn.microsoft.com/en-us/library/ms378956(v=sql.90).aspx
Ecco il mio codice:
package javasql;
import java.sql.*;
import java.util.*;
public class Program {
private static String url = "jdbc:sqlserver://localhost\\SQLExpress;database=Northwind;integratedSecurity=true;";
//private static String userName = "sa";
//private static String password = "myPassword";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
RunDemo();
}
public static void RunDemo() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection(url);
Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery("SELECT ProductName, Price FROM Products ORDER BY ProductName");
while(results.next()) {
System.out.println("Product Name: " + results.getNString("ProductName") + " Price: $" + results.getFloat("UnitPrice"));
}
} catch (ClassNotFoundException | SQLException ex) {
System.out.println(ex.getMessage());
}
}
}
Quando eseguo il codice, non ottengo alcun eccezioni generate .. ottengo solo questo nella finestra di output:
run:
com.microsoft.sqlserver.jdbc.SQLServerDriver
BUILD SUCCESSFUL (total time: 0 seconds)
sto utilizzando NetBeans 7.2. Per favore, qualcuno mi dia un esempio funzionante.
EDIT:
Tra l'altro, per la stringa di connessione, dove si vede la \\SQLExpress
, ho fatto provare a rimuovere questo e utilizzando instanceName=SQLExpress
invece .. ma che non ha avuto alcun effetto neanche.
EDIT 2:
OK, ho scaricato il driver JDBC più recente per MSSQL da MS e fatto riferimento i 2 file JAR in là. Ora sto ricevendo questo output:
run:
The connection to the host localhost, named instance SQLExpress failed.
Error: "java.net.SocketTimeoutException: Receive timed out".
Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434.
For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
BUILD SUCCESSFUL (total time: 15 seconds)
Progress .. almeno possiamo vedere che sta tentando di connettersi ora, qualcuno mi può illuminare per l'errore precedente però?
EDIT 3:
2 ulteriori problemi risolti .. uno è abilitare SQL Server Browser e il secondo stava permettendo TCP/IP per SQL Server. Grazie @Vikdor Ora sto ottenendo questo errore:
run:
The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
BUILD SUCCESSFUL (total time: 15 seconds)
ho controllato Windows Firewall e ha aggiunto una regola in entrata per consentire che la porta, ma sto ancora ricevendo l'errore precedente. Qualche idea?
EDIT 4:
provato la soluzione in questo link: http://www.coderanch.com/t/306316/JDBC/databases/SQLServerException-TCP-IP-connection-host
Non più ottenere errore nel EDIT 3. Ora ottenere un altro ...
run:
Sep 21, 2012 11:33:16 AM com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
This driver is not configured for integrated authentication. ClientConnectionId:577f359e-4774-45f3-96fb-588785911817
BUILD SUCCESSFUL (total time: 14 seconds)
Ottenere molto stanco di questo ora .. perché Java, perché ?? Seriamente ... Sono contento di lavorare principalmente con .NET. Beh, quando ho trovato la soluzione, mi post qui per fare in modo che possa aiutare gli altri prima di andare pazza come sto per ...
EDIT 5:
Questo ha aiutato: java connecting to MicrosoftSQLServer 2005
Ho inserito il percorso della directory nella variabile di ambiente PATH.Non ha funzionato, quindi ho anche inserito lo sqljdbc_auth.dll
nella mia cartella JDK C:\Program Files\Java\jdk1.7.0_04\bin
. Risolto.
sqljdbc.jar us usato in Java 1.5, sqljdbc4.jar richiede JDK6 + –