Advertisement
Guest User

Untitled

a guest
Dec 15th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.68 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.SQLException;
  7. import java.util.Iterator;
  8.  
  9. public class Main {
  10.  
  11.     private static final String cancelprojectobycode ="UPDATE auctions SET Active= ? WHERE Code = ?";
  12.     private static final String cancelprojectobyusername ="UPDATE auctions2 SET Active= ? WHERE Username = ?";
  13.     private static final String insereuser = "INSERT INTO users (username, password, isAdmin, nAuctions, nWins, isBanned) VALUES ('admin', 'admin', 0, NULL, NULL, 0)";
  14.     private static final String insereauction = "INSERT INTO `auctions` (`id`,`title`, `description`, `code`,`username`,`vencedor`,`deadline`,`active`,`creation`,`amount`) VALUES (1, 'swag' , 'cap' , '1994', 'admin', '', NULL, 1, NULL, 100)";
  15.     private static final String s = "";
  16.     private static final String removeUserBids = "SELECT execute(? );";
  17.  
  18.  
  19.     static final String DB_URL = "jdbc:mysql://localhost/world";
  20.     private static Connection conn = null;
  21.     //  Database credentials
  22.     static final String USER = "admin";
  23.     static final String PASS = "admin";
  24.     public static void main(String[] args) {
  25.     // write your code here
  26.        //conectar à base de dados
  27.         startCon();
  28.         //insereUser();
  29.         //insereAuction();
  30.         cancelAuctionByCode("1994");
  31.         cancelUserBids("admin");
  32.  
  33.     }
  34.  
  35.     public boolean deleteBids(String username){
  36.         {
  37.             PreparedStatement pStatement=null;
  38.             boolean aux = true;
  39.             try {
  40.                 String query = "DELETE FROM auctions2 WHERE username=?";
  41.                 pStatement = conn.prepareStatement(query);
  42.                 pStatement.setString(1,username);
  43.                 pStatement.executeUpdate();
  44.  
  45.             } catch (SQLException e) {
  46.                 aux = false;
  47.                 e.printStackTrace();
  48.             }
  49.             return aux;
  50.         }
  51.     }
  52.     public static boolean cancelUserBids(String username){
  53.         PreparedStatement pStatement;
  54.         boolean resp;
  55.         try {
  56.             pStatement = conn.prepareStatement(removeUserBids);
  57.             pStatement.setString(1, username);
  58.             pStatement.executeQuery();
  59.             resp = true;
  60.  
  61.         } catch (SQLException e) {
  62.             resp = false;
  63.             e.printStackTrace();
  64.         }
  65.         return resp;
  66.     }
  67.  
  68.     public static boolean cancelAuctionByUsername(String username){
  69.         PreparedStatement pStatement=null;
  70.         boolean aux = true;
  71.         try {
  72.             conn.setAutoCommit(false);
  73.             pStatement = conn.prepareStatement(cancelprojectobyusername);
  74.             pStatement.setInt(1,0);
  75.             pStatement.setString(2 ,username);
  76.             pStatement.executeUpdate();
  77.             conn.commit();
  78.         } catch (SQLException e) {
  79.             aux = false;
  80.             e.printStackTrace();
  81.         }
  82.         return aux;
  83.     }
  84.  
  85.     public static boolean insereAuction(){
  86.         PreparedStatement pStatement=null;
  87.         boolean aux = true;
  88.         try {
  89.             conn.setAutoCommit(false);
  90.             pStatement = conn.prepareStatement(insereauction);
  91.             pStatement.executeUpdate();
  92.             conn.commit();
  93.         } catch (SQLException e) {
  94.             aux = false;
  95.             e.printStackTrace();
  96.         }
  97.         return aux;
  98.     }
  99.  
  100.     public static boolean insereUser(){
  101.         PreparedStatement pStatement=null;
  102.         boolean aux = true;
  103.         try {
  104.             conn.setAutoCommit(false);
  105.             pStatement = conn.prepareStatement(insereuser);
  106.             pStatement.executeUpdate();
  107.             conn.commit();
  108.         } catch (SQLException e) {
  109.             aux = false;
  110.             e.printStackTrace();
  111.         }
  112.         return aux;
  113.     }
  114.  
  115.     public static boolean cancelAuctionByCode(String code){
  116.         PreparedStatement pStatement=null;
  117.         boolean aux = true;
  118.         try {
  119.             conn.setAutoCommit(false);
  120.             pStatement = conn.prepareStatement(cancelprojectobycode);
  121.             pStatement.setInt(1,0);
  122.             pStatement.setString(2,code);
  123.             pStatement.executeUpdate();
  124.             conn.commit();
  125.         } catch (SQLException e) {
  126.             aux = false;
  127.             e.printStackTrace();
  128.         }
  129.         return aux;
  130.     }
  131.  
  132.  
  133.     public static void startCon() {
  134.  
  135.         System.out.println("Connecting to database...");
  136.         try {
  137.             conn = DriverManager.getConnection(DB_URL, USER, PASS);
  138.             System.out.println("com sucesso");
  139.         } catch (SQLException e) {
  140.             System.err.println("falhou a coneccao: " + e);
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement