La scansione a tappeto la intendo così :
Ho una lista di DBMS con Nome, Driver, Server, Porta, User, Password
codice:
Nome (DSN) Driver Server Porta User Password
DBVendita MySQL 192.14.36.52 3361 root kkkkk
DBPersonale ORACLE 192.14.36.80 1521 john jjjjj
DBMagazz MySQL 192.14.36.52 3362 jack uuuu
Allora faccio :
codice:
.....
import java.sql.*;
import java.util.*;
.....
// --------------------------------------------------------------------------------
// Per ogni riga della lista
// --------------------------------------------------------------------------------
driverDB = .....
serverName = .....
mydatabase = .....
username = .....
password = .....
porta = .....
if (openConnection(driverDB,serverName, mydatabase, username, password, porta))
// ---- Sono connesso al DB
else
// ---- Non è possibile connettersi, quindi il db non è probabilmente installato
// --------------------------------------------------------------------------------
public boolean openConnection(String driverDB, String serverName, String mydatabase, String username, String password, String porta) {
if (driverDB.equalsIgnoreCase("MYSQL")) {
driverName = "com.mysql.jdbc.Driver";
url = "jdbc:mysql://" + serverName + "/" + mydatabase;
} else if (driverDB.equalsIgnoreCase("MSACCESS")) {
driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=" + mydatabase + ";";
} else if (driverDB.equalsIgnoreCase("ORACLE")) {
driverName = "oracle.jdbc.driver.OracleDriver";
url = "jdbc:oracle:thin:@" + serverName + ":" + porta + ":" + mydatabase;
} else if (driverDB.equalsIgnoreCase("IBMDB2")) {
driverName = "com.ibm.db2.jcc.DB2Driver";
url = "jdbc:mysql://" + serverName + ":" + porta + "/" + mydatabase;
}
try {
// Load the JDBC driver
Class.forName(driverName);
Connettore = DriverManager.getConnection(url, u, p);
return true;
} catch (ClassNotFoundException e) {
// Could not find the database driver
return false;
} catch (SQLException e) {
// Could not connect to the database
return false;
}
}
// Nel codice vedi una faccina :D al suo posto ci va due punti + D (":" + "D").
Ovvio che i driver (i .jar) dei connettori devono essere raggiungibili dall'applicazione....