Guest User

create_auction

a guest
Dec 1st, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. //CRIAR LEIL�O
  2. public String create_auction(long code, String title, String description, String Date, int amount, String username) throws RemoteException {
  3. //type: login, username: imota, password: cenasmalucas3
  4. //type: create_auction, code: 9780451524935, title: 1984, description: big brother i s watching you, deadline: 2017-01-01 00:01, amount: 10
  5.  
  6. int check = 0;
  7. String resultado;
  8. //-------------------------------------
  9. //Verificar se o registo pode ser feito;
  10. //-------------------------------------
  11.  
  12. Statement myStmt;
  13. try {
  14.  
  15. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  16. Date date2 = format.parse(getcurrentdate());
  17. Date date = format.parse(Date);
  18. if (date.compareTo(date2) < 0) {
  19. return "type: create_auction, ok: false";
  20. }
  21.  
  22. myStmt = myConn.createStatement();
  23.  
  24. ResultSet myRs = myStmt.executeQuery("SELECT ID_USERS\n"
  25. + "FROM users\n"
  26. + "WHERE USERNAME ='" + username + "'");//results set
  27.  
  28. if (myRs.next()) {
  29. int id_user = myRs.getInt("ID_USERS");
  30.  
  31. String commandSql = "INSERT INTO auction (CODE, TITLE, DESCRIPTION, DEADLINE, ID_USER, AUCTION_STATE)\n"
  32. + "values (" + code + ", '" + title + "', '" + description + "', '" + Date + "', " + id_user + ", 'active')";
  33. myStmt.executeUpdate(commandSql); //results set
  34.  
  35.  
  36. //para a bid, preciso de saber o ID_AUCTION
  37. ResultSet myRs2 = myStmt.executeQuery("SELECT ID_AUCTION "
  38. + "FROM auction "
  39. + "WHERE (CODE =" + code + " AND TITLE='" + title + "' AND DESCRIPTION='" + description + "' AND DEADLINE='" + Date + "' AND ID_USER=" + id_user + " AND AUCTION_STATE='active')");
  40. //myRs2.next();
  41. myRs2.last();
  42.  
  43. //adiciona ao array das deadlines a data final da auction
  44. client.writetodeadlinearray(myRs2.getInt("ID_AUCTION"),Date);
  45.  
  46.  
  47. //acrescento a tabela de bids
  48. String commandSql2 = "INSERT INTO bids (AMOUNT, ID_AUCTION, ID_USER, BID_STATE)\n"
  49. + "values (" + amount + ", " + myRs2.getInt("ID_AUCTION") + ", " + id_user + ", 'active')";
  50. myStmt.executeUpdate(commandSql2);
  51. check = 1;
  52. }
  53.  
  54. } catch (SQLException e) {//tratar exception de limite de carateres
  55. check = 0;
  56. e.printStackTrace();
  57. } catch (java.text.ParseException ex) {
  58. Logger.getLogger(RMIServer.class.getName()).log(Level.SEVERE, null, ex);
  59. }
  60. //-----------------------------------
  61. //Verificar se o leil�o foi criado
  62. //-----------------------------------
  63. if (check == 1) {
  64. resultado = "type: create_auction, ok: true";
  65. } else {
  66. resultado = "type: create_auction, ok: false";
  67. }
  68. return resultado;
  69.  
  70. }
Add Comment
Please, Sign In to add comment