Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.util.Iterator;
- public class Main {
- private static final String cancelprojectobycode ="UPDATE auctions SET Active= ? WHERE Code = ?";
- private static final String cancelprojectobyusername ="UPDATE auctions2 SET Active= ? WHERE Username = ?";
- private static final String insereuser = "INSERT INTO users (username, password, isAdmin, nAuctions, nWins, isBanned) VALUES ('admin', 'admin', 0, NULL, NULL, 0)";
- 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)";
- private static final String s = "";
- private static final String removeUserBids = "SELECT execute(? );";
- static final String DB_URL = "jdbc:mysql://localhost/world";
- private static Connection conn = null;
- // Database credentials
- static final String USER = "admin";
- static final String PASS = "admin";
- public static void main(String[] args) {
- // write your code here
- //conectar à base de dados
- startCon();
- //insereUser();
- //insereAuction();
- cancelAuctionByCode("1994");
- cancelUserBids("admin");
- }
- public boolean deleteBids(String username){
- {
- PreparedStatement pStatement=null;
- boolean aux = true;
- try {
- String query = "DELETE FROM auctions2 WHERE username=?";
- pStatement = conn.prepareStatement(query);
- pStatement.setString(1,username);
- pStatement.executeUpdate();
- } catch (SQLException e) {
- aux = false;
- e.printStackTrace();
- }
- return aux;
- }
- }
- public static boolean cancelUserBids(String username){
- PreparedStatement pStatement;
- boolean resp;
- try {
- pStatement = conn.prepareStatement(removeUserBids);
- pStatement.setString(1, username);
- pStatement.executeQuery();
- resp = true;
- } catch (SQLException e) {
- resp = false;
- e.printStackTrace();
- }
- return resp;
- }
- public static boolean cancelAuctionByUsername(String username){
- PreparedStatement pStatement=null;
- boolean aux = true;
- try {
- conn.setAutoCommit(false);
- pStatement = conn.prepareStatement(cancelprojectobyusername);
- pStatement.setInt(1,0);
- pStatement.setString(2 ,username);
- pStatement.executeUpdate();
- conn.commit();
- } catch (SQLException e) {
- aux = false;
- e.printStackTrace();
- }
- return aux;
- }
- public static boolean insereAuction(){
- PreparedStatement pStatement=null;
- boolean aux = true;
- try {
- conn.setAutoCommit(false);
- pStatement = conn.prepareStatement(insereauction);
- pStatement.executeUpdate();
- conn.commit();
- } catch (SQLException e) {
- aux = false;
- e.printStackTrace();
- }
- return aux;
- }
- public static boolean insereUser(){
- PreparedStatement pStatement=null;
- boolean aux = true;
- try {
- conn.setAutoCommit(false);
- pStatement = conn.prepareStatement(insereuser);
- pStatement.executeUpdate();
- conn.commit();
- } catch (SQLException e) {
- aux = false;
- e.printStackTrace();
- }
- return aux;
- }
- public static boolean cancelAuctionByCode(String code){
- PreparedStatement pStatement=null;
- boolean aux = true;
- try {
- conn.setAutoCommit(false);
- pStatement = conn.prepareStatement(cancelprojectobycode);
- pStatement.setInt(1,0);
- pStatement.setString(2,code);
- pStatement.executeUpdate();
- conn.commit();
- } catch (SQLException e) {
- aux = false;
- e.printStackTrace();
- }
- return aux;
- }
- public static void startCon() {
- System.out.println("Connecting to database...");
- try {
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- System.out.println("com sucesso");
- } catch (SQLException e) {
- System.err.println("falhou a coneccao: " + e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement