Guest User

Untitled

a guest
Feb 19th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.99 KB | None | 0 0
  1. package ebuy;
  2.  
  3. import java.sql.*;
  4. import java.io.*;
  5. import ebuy.ItemCats;
  6. import ebuy.Items;
  7. import ebuy.Users;
  8.  
  9. public class eBuyUniversal
  10. {
  11.     private Connection createConnection()
  12.     {
  13.         Connection connection = null;
  14.         try
  15.         {
  16.             String connectionURL;
  17.            
  18.             connectionURL = "jdbc:mysql://localhost:3306/ebuy";
  19.            
  20.             Class.forName("com.mysql.jdbc.Driver");
  21.            
  22.             connection = DriverManager.getConnection(connectionURL, "root", "uniboyz");
  23.        
  24.         }
  25.         catch(Exception e)
  26.         {
  27.             System.out.println("Unable to connect to database.");
  28.         }
  29.         return connection;
  30.  
  31.     }
  32.    
  33.     public String getCatList(int parent, int depth)
  34.     {
  35.         String cats = "";
  36.         try
  37.         {
  38.             Connection connection;
  39.             Statement statement;
  40.             ResultSet resultSet;
  41.        
  42.             connection = createConnection();
  43.            
  44.             statement = connection.createStatement();
  45.            
  46.             resultSet = statement.executeQuery("SELECT * FROM itemcats WHERE parentCat="+parent);
  47.            
  48.             while(resultSet.next())
  49.             {
  50.                 for(int i=0; i<depth; i++)
  51.                     cats += " - ";
  52.                
  53.                 if(parent == 0)
  54.                     cats += "<b>";
  55.                
  56.                 cats += "<a href='category.jsp?catID=" + resultSet.getInt(1) + "'>" + resultSet.getObject(2) + "</a>";
  57.                
  58.                 if(parent == 0)
  59.                     cats += "</b>";
  60.                
  61.                 cats += "<br />";
  62.                 cats += getCatList(resultSet.getInt(1),depth+1);
  63.             }
  64.             connection.close();
  65.         }
  66.         catch(Exception e)
  67.         {
  68.             System.out.println("Unable to connect to database.");
  69.         }
  70.        
  71.         return cats;
  72.     }
  73.    
  74.     public ItemCats getItemCat(int categoryID)
  75.     {
  76.         ItemCats currentCat = new ItemCats();
  77.        
  78.         try
  79.         {
  80.             Connection connection;
  81.             Statement statement;
  82.             ResultSet resultSet;
  83.        
  84.             connection = createConnection();
  85.            
  86.             statement = connection.createStatement();
  87.            
  88.             resultSet = statement.executeQuery("SELECT * FROM itemcats WHERE catID=" + categoryID);
  89.            
  90.             while(resultSet.next())
  91.             {
  92.                 currentCat.setCatID(resultSet.getInt(1));
  93.                 currentCat.setCatName(resultSet.getString(2));
  94.                 currentCat.setParentCat(resultSet.getInt(3));
  95.             }
  96.             connection.close();
  97.         }
  98.         catch(Exception e)
  99.         {
  100.             System.out.println("Unable to connect to database.");
  101.         }
  102.        
  103.         return currentCat;
  104.     }
  105.    
  106.     public boolean createUser(Users newUser)
  107.     {
  108.        
  109.         try
  110.         {
  111.             Connection connection;
  112.             connection = createConnection();
  113.            
  114.             String query = "INSERT INTO users (username, password, firstName, lastName, email, phoneNo, DOB, streetNo, streetName, suburb, city, postcode, country) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
  115.            
  116.             PreparedStatement preparedStmt = connection.prepareStatement(query);
  117.            
  118.             preparedStmt.setString(1,newUser.getUserName());
  119.             preparedStmt.setString(2,newUser.getPassword());
  120.             preparedStmt.setString(3,newUser.getFirstName());
  121.             preparedStmt.setString(4,newUser.getLastName());
  122.             preparedStmt.setString(5,newUser.getEmail());
  123.             preparedStmt.setString(6,newUser.getPhoneNo());
  124.             preparedStmt.setString(7,newUser.getDOB());
  125.             preparedStmt.setString(8,newUser.getStreetNo());
  126.             preparedStmt.setString(9,newUser.getStreetName());
  127.             preparedStmt.setString(10,newUser.getSuburb());
  128.             preparedStmt.setString(11,newUser.getCity());
  129.             preparedStmt.setString(12,newUser.getPostcode());
  130.             preparedStmt.setString(13,newUser.getCountry());
  131.            
  132.             preparedStmt.executeUpdate();
  133.                    
  134.             if(!connection.isClosed())
  135.             {
  136.                 System.out.println("Successfully connected to " + "MySQL server using TCP/IP...<br /><br />");
  137.                 return true;
  138.             }
  139.        
  140.             connection.close();
  141.         }
  142.         catch(SQLException e)
  143.         {
  144.             System.out.println("Unable to connect to database.");
  145.             System.out.println("<br />" + e.getMessage());
  146.             System.out.println("<br />" + e.getErrorCode());
  147.             return false;
  148.         }
  149.         return false;
  150.  
  151.     }
  152.    
  153.     public boolean createItem(Items newItem)
  154.     {
  155.         try
  156.         {
  157.             Connection connection;
  158.             connection = createConnection();
  159.            
  160.             String query = "INSERT INTO items (seller, catID, itemName, itemDescription, startTime, duration, currentBidID, currentPrice, reserve) VALUES(?,?,?,?,?,?,?,?,?)";
  161.            
  162.             PreparedStatement preparedStmt = connection.prepareStatement(query);
  163.  
  164.             preparedStmt.setInt(1,newItem.getSellerID());
  165.             preparedStmt.setInt(2,newItem.getCategoryID());
  166.             preparedStmt.setString(3,newItem.getItemName());
  167.             preparedStmt.setString(4,newItem.getItemDescription());
  168.             preparedStmt.setLong(5,newItem.getStartTime());
  169.             preparedStmt.setLong(6,newItem.getDuration());
  170.             preparedStmt.setInt(7,newItem.getCurrentBidID());
  171.             preparedStmt.setDouble(8,newItem.getCurrentPrice());
  172.             preparedStmt.setDouble(9,newItem.getReserve());
  173.            
  174.             preparedStmt.executeUpdate();
  175.                    
  176.             if(!connection.isClosed())
  177.             {
  178.                 System.out.println("Successfully connected to " + "MySQL server using TCP/IP...<br /><br />");
  179.                 return true;
  180.             }
  181.        
  182.             connection.close();
  183.         }
  184.         catch(SQLException e)
  185.         {
  186.             System.out.println("Unable to connect to database.");
  187.             System.out.println("<br />" + e.getMessage());
  188.             System.out.println("<br />" + e.getErrorCode());
  189.             return false;
  190.         }
  191.         return false;
  192.     }
  193. }
Add Comment
Please, Sign In to add comment