Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.jimnz.ZediaVille.Objets;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- public class ConnectionDb {
- private final String dbName = "zediaville";
- private final String dbUsername="root";
- private final String dbPassword="";
- String url = "jdbc:mysql://localhost/" + dbName + "?user=" + dbUsername + "&password=" + dbPassword + "&useUnicode=true&characterEncoding=UTF-8";
- static Connection connection;
- public Boolean enable() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.err.println("jdbc driver unavailable!");
- return false;
- }
- try {
- connection = DriverManager.getConnection(url,dbUsername,dbPassword);
- return true;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- public Boolean disable() {
- try {
- if (connection!=null && !connection.isClosed()){
- connection.close();
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- return false;
- }
- public String select(String requete, String champ) {
- try {
- PreparedStatement stmt = connection.prepareStatement(requete);
- ResultSet results = stmt.executeQuery();
- if (!results.next()) {
- return "FALSE";
- } else {
- return results.getString(champ);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return "FALSE";
- }
- public ArrayList<String> selectArrayResults(String requete, String champ) {
- try {
- ArrayList<String> resultats = new ArrayList<String>();
- PreparedStatement stmt = connection.prepareStatement(requete);
- ResultSet results = stmt.executeQuery();
- while ( results.next() ) {
- resultats.add(results.getString(champ));
- }
- return resultats;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- public int selectCount(String requete) {
- try {
- PreparedStatement stmt = connection.prepareStatement(requete);
- ResultSet results = stmt.executeQuery();
- if (!results.next()) {
- return 0;
- } else {
- return results.getInt(1);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return 0;
- }
- public ArrayList<String> selectArrayChamps(String requete, ArrayList<String> champs) {
- try {
- ArrayList<String> resultats = new ArrayList<String>();
- Statement stmt = connection.createStatement();
- ResultSet results = stmt.executeQuery(requete);
- while ( results.next() ) {
- for (String s : champs) {
- resultats.add(results.getString(s));
- }
- }
- return resultats;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- public Boolean insert(String requete) {
- try {
- Statement statement = connection.createStatement();
- int results = statement.executeUpdate(requete);
- if (results == 1) {
- return true;
- } else {
- return false;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- public Boolean update(String requete) {
- try {
- Statement statement = connection.createStatement();
- int results = statement.executeUpdate(requete);
- if (results > 0) {
- return true;
- } else {
- return false;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- public Boolean delete(String requete) {
- try {
- Statement statement = connection.createStatement();
- int results = statement.executeUpdate(requete);
- if (results > 0) {
- return true;
- } else {
- return false;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement