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
|
package utilitaire;
import java.sql.*;
public class DB {
//Code pour ce connecetr a la base MySQL
static Connection Conn;
Statement st;
private String hoste = "jdbc:mysql://localhost/mabase";
private String login = "root";
private String pass = "";
private String err;
public String getErr() {
return err;
}
public DB(){
if(Conn == null){
this.Conn();
} else {
System.out.println("CONNEXION SQL EXISTANTE ! ");
}
}
private Connection Conn(){
try
{
Class.forName("com.mysql.jdbc.Driver");
Conn=DriverManager.getConnection(hoste,login,pass);
System.out.println("Connected");
}
catch (Exception e)
{
System.out.println("No Connction :"+Conn);
System.out.println(e.getMessage());
this.err = e.getMessage();
Log log = new Log();
log.SLog("----------Fichier : DB.java-------------\n");
log.SLog(e.getMessage()+"\n");
log.SLog("-----------------------------------------\n");
}
return Conn;
}
public ResultSet Query(String rq) throws SQLException{
st = Conn.createStatement();
return st.executeQuery(rq);
}
public int Update(String rq) throws SQLException {
// TODO Auto-generated method stub
st = Conn.createStatement();
PreparedStatement prepare = Conn.prepareStatement(rq);
int R = prepare.executeUpdate();
prepare.close();
return R;
}
} |