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
| package PFA;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class projet {
private String dbURL = "";
private String user = "";
private String password = "";
private java.sql.Connection dbConnect = null;
private java.sql.Statement dbStatement = null;
public projet(String url, String user, String password) {
this.dbURL = url;
this.user = user;
this.password = password;
}
public Boolean connect() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
this.dbConnect = DriverManager.getConnection("jdbc:mysql:" + this.dbURL, this.user, this.password);
this.dbStatement = this.dbConnect.createStatement();
return true;
} catch (SQLException ex) {
Logger.getLogger(projet.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(projet.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(projet.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(projet.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
/**
* Executer une requete SQL
* @param sql
* @return resultat de la requete
*/
public ResultSet exec(String sql) {
try {
ResultSet rs = this.dbStatement.executeQuery(sql);
return rs;
} catch (SQLException ex) {
Logger.getLogger(projet.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public int exec_update(String sql) {
try {
int rs = this.dbStatement.executeUpdate(sql);
return rs;
} catch (SQLException ex) {
Logger.getLogger(projet.class.getName()).log(Level.SEVERE, null, ex);
}
return 0;
}
/**
* Fermer la connexion au serveur de DB
*/
public void close() {
try {
this.dbStatement.close();
this.dbConnect.close();
this.dbConnect.close();
} catch (SQLException ex) {
Logger.getLogger(projet.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
PFA mysqlCli = new projet("//localhost:3306/sabrine", "root", "sabrine");
if (mysqlCli.connect()) {
try {
int rs2 = mysqlCli.exec_update("INSERT INTO projet_db (id,add_mac,time,temperature,heartbeat) VALUES('w02','01245475','16-03-2012','40','99')");
if(rs2!=0)
{
System.out.println("insertion d'une ligne avec succee");
}
else System.out.println("insertion d'une ligne echoué");
ResultSet rs = mysqlCli.exec("SELECT * FROM projet_db");
if (rs != null) {
while (rs.next()) {
System.out.print("ligne1: " + rs.getString(1)+" ");
System.out.print("ligne2: " + rs.getString(2)+" ");
System.out.print("ligne3: " + rs.getString(3)+" ");
System.out.print("ligne4: " + rs.getString(4)+" ");
System.out.println("ligne5: " + rs.getString(5)+" ");
}
}
} catch (SQLException ex) {
Logger.getLogger(projet.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
System.out.println("Mysql connection failed !!!");
}
mysqlCli.close();
}
} |
Partager