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
| import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.NumberFormat;
import java.text.ParsePosition;
public class MoteurInject implements ActionListener {
Grille parent;
MoteurInject(Grille parent){
this.parent=parent;
}
public void actionPerformed(ActionEvent événement) {
JButton boutonClic = (JButton) événement.getSource();
String texteChampNom = parent.champNom.getText();
String texteChampPrénom= parent.champPrénom.getText();
Object sourceEvénement = événement.getSource();
if (sourceEvénement == parent.boutonInject){
// Information d'accès à la base de données
String url = "jdbc:mysql://localhost/test";
String login = "root";
String passwd = "root";
Connection cn =null;
Statement st =null;
try {
// Etape 1 : Chargement du driver
Class.forName("com.mysql.jdbc.Driver");
// Etape 2 : récupération de la connexion
cn = DriverManager.getConnection(url, login, passwd);
// Etape 3 : Création d'un statement
st = cn.createStatement();
String sql = "CREATE DATABASE IF NOT EXISTS data;";
// "use data;"+"CREATE TABLE IF NOT EXISTS valeurs (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, prenom CHAR(30), NOM CHAR(30));"+"INSERT INTO valeurs VALUES (null,'"+texteChampPrénom+"','"+texteChampNom+"');"
// // Etape 4 : exécution requête
st.executeUpdate(sql);
// Si récup données alors étapes 5 (parcours Resultset)
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
// Etape 6 : libérer ressources de la mémoire.
cn.close();
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
String urlfin = "jdbc:mysql://localhost/data";
try {
// Etape 1 : Chargement du driver
Class.forName("com.mysql.jdbc.Driver");
// Etape 2 : récupération de la connexion
cn = DriverManager.getConnection(urlfin, login, passwd);
// Etape 3 : Création d'un statement
st = cn.createStatement();
String sql2 = "CREATE TABLE IF NOT EXISTS valeurs (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, prenom CHAR(30), nom CHAR(30));"
+"INSERT INTO valeurs (id, prenom, nom) VALUES (null,'"+texteChampPrénom+"','"+texteChampNom+"');";
// Etape 4 : exécution requête
st.executeUpdate(sql2);
// Si récup données alors étapes 5 (parcours Resultset)
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
// Etape 6 : libérer ressources de la mémoire.
cn.close();
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
} |
Partager