1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
package mis.cinq.connexion;
/**
*
* @author laplace
*/
import java.util.Vector;
import java.awt.*;
import javax.swing.*;
/**
* JDBCConnectionDialog
* Dialogue pour la connexion à une base de données via JDBC.
* Permet de saisir les informations de connection :
* <blockqoute>
* <li> nom de l'utilisateur </li>
* <li> son mot de passe </li>
* <li> l'url du serveur de la BD </li>
* <li> le nom du driver JDBC </li>
* </blockqoute>
* <P>et de récupérer l'objet java.sql.Connection correspondant
*
* @author : Petko Valtchev, Philippe Genoud d'après Philip Milne
* (SUN Micro Systems).
*/
public class ConnectionDialog {
t_connect connexion;
// les différents titre des boutons du dialogue de connexion
static String[] optionNames = { "Connect" , "Cancel" };
public String text;
public boolean bool= false;;
String username;
String password;
String servername;
String port1;
String databaseName;
String driver1;
String server1;
// les TextFields pour la saisie des informations de connexion
JTextField userNameField;
JTextField passwordField;
JTextField serverNameField;
JTextField databaseNameField;
JComboBox serverField;
JComboBox driverField;
JComboBox portField;
// le Panel regroupant les "widgets" pour la connexion (TextFields
// et Labels)
JPanel connectionPanel;
Vector server;
Vector driver;
Vector port;
int resdialog;
/**
* Création du panel qui contientra tous les champs d'information pour la connexion.
* Attention, le dialogue n'est pas affiché. Il ne le sera que lorsque la méthode activate
* sera invoquée.
* @see #activate
*/
public ConnectionDialog(t_connect connexion) {
this.connexion = connexion;
Init();
}
public ConnectionDialog() {
Init();
}
public void Init() {
// Création des Labels et des Textfields pour la saisie des informations
// de connexion
JLabel userNameLabel = new JLabel("User name: ", JLabel.RIGHT);
userNameField = new JTextField(20);userNameField.setText("jpa");
JLabel passwordLabel = new JLabel("Password: ", JLabel.RIGHT);
passwordField = new JPasswordField(20);passwordField.setText("jpa");
JLabel serverLabel = new JLabel("Database URL: ", JLabel.RIGHT);
server = new Vector();
//server.add("jdbc:mysql:///test");
//server.add("jdbc:oracle:thin:@userman:1521:master");
server.add("jdbc:oracle:thin:@");
server.add("jdbc:mysql://");
server.add("jdbc:odbc:");//"jdbc:odbc:diffu"); // Access
server.add("jdbc:hsqldb:file:"); //HSQL création dans la base
server.add("jdbc:hsqldb:mem:"); //HSQL création de la base en mémoire
serverField = new JComboBox(server);
serverField.setEditable(true);
// "jdbc:oracle:thin:@hoff.imag.fr:1521:ufrima" pour oracle
JLabel driverLabel = new JLabel("Driver: ", JLabel.RIGHT);
driver = new Vector();
driver.add("oracle.jdbc.driver.OracleDriver"); // Oracle 8i
driver.add("com.mysql.jdbc.Driver");//"org.gjt.mm.mysql.Driver"); // Mysql
driver.add("sun.jdbc.odbc.JdbcOdbcDriver"); // Access
driver.add("org.hsqldb.jdbcDriver"); // HSQL
driverField = new JComboBox(driver);
// "oracle.jdbc.driver.OracleDriver" pour oracle
JLabel serverNameLabel = new JLabel("Server Name: ", JLabel.RIGHT);
serverNameField = new JTextField(20);serverNameField.setText("Localhost");
JLabel databaseNameLabel = new JLabel("Database Name: ", JLabel.RIGHT);
databaseNameField = new JTextField(20);databaseNameField.setText("gestcar");
JLabel portLabel = new JLabel("Port: ", JLabel.RIGHT);
port = new Vector();
port.add("1521");
port.add("3306");
portField = new JComboBox(port);
// Création d'un Panel et placement dans celui-ci des Labels
JPanel namePanel = new JPanel(false);
namePanel.setLayout(new GridLayout(7, 1));
namePanel.add(userNameLabel);
namePanel.add(passwordLabel);
namePanel.add(databaseNameLabel);
namePanel.add(serverNameLabel);
namePanel.add(portLabel);
namePanel.add(serverLabel);
namePanel.add(driverLabel);
// Création d'un Panel et placement dans celui-ci des TextFiels
JPanel fieldPanel = new JPanel(false);
fieldPanel.setLayout(new GridLayout(7, 1));
fieldPanel.add(userNameField);
fieldPanel.add(passwordField);
fieldPanel.add(databaseNameField);
fieldPanel.add(serverNameField);
fieldPanel.add(portField);
fieldPanel.add(serverField);
fieldPanel.add(driverField);
// Création d'un Panel regroupant le Panel des Labels et le Panel
// des TextFiedls
connectionPanel = new JPanel(false);
connectionPanel.setLayout(new BoxLayout(connectionPanel,
BoxLayout.X_AXIS));
connectionPanel.add(namePanel);
connectionPanel.add(fieldPanel);
/*
* Affiche le dialogue pour la connexion.
*/
resdialog = JOptionPane.showOptionDialog(
null, // composant parent, ici une Frame par défaut
connectionPanel, // le contenu du dialogue
"Connexion JDBC", // titre du dialogue
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE, // type du message (utilisé pour l'icone du dialogue)
null, // icone à afficher, ici icone par défaut en fonction du type de message
optionNames, //tableau de String indiquant les choix possibles pour l'utilisateur
optionNames[0] // la selection par defaut pour le dialogue
);
activate();
}
/**
* @return la référence pour l'adpatateur de connexion JDBC si la connexion
* a été demandée et a réussi, null sinon.
*/
public void activate() {
if (resdialog == 0) {
username = userNameField.getText();
password = passwordField.getText();
servername = serverNameField.getText();
port1 = (String) portField.getSelectedItem();
databaseName = databaseNameField.getText();
driver1 = (String) driverField.getSelectedItem();
server1 = (String) serverField.getSelectedItem();
System.out.println("Username :" + getUsername());
System.out.println("Password :" + getPassword());
if ( databaseName == null) {
text = "No database provided. Program stopped!";
bool = false;
//System.out.println("No database provided. Program stopped!");
System.exit(0);
}
/*try {
connexion = new t_connect(username,password,servername,port1,databaseName,driver1,server1);
//connexion = new t_connect(servername,username,password,databaseName); MYSQL
if ( ! connexion.acceptsConnection() ) {
text = "I'm not able to connect. Error:\n" + connexion.getError();
bool= false;
//System.out.println("I'm not able to connect. Error:\n" + connexion.getError());
} else {
text = "This DB accepts connection with these parameters!";
bool= true;
//System.out.println("This DB accepts connection with these parameters!");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Classes du driver non trouvées","alert", JOptionPane.ERROR_MESSAGE);
}*/
}
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setservername(String servername) {
this.servername = servername;
}
public String getservername() {
return servername;
}
public void setPort(String port1) {
this.port1 = port1;
}
public String getPort() {
return port1;
}
public void setDatabasename(String databaseName) {
this.databaseName = databaseName;
}
public String getDatabasename() {
return databaseName;
}
public void setDriver(String driver1) {
this.driver1 = driver1;
}
public String getDriver() {
return driver1;
}
public void setServer1(String server1) {
this.server1 = server1;
}
public String getServer1() {
return server1;
}
/**
* retourne l'url désignant la BD pour laquelle la connexion est établie.
* @return l'url désignant la bd
*/
public String getDburl() {
return (String) serverField.getSelectedItem();
}
/**
* programme de Test de la classe JDBCConnectionDialog
*/
/* public static void main(String s[]) {
ConnectionDialog dialConnexion = new ConnectionDialog();
//Connection conn = dialConnexion.activate();
dialConnexion.activate();
//if (conn == null)
// System.exit(0);
//else
// System.out.println("Connexion réussie");
}*/
} |