Advertisement
Guest User

Connectivity

a guest
May 5th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.23 KB | None | 0 0
  1. package com.example;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7.  
  8. /**
  9.  * Connectivity class.
  10.  */
  11. public class Connectivity {
  12.  
  13.     /**
  14.      * @param connectivity: It stores the Connectivity variable.
  15.      * @param name: It stores the Connectivity name variable.
  16.      */
  17.     private static Connectivity connectivity;
  18.     private String name = "";
  19.  
  20.     /**
  21.      * An empty private constructor of the class Connectivity.
  22.      */
  23.     private Connectivity() { }
  24.  
  25.     /**
  26.      * A only public object of the class Connectivity.
  27.      * @return connectivity
  28.      */
  29.     public static Connectivity getInstance() {
  30.         if (connectivity == null) {
  31.             connectivity = new Connectivity();
  32.         }
  33.         return connectivity;
  34.     }
  35.  
  36.     /**
  37.      * A method that load the connection of the class Connectivity.
  38.      * @return conn
  39.      */
  40.     public static Connection getConnection() throws SQLException {
  41.         Connection conn;
  42.         conn = DriverManager.getConnection("jdbc:mysql://localhost/inventory?autoReconnect=true&useSSL=false", "root", "qwerty12345");
  43.         return conn;
  44.     }
  45.  
  46.     /**
  47.      * A method that inserts a Product into the Database.
  48.      * @return recordCounter
  49.      */
  50.     public int insertProduct(Product p) throws SQLException {
  51.         Connection conn = null;
  52.         PreparedStatement ps = null;
  53.         int recordCounter = 0;
  54.  
  55.         try {
  56.             conn = this.getConnection();
  57.             ps = conn.prepareStatement("INSERT INTO Products " + "(Name, Description, Price, ProductId) " + "VALUES (?, ?, ?, ?)");
  58.             ps.setString(1, p.getName());
  59.             ps.setString(2, p.getDescription());
  60.             ps.setInt(3, p.getPrice());
  61.             ps.setInt(4, p.getProductID());
  62.  
  63.             recordCounter = ps.executeUpdate();
  64.         } catch (Exception e) {
  65.             e.printStackTrace();
  66.         } finally{
  67.             if(ps != null) {
  68.                 ps.close();
  69.             }
  70.             if(conn != null) {
  71.                 conn.close();
  72.             }
  73.         }
  74.         return recordCounter;
  75.     }
  76.  
  77.     /**
  78.      * A method that inserts a Customer into the Database.
  79.      * @return recordCounter
  80.      */
  81.     public int insertCustomer(Customer c) throws SQLException {
  82.         Connection conn = null;
  83.         PreparedStatement ps = null;
  84.         int recordCounter = 0;
  85.  
  86.         try {
  87.             conn = this.getConnection();
  88.             ps = conn.prepareStatement("INSERT INTO Customers " + "(Name, Address) " + "VALUES (?, ?)");
  89.             ps.setString(1, c.getName());
  90.             ps.setString(2, c.getAddress());
  91.  
  92.             recordCounter = ps.executeUpdate();
  93.         } catch (Exception e) {
  94.             e.printStackTrace();
  95.         } finally{
  96.             if (ps != null) {
  97.                 ps.close();
  98.             }
  99.             if(conn != null) {
  100.                 conn.close();
  101.             }
  102.         }
  103.         return recordCounter;
  104.     }
  105.  
  106.  
  107. //    // Database URL
  108. //    private static final String URL = "jdbc:mysql://localhost/inventory?autoReconnect=true&useSSL=false";
  109. //
  110. //    // Database credentials
  111. //    private static final String USERNAME = "root";
  112. //    private static final String PASSWORD = "qwerty12345";
  113. //
  114. //    private Connection connection = null;
  115. //    private PreparedStatement insert = null;
  116. //    private PreparedStatement insertCostumer = null;
  117. //    private String retrieveData = "";
  118. //
  119. //    public String getRetrieveCustomerData() {
  120. //        return retrieveCustomerData;
  121. //    }
  122. //
  123. //    private String retrieveCustomerData = "";
  124. //
  125. //    public String getRetrieveData() {
  126. //        return retrieveData;
  127. //    }
  128. //
  129. //    public Connectivity() {
  130. //
  131. //        // Insert records SQL clause
  132. //        String insertSQL = "INSERT INTO Products " + "(Name, Description, Price, ProductId) " + "VALUES (?, ?, ?, ?)";
  133. //        String insertCustomerSQL = "INSERT INTO Customers " + "(Name, Address) " + "VALUES (?, ?)";
  134. //
  135. //        // Select records SQL clause
  136. //        String selectSQL = "SELECT Name, Description, Price, ProductId FROM Products";
  137. //        String selectCustomerSQL = "SELECT Name, Address FROM Customers";
  138. //
  139. //        try {
  140. //            // Open a connection
  141. //            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
  142. //
  143. //            // PrepareStatement for Select and Insert clauses
  144. //            insert = connection.prepareStatement(insertSQL);
  145. //            insertCostumer = connection.prepareStatement(insertCustomerSQL);
  146. //
  147. //            PreparedStatement retrieve = connection.prepareStatement(selectSQL);
  148. //            PreparedStatement retrieveCustomer = connection.prepareStatement(selectCustomerSQL);
  149. //
  150. //            // Execute a retrieve query
  151. //            ResultSet rs = retrieve.executeQuery();
  152. //
  153. //            // Extract data from result set
  154. //            while(rs.next()){
  155. //                String name  = rs.getString("Name");
  156. //                String description = rs.getString("Description");
  157. //                int price = rs.getInt("Price");
  158. //                int productId = rs.getInt("ProductId");
  159. //
  160. //                // Return back the data
  161. //                retrieveData += name + "\t\t\t" + description + "\t\t\t€" + price + "\t\t\t\t" + productId +"\n";
  162. //            }
  163. //            rs.close();
  164. //
  165. //            // Execute a retrieve query
  166. //            ResultSet rsCustomer = retrieveCustomer.executeQuery();
  167. //
  168. //            // Extract data from result set
  169. //            while(rsCustomer.next()){
  170. //                String name  = rsCustomer.getString("Name");
  171. //                String address = rsCustomer.getString("Address");
  172. //
  173. //                // Return back the data
  174. //                retrieveCustomerData += name + "\t\t" + address + "\n";
  175. //            }
  176. //            rs.close();
  177. //        }
  178. //
  179. //        catch (SQLException sqlException) {
  180. //            sqlException.printStackTrace();
  181. //            System.exit( 1 );
  182. //        }
  183. //    }
  184. //
  185. //    public int addProduct(Product p) {
  186. //        int result = 0;
  187. //
  188. //        try {
  189. //            insert.setString(1, p.getName());
  190. //            insert.setString(2, p.getDescription());
  191. //            insert.setInt(3, p.getPrice());
  192. //            insert.setInt(4, p.getProductID());
  193. //
  194. //            result = insert.executeUpdate();
  195. //        }
  196. //        catch (SQLException sqlException) {
  197. //            sqlException.printStackTrace();
  198. //            close();
  199. //        }
  200. //
  201. //        return result;
  202. //    }
  203. //
  204. //    public int addCostumer(Customer c) {
  205. //        int result = 0;
  206. //
  207. //        try {
  208. //            insertCostumer.setString(1, c.getName());
  209. //            insertCostumer.setString(2, c.getAddress());
  210. //
  211. //            result = insertCostumer.executeUpdate();
  212. //        }
  213. //        catch (SQLException sqlException) {
  214. //            sqlException.printStackTrace();
  215. //            close();
  216. //        }
  217. //
  218. //        return result;
  219. //    }
  220. //
  221. //    // Close the Database Connection.
  222. //    private void close() {
  223. //        try {
  224. //            connection.close();
  225. //        }
  226. //        catch (SQLException sqlException) {
  227. //            sqlException.printStackTrace();
  228. //        }
  229. //    }
  230.  
  231.  
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement