Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.sql.*;
- import java.util.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author moute
- */
- public class DataBase {
- private String server;
- private String base;
- private int port;
- private Connection connexion;
- public DataBase(String server, int port, String base) {
- this.server = server;
- this.port = port;
- this.base = base;
- }
- public boolean Connect() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
- }
- String url = "jdbc:mysql://" + this.server + ":" + String.valueOf(this.port) + "/" + this.base;
- try {
- this.connexion = DriverManager.getConnection(url, "root", "root");
- } catch (SQLException ex) {
- Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
- return false;
- }
- System.out.println("DBManager : Connected to the database " + this.base);
- return true;
- }
- public void Disconnect() {
- try {
- this.connexion.close();
- System.out.println("DBManager : Disconnected from the database " + this.base);
- } catch (SQLException ex) {
- Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public void Insert(final String table) {
- }
- public List<Map<String, String>> returnRow(final String table, final String... fields) {
- try {
- List<Map<String, String>> rows = new ArrayList<Map<String, String>>();
- Statement instruction = this.connexion.createStatement();
- ResultSet results = instruction.executeQuery("SELECT " + join(Arrays.asList(fields), ", ") + " FROM " + table);
- while (results.next()) {
- Map<String, String> map = new HashMap<String, String>();
- for (String field : fields)
- map.put(field, results.getString(field));
- rows.add(map);
- }
- return rows;
- } catch (SQLException ex) {
- Logger.getLogger(DataBase.class.getName()).log(Level.SEVERE, null, ex);
- return new ArrayList<Map<String, String>>();
- }
- }
- public static String join(Collection<String> s, String delimiter) {
- if (!s.isEmpty()) {
- Iterator<String> iter = s.iterator();
- StringBuffer buffer = new StringBuffer(iter.next());
- while (iter.hasNext()) {
- buffer.append(delimiter);
- buffer.append(iter.next());
- }
- return buffer.toString();
- } else {
- return "";}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement