|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: Dec 2004
Città: Treviso e Udine
Messaggi: 258
|
[Java] connessione ad un Data Base SQL Server
Premetto che il seguente codice non è mio: l'ho trovato in Internet. Ho installato le JDBC della Microsoft per SQL Server 2000 e ho aggiornato il CLASSPATH (credo, in realtà). Il problema è che ogni volta mi dà la stessa eccezione: "Could not find the database driver". Pensavo che l'indirizzo di registrazione dei driver fosse sbagliato ma ho controllato nella guida ufficiale e non è così. Per quanto riguarda l'indirizzo del server ci sono dati ipotetici non corrispondenti a nessun database esistente ma non è questo il problema, altrimenti mi darebbe un altro messaggio di errore...
Dove sto sbagliando? Codice:
import java.sql.*; public class SQLHere { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Connection connection = null; try { String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; String serverName = "127.0.0.1"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String url = "jdbc:JSQLConnect://" + mydatabase; // a JDBC url String username = "username"; String password = "password"; // Load the JDBC driver Class.forName(driverName); // Create a connection to the database connection = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { System.out.println("Could not find the database driver"); } catch (SQLException e) { System.out.println("Could not connect to the database"); } } } |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Jan 2001
Città: Milano
Messaggi: 5707
|
ClassNotFoundException?
penso che tu non abbia aggiunto correttamente al classpath il driver. |
![]() |
![]() |
![]() |
#3 |
Member
Iscritto dal: Dec 2004
Città: Treviso e Udine
Messaggi: 258
|
A quanto pare non sono affatto sicuro di aver aggiornato il CLASSPATH... qualcuno mi spiega come si fa?
![]() |
![]() |
![]() |
![]() |
#4 |
Member
Iscritto dal: Dec 2004
Città: Treviso e Udine
Messaggi: 258
|
Scusate, ho risolto!
![]() (dovrei proprio imparare ad avere più pazienza, vero?) |
![]() |
![]() |
![]() |
#5 | |
Senior Member
Iscritto dal: Jan 2001
Città: Milano
Messaggi: 5707
|
Quote:
era quello? |
|
![]() |
![]() |
![]() |
#6 |
Member
Iscritto dal: Dec 2004
Città: Treviso e Udine
Messaggi: 258
|
Ovviamente sì... cmq ora ho un nuovo problema: riesco a connettermi ad un database remoto ma se tento di connettermi ad un database locale mi dà eccezione "Could not connect to the database".
Tengo a precisare che ho modificato il codice: Codice:
import java.sql.*; public class SQLHere { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Connection connection = null; try { String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; String username = "username"; String password = "password"; // Load the driver Class.forName(driverName); // Create a connection to the database connection = DriverManager.getConnection("jdbc:microsoft:sqlserver://locale:1433;DatabaseName=database01", username, password); } catch (ClassNotFoundException e) { System.out.println("Could not find the database driver"); } catch (SQLException e) { System.out.println("Could not connect to the database"); } } } Dove potrebbe stare l'errore? Forse gli dà fastidio che sia locale? Possibile? Ultima modifica di Corvo80 : 20-01-2006 alle 13:56. |
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: Dec 2002
Città: Bologna
Messaggi: 483
|
Normalmente non dovrebbero esserci problemi se il database è locale..l'unico dubbio che mi sorge è che alcuni database hanno driver distinti nel caso in cui ti stia colleganto ad un database locale o ad uno remoto...controlla se nel tuo caso è così..
Un'altro possibile dubbio: hai dei firewall attivi sul pc?
__________________
Acer TravelMate 8103Wlmi |
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Mar 2004
Messaggi: 502
|
Magari al posto di "locale" va "localhost" (o 127.0.0.1)? o la tua macchina si chiama locale
![]() connection = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=database01", username, password); Poi ti conviene stampare direttamente l'eerore che magari si capisce dov'è che sbagli... catch (SQLException e) { e.printStackTrace(); <--nn sono sicuro sia così però è na roba simile } P.S. potrebbbero essere tutte cagate ![]() |
![]() |
![]() |
![]() |
#9 | |
Member
Iscritto dal: Dec 2004
Città: Treviso e Udine
Messaggi: 258
|
Quote:
![]() x Zeus: grazie ora provo... |
|
![]() |
![]() |
![]() |
#10 |
Member
Iscritto dal: Dec 2004
Città: Treviso e Udine
Messaggi: 258
|
Nisba: nè l'uno nè l'altro. L'eccezione che mi dà il compilatore è
[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket. Ma ho verificato: a) Il nome del PC è esatto e la macchina lo pinga. b) Il protocollo TCP/IP è enabled. c) La porta è corretta. d) Il database server è avviato (tant'è che riesco ad accedervi tranquillamente con qualunque altro applicativo). e) User e Password sono esatti, li ho verificati tramite altri mezzi. ... ma cosa cavolo succede?!!! ![]() |
![]() |
![]() |
![]() |
#11 |
Senior Member
Iscritto dal: Mar 2004
Messaggi: 502
|
Ops sorry, non avevo letto la nota in fondo..
Ho letto che il problema potrebbe essere proprio il nome della macchina, prova a inserire l'ip direttamente del pc... forse qua trovi qualcosa http://forum.java.sun.com/thread.jspa?threadID=419214 è la prima cosa che mi è uscita cercando il tuo errore su gugol (e dove ho letto dell'ip..) byez |
![]() |
![]() |
![]() |
#12 | |
Member
Iscritto dal: Dec 2004
Città: Treviso e Udine
Messaggi: 258
|
Quote:
BOH?!!! |
|
![]() |
![]() |
![]() |
#13 |
Member
Iscritto dal: Dec 2004
Città: Treviso e Udine
Messaggi: 258
|
Uppo nella speranza che qualcuno possa avere un'illuminazione...
![]() |
![]() |
![]() |
![]() |
#14 | |
Senior Member
Iscritto dal: Aug 2005
Città: Aulla (MS)
Messaggi: 1309
|
Quote:
Il drive jdbc di microsoft per sql server non supporta le trusted SQL Server connection, trovi maggiori dettagli qui: http://support.microsoft.com/default...b;en-us;313100 con il codice della prima pagina modificando la connessione in questo modo: connection = DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.1.60:1433;DatabaseName=Archivio;user=pippo;password=cane"); il codice funziona, purtruppo l'utente pippo deve essere un utente di Sql server e non facente parte di un dominio. |
|
![]() |
![]() |
![]() |
#15 | |
Senior Member
Iscritto dal: Aug 2005
Città: Aulla (MS)
Messaggi: 1309
|
Quote:
Ho trovato un driver jdbc open source qui: http://jtds.sourceforge.net/ con questo driver sono riuscito ad avere una connessione trusted utilizzando un utente del dominio, il codice è questo: import java.sql.DriverManager; import java.sql.Connection; import java.sql.Statement; import java.sql.ResultSet; import java.sql.Date; import java.sql.SQLException; public class TestConnessione { public static void main(String args[]) { // TODO Auto-generated method stub Connection connection = null; try { // String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; String driverName = "net.sourceforge.jtds.jdbc.Driver"; String serverName = "192.168.1.60"; String nomedatabase = "Archivio"; String portNumber = "1433"; String mydatabase = serverName + ":" + portNumber; String nomedominio = "CICERONE"; String url = "jdbc:jtds:sqlserver://" + mydatabase + ";Database=" + nomedatabase + ";domain=" + nomedominio; // a JDBC url String username = "ginogino"; String password = "federica74942"; // Load the JDBC driver Class.forName(driverName); // Create a connection to the database connection = DriverManager.getConnection(url, username, password); // Execute a query Statement stmt = connection.createStatement(); String sql; sql="SELECT CODFISC, COGNOMENOME FROM tbPersonale order by COGNOMENOME;"; ResultSet rs = stmt.executeQuery(sql); // Estrazione Dati while(rs.next() ) { // Legge i valori String CODFISC = rs.getString("CODFISC"); String COGNOMENOME = rs.getString("COGNOMENOME"); // Visualizza i dati System.out.print("Codice Fiscale: " + CODFISC ); System.out.println("Cognome e Nome: " + COGNOMENOME ); } } catch (ClassNotFoundException e) { System.out.println("Could not find the database driver"); } catch (SQLException e) { System.out.println("Could not connect to the database"); } } } |
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 10:39.