Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.58 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package cims_project;
  6.  
  7. import com.mysql.jdbc.PreparedStatement;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.Vector;
  15. import javax.swing.JOptionPane;
  16.  
  17. /**
  18.  *
  19.  * @author Aron
  20.  */
  21. public class ClientDBControl {
  22.  
  23.     private String pass;
  24.     private String user;
  25.     private Connection conn;
  26.     private Statement stmt;
  27.     private ResultSet rs;
  28.     private PreparedStatement ps;
  29.  
  30.     public ClientDBControl() {}
  31.  
  32.     public void Connection() throws SQLException{
  33.         String path = "jdbc:mysql://localhost/cims";
  34.         user = "root";
  35.         pass = "";
  36.         conn = DriverManager.getConnection(path, user, pass);
  37.         stmt = conn.createStatement();
  38.     }
  39.  
  40.     private void log(Object aObject) {
  41.         System.out.println(aObject);
  42.     }
  43.  
  44.     public boolean addClient(Client c) throws ClassNotFoundException {
  45.         final int mmv = 1000;
  46.         final int wins = 0;
  47.         final int losses = 0;
  48.         final int draws = 0;
  49.         boolean result = false;
  50.         String insertString = "";
  51.  
  52.         insertString = "INSERT INTO clients (fname, lname, cphone, cemail, wins, losses, draws, mmv, warhammer, warhammer_40k)"
  53.                 + "VALUES('"
  54.                 + c.getFname() + "','"
  55.                 + c.getLname() + "','"
  56.                 + c.getCphone() + "','"
  57.                 + c.getCemail() + "','"
  58.                 + wins + "','"
  59.                 + losses + "','"
  60.                 + draws + "','"
  61.                 + mmv + "','"
  62.                 + c.isWh() + "','"
  63.                 + c.isWh40k() + "')";
  64.         try {
  65.             Connection();
  66.             stmt.executeUpdate(insertString);
  67.             result = true;
  68.             stmt.close();
  69.             conn.close();
  70.         } catch (SQLException ex) {
  71.             Object [] options = {"OK","Cancel"};
  72.             JOptionPane.showOptionDialog(null, "Too many characters inputted, please try again." + ex.getLocalizedMessage(), "WARNING", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
  73.         }
  74.         return result;
  75.     }
  76.  
  77.         public boolean modifyClient(Client c, int id) throws ClassNotFoundException {
  78.             boolean result = false;
  79.             String updateString = "";
  80.  
  81.             updateString = "UPDATE clients "
  82.                 + "SET fname='"+ c.getFname() + "',"
  83.                 + "lname='" + c.getLname() + "',"
  84.                 + "cphone='" + c.getCphone() + "',"
  85.                 + "cemail='" + c.getCemail() + "',"
  86.                 + "wins='" + c.getWins() + "',"
  87.                 + "losses='" +c.getLosses() + "',"
  88.                 + "draws='" + c.getLosses() + "',"
  89.                 + "mmv='" + c.getMmv() + "',"
  90.                 + "warhammer='" + c.isWh() + "',"
  91.                 + "warhammer_40k='" + c.isWh40k() + "'"
  92.                 + "WHERE c_id = '" + id + "'";
  93.        
  94.         try {
  95.             Connection();
  96.             stmt.executeUpdate(updateString);
  97.             result = true;
  98.             stmt.close();
  99.             conn.close();
  100.         } catch (SQLException ex) {
  101.             Object [] options = {"OK","Cancel"};
  102.             JOptionPane.showOptionDialog(null, "Too many characters inputted, please try again." + ex.getLocalizedMessage(), "WARNING", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
  103.         }
  104.  
  105.         return result;
  106.     }
  107.  
  108.     public Client selectClient(int id) throws Exception {
  109.         Client c = new Client();
  110.         int c_id;
  111.         String queryString = "";
  112.  
  113.         c_id = id;
  114.         queryString = "SELECT * FROM clients WHERE c_id='" + c_id + "'";
  115.  
  116.         try {
  117.             Connection();
  118.             rs = stmt.executeQuery(queryString);
  119.  
  120.             while (rs.next()) {
  121.                 c.setC_id(rs.getInt(1));
  122.                 c.setFname(rs.getString(2));
  123.                 c.setLname(rs.getString(3));
  124.                 c.setCphone(rs.getString(4));
  125.                 c.setCemail(rs.getString(5));
  126.                 c.setWins(rs.getInt(6));
  127.                 c.setLosses(rs.getInt(7));
  128.                 c.setDraws(rs.getInt(8));
  129.                 c.setMmv(rs.getInt(9));
  130.                 c.setWh(rs.getInt(10));
  131.                 c.setWh40k(rs.getInt(11));
  132.             }
  133.  
  134.             stmt.close();
  135.             conn.close();
  136.         } catch (SQLException ex) {
  137.             log("SQL Exception in selectClient method " + ex.getLocalizedMessage());
  138.         }
  139.  
  140.        
  141.  
  142.         return c;
  143.     }
  144.  
  145.     public ResultSet getAllClients() throws Exception {
  146.         String queryString = "SELECT * FROM clients";
  147.         Connection();
  148.         ResultSet rs = stmt.executeQuery(queryString);
  149. //        conn.close();
  150.         return rs;
  151.     }
  152.  
  153.     public boolean deleteClient(Client c, int id) throws ClassNotFoundException, ClassNotFoundException {
  154.         boolean result = false;
  155.         try {
  156.             Connection();
  157.             String deleteString = "DELETE FROM clients WHERE c_id = " + "'" + id + "'";
  158.             stmt.executeUpdate(deleteString);
  159.             result = true;
  160.             stmt.close();
  161.             conn.close();
  162.         } catch (SQLException ex) {
  163.              Object [] options = {"OK","Cancel"};
  164.             JOptionPane.showOptionDialog(null, "Client does not exist!" + ex.getLocalizedMessage(), "WARNING", JOptionPane.ERROR, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
  165.         }
  166.         return result;
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement