Advertisement
Ilham_Sianipar

Query Builder Java

Sep 25th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.81 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package main;
  7.  
  8. import com.mysql.jdbc.Connection;
  9. import com.mysql.jdbc.Statement;
  10. import java.awt.List;
  11. import java.sql.DriverManager;
  12. import java.sql.PreparedStatement;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.util.ArrayList;
  16. import java.util.Map;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19.  
  20. /**
  21.  *
  22.  * @author Sianipar
  23.  */
  24. public class QueryBuilder {
  25.  
  26.     private Connection koneksi = null;
  27.     private ResultSet rs = null;
  28.     private Statement stmt = null;
  29.     private PreparedStatement pst = null;
  30.  
  31.     private String _field = "*";
  32.     private String _table = "";
  33.     private String __join = "";
  34.     private String _where = "";
  35.     private String _group = "";
  36.     private String _order = "";
  37.     private String _limit = "";
  38.  
  39.     public QueryBuilder() {
  40.         try {
  41.             String url = "jdbc:mysql://192.168.43.201:3306/absensi";
  42.             DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  43.             koneksi = (Connection) DriverManager.getConnection(url, "absen", "absen123");
  44.         } catch (SQLException ex) {
  45.             Logger.getLogger(QueryBuilder.class.getName()).log(Level.SEVERE, null, ex);
  46.         }
  47.     }
  48.  
  49.     public QueryBuilder select(String field) {
  50.         this._field = field;
  51.         return this;
  52.     }
  53.  
  54.     public QueryBuilder table(String table) {
  55.         this._table = table;
  56.         return this;
  57.     }
  58.  
  59.     public QueryBuilder join(String type, String table, String on) {
  60.         this.__join += " " + type + " " + table + " ON (" + on + ")";
  61.         return this;
  62.     }
  63.  
  64.     public QueryBuilder where(String field, String condition, String value) {
  65.         this._where += (this._where.contains("WHERE")) ? " AND " : " ";
  66.         this._where += (this._where.contains("WHERE")) ? " " : " WHERE ";
  67.         this._where += field + " " + condition + " '" + value + "'";
  68.         return this;
  69.     }
  70.  
  71.     public QueryBuilder where(String field, String value) {
  72.         this._where += (this._where.contains("WHERE")) ? " AND " : " ";
  73.         this._where += (this._where.contains("WHERE")) ? " " : " WHERE ";
  74.         this._where += field + " = '" + value + "'";
  75.         return this;
  76.     }
  77.  
  78.     public QueryBuilder group(String group) {
  79.         this._group = " GROUP BY " + group;
  80.         return this;
  81.     }
  82.  
  83.     public QueryBuilder order(String order) {
  84.         this._order = " ORDER BY " + order;
  85.         return this;
  86.     }
  87.  
  88.     public QueryBuilder limit(String limit) {
  89.         this._limit = " LIMIT " + limit;
  90.         return this;
  91.     }
  92.  
  93.     public ResultSet execute() {
  94.         try {
  95.             String query = "SELECT " + this._field + " FROM " + this._table + this.__join + this._where + this._group + this._order + this._limit;
  96.             System.out.println(query);
  97.             stmt = (Statement) koneksi.createStatement();
  98.             rs = stmt.executeQuery(query);
  99.  
  100.             this._field = "*";
  101.             this._table = "";
  102.             this.__join = "";
  103.             this._where = "";
  104.             this._group = "";
  105.             this._order = "";
  106.             this._limit = "";
  107.  
  108.             return rs;
  109.         } catch (SQLException ex) {
  110.             Logger.getLogger(QueryBuilder.class.getName()).log(Level.SEVERE, null, ex);
  111.         }
  112.         return null;
  113.     }
  114.  
  115.     public void insert(Map<String, String> param) {
  116.         try {
  117.             String args = "";
  118.             String field = "";
  119.             int i = 0;
  120.  
  121.             for (Map.Entry<String, String> entry : param.entrySet()) {
  122.                 field += (i < param.size() - 1) ? entry.getKey() + "," : entry.getKey();
  123.                 args += (i < param.size() - 1) ? "?," : "?";
  124.                 i++;
  125.             }
  126.  
  127.             String query = "INSERT INTO " + this._table + " (" + field + ") VALUES (" + args + ")";
  128.             System.out.println(query);
  129.             pst = koneksi.prepareStatement(query);
  130.  
  131.             i = 1;
  132.             for (Map.Entry<String, String> entry : param.entrySet()) {
  133.                 pst.setString(i, entry.getValue());
  134.                 i++;
  135.             }
  136.  
  137.             pst.execute();
  138.             this._table = "";
  139.         } catch (SQLException ex) {
  140.             Logger.getLogger(QueryBuilder.class.getName()).log(Level.SEVERE, null, ex);
  141.         }
  142.     }
  143.  
  144.     public void update(Map<String, String> param) {
  145.         try {
  146.             String set = "";
  147.             int i = 0;
  148.  
  149.             for (Map.Entry<String, String> entry : param.entrySet()) {
  150.                 set += entry.getKey() + "=?";
  151.                 set += (i < param.size() - 1) ? "," : "";
  152.                 i++;
  153.             }
  154.  
  155.             String query = "UPDATE " + this._table + " SET " + set + this._where;
  156.             System.out.println(query);
  157.             pst = koneksi.prepareStatement(query);
  158.  
  159.             i = 1;
  160.             for (Map.Entry<String, String> entry : param.entrySet()) {
  161.                 pst.setString(i, entry.getValue());
  162.                 i++;
  163.             }
  164.  
  165.             pst.execute();
  166.             this._table = "";
  167.             this._where = "";
  168.         } catch (SQLException ex) {
  169.             Logger.getLogger(QueryBuilder.class.getName()).log(Level.SEVERE, null, ex);
  170.         }
  171.     }
  172.  
  173.     public void delete() {
  174.         try {
  175.             String query = "DELETE FROM " + this._table + this._where;
  176.             System.out.println(query);
  177.             pst = koneksi.prepareStatement(query);
  178.             pst.execute();
  179.  
  180.             this._table = "";
  181.             this._where = "";
  182.         } catch (SQLException ex) {
  183.             Logger.getLogger(QueryBuilder.class.getName()).log(Level.SEVERE, null, ex);
  184.         }
  185.     }
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement