Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static java.lang.System.exit;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- ;
- /**
- *
- * @author jpplp
- */
- public class DbBD {
- /**
- * @param args the command line arguments
- */
- static final String user = "postgres";
- static final String senha = "postgres";
- static final String url = "jdbc:postgresql://localhost:5432/postgres";
- static Statement s=null;
- static Connection c= null;
- static ResultSet rs=null;
- public static void main(String[] args) throws SQLException {
- conexao();
- alterarAlbum("L1","Leandro");
- encerraConexao();
- }
- private static void conexao(){
- try{
- Class.forName("org.postgresql.Driver");
- c = DriverManager.getConnection(url, user, senha);
- s = c.createStatement();
- }catch(Exception ex){
- System.out.println(ex);
- }
- }
- private static void encerraConexao(){
- try{
- s.close();
- c.close();
- }catch(Exception ex){
- System.out.println(ex);
- }
- }
- static private ResultSet executeQuery(String query) throws SQLException {
- rs = s.executeQuery(query);
- return rs;
- }
- static public void menuInicial() {
- System.out.println("BEM VINDO AO DROP MUSIC\n\n");
- System.out.println(" 1. Registo");
- System.out.println(" 2. Login");
- System.out.println(" 0. Exit");
- }
- static public void menu() throws SQLException{
- String opcao = "1";
- Scanner input = new Scanner(System.in);
- String ok;
- while(!"0".equals(opcao)) {
- menuInicial();
- opcao=input.nextLine();
- switch(opcao) {
- case "1":
- try {
- registaUser();
- } catch (SQLException ex) {
- Logger.getLogger(DbBD.class.getName()).log(Level.SEVERE, null, ex);
- }
- System.out.println("Enter pra menu principal");
- ok =input.nextLine();
- break;
- case "2":
- String user = login();
- if(saberTipo(user).equals("n")){
- menuUtilizador();
- }else
- menuEditor();
- break;
- // run(user);
- //break;
- case "0":
- exit(0);
- }
- }
- }
- private static void registaUser() throws SQLException{
- int check = 1;
- String user=null,password=null;
- Scanner sc = new Scanner(System.in);
- System.out.println("Registo\n\n");
- do{
- System.out.print("USER: ");
- user=sc.nextLine();
- ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM pessoa WHERE username='"+user+"';");
- rs.next();
- check=rs.getInt("contagem");
- }while(check==1);
- do{
- System.out.print("Password: ");
- password=sc.nextLine();
- }while(password.equals(""));
- try {
- ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM pessoa;");
- rs.next();
- check=rs.getInt("contagem");
- if(check==0){
- s.executeQuery("INSERT INTO pessoa(username,password,type) values('"+user+"','"+password+"','e')");
- }else
- s.executeQuery("INSERT INTO pessoa(username,password,type) values('"+user+"','"+password+"','n')");
- } catch (Exception ex) {
- System.out.println();
- }
- }
- private static String login() throws SQLException{
- int check = 0,i=0;
- String user=null,password=null;
- Scanner sc = new Scanner(System.in);
- System.out.println("Login\n\n");
- while(check!=1){
- i++;
- if(i==4){
- System.out.println("Esgotou as suas tentativas\nClique Enter");
- String m=sc.nextLine();
- menu();
- }
- System.out.print("USER: ");
- user=sc.nextLine();
- System.out.print("Password: ");
- password=sc.nextLine();
- ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM pessoa WHERE username='"+user+"' and password='"+password+"';");
- rs.next();
- check=rs.getInt("contagem");
- if(check!=1)
- System.out.println("\nUm dos campos não esta correto\nTente novamente\n");
- }
- return user;
- }
- //saber o tipo do editor
- private static String saberTipo(String user) throws SQLException{
- ResultSet query = executeQuery("SELECT type"+"\"tipo\""+" FROM pessoa WHERE username='"+user+"';");
- rs.next();
- String tipo=rs.getString("tipo");
- return tipo;
- }
- //menu do utilizador, estatuto=normal
- static public void menuUtilizador(){
- System.out.println("| MENU DE UTILIZADOR |\n");
- System.out.println(" 1. Pesquisa.");
- System.out.println(" 2. Playlists.");
- System.out.println(" 0. Exit.");
- System.out.println(" Opcao: ");
- }
- //menu de um utilizador, estatuto=editor
- private static void menuEditor() {
- System.out.println("| MENU DE EDITOR |\n");
- System.out.println(" 1. Pesquisa.");
- System.out.println(" 2. Playlists.");
- System.out.println("INSERÇÃO:\n");
- System.out.println(" 1. Inserir Artista");
- System.out.println(" 2. Inserir Album");
- System.out.println(" 3. Inserir Musica");
- System.out.println("\n\n 0. Exit.");
- System.out.print("\n\n\n Opcao: ");
- }
- private static String inserirArtista() throws SQLException{
- int check = 0;
- //id,nome, historia, type
- String nome=null;
- String type = null;
- String historia=null;
- Scanner sc = new Scanner(System.in);
- System.out.println("Inserir Artista\n\n");
- while(check!=1){
- System.out.print("Nome: ");
- nome=sc.nextLine();
- if(!nome.equals("")){
- check=1;
- }else
- System.out.println("O nome do artista não pode ser null");
- }
- check=0;
- while(check!=1){
- System.out.print("Historia: ");
- historia=sc.nextLine();
- if(!historia.equals("")){
- check=1;
- }else
- System.out.println("A historia do artista nao pode ser null");
- }
- check=0;
- while(check!=1){
- System.out.print("Type: ");
- type=sc.nextLine();
- if(type.equals("Banda") || type.equals("Solo")){
- check=1;
- }else
- System.out.println("O Artista so pode ser uma Banda ou um Solo");
- }
- check=0;
- ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM artista WHERE nome='"+nome+"';");
- rs.next();
- check=rs.getInt("contagem");
- if(check==1){
- System.out.println("Esse artista já existe");
- String ok= sc.nextLine();
- return null;
- }
- query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM artista;");
- rs.next();
- check=rs.getInt("contagem");
- try{
- if(check==0){
- int id=1;
- s.executeQuery("INSERT INTO artista(id,nome,historia,type) values("+id+",'"+nome+"','"+historia+"','"+type+"');");
- return nome;
- }
- query = executeQuery("SELECT max(id)"+"\"id\""+" FROM artista;");
- rs.next();
- int id=rs.getInt("id");
- id=id+1;
- s.executeQuery("INSERT INTO artista(id,nome,historia,type) values("+id+",'"+nome+"','"+historia+"','"+type+"');");
- return nome;
- } catch (Exception ex) {
- System.out.println();
- }
- return nome;
- }
- private static String inserirAlbum() throws SQLException{
- int check = 0;
- //id,nome,lancamento(date),genero, editora, artista_id
- String nome=null;
- String genero = null;
- String editora=null;
- String artista=null;
- int ano = 0, mes = 0, dia = 0,artista_id = 0;
- Scanner sc = new Scanner(System.in);
- System.out.println("Inserir Album\n\n");
- while(check!=1){
- System.out.print("Nome: ");
- nome=sc.nextLine();
- if(!nome.equals("")){
- check=1;
- }else
- System.out.println("O nome do album não pode ser null");
- }
- check=0;
- while(check!=1){
- System.out.print("Genero: ");
- genero=sc.nextLine();
- if(!genero.equals("")){
- check=1;
- }else
- System.out.println("O genero do album não pode ser null");
- }
- check=0;
- int teste=0;
- while(teste!=1){
- System.out.print("Editora: ");
- editora=sc.nextLine();
- if(editora.equals("")){
- System.out.println("O nome da editora nao pode ser null");
- }else{
- ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM editora WHERE nome='"+editora+"';");
- rs.next();
- int contagem=rs.getInt("contagem");
- if(contagem==0){
- System.out.println("Essa editora nao existe");
- teste=inserirEditora(editora);
- }else{
- teste=1;
- }
- }
- }
- check=0;
- while(check!=1){
- System.out.print("Artista: ");
- artista=sc.nextLine();
- if(artista.equals("")){
- System.out.println("O nome do artista nao pode ser null");
- }else{
- ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM artista WHERE nome='"+artista+"';");
- rs.next();
- int contagem=rs.getInt("contagem");
- if(contagem==0){
- System.out.println("Esse artista nao existe");
- String ok= sc.nextLine();
- }else{
- query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+artista+"';");
- rs.next();
- artista_id=rs.getInt("id");
- check=1;
- }
- }
- }
- check=0;
- while(check!=1){
- System.out.print("Ano: ");
- ano=sc.nextInt();
- if(ano<=2018 && ano>0){
- check=1;
- }else
- System.out.println("O ano que colocou não é possivel");
- }
- check=0;
- while(check!=1){
- System.out.print("Mes: ");
- mes=sc.nextInt();
- if(mes<=12 && mes>0){
- check=1;
- }else
- System.out.println("O mes que colocou não é possivel");
- }
- check=0;
- while(check!=1){
- System.out.print("Dia: ");
- dia=sc.nextInt();
- if(dia<=31 && dia>0){
- check=1;
- }else
- System.out.println("O dia que colocou não é possivel");
- }
- check=0;
- ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM artista a,album b WHERE a.id=b.artista_id and b.nome='"+nome+"' and a.nome='"+artista+"';");
- rs.next();
- check=rs.getInt("contagem");
- if(check==1){
- System.out.println("Esse artista ja tem esse album");
- String ok= sc.nextLine();
- return null;
- }
- try{
- query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM album;");
- rs.next();
- check=rs.getInt("contagem");
- if(check==0){
- int id=1;
- //id,nome,lancamento(date),genero, editora, artista_id '2008-11-11'
- s.executeQuery("INSERT INTO album(id,nome,lancamento,genero,editora,artista_id) values("+id+",'"+nome+"','"+ano+"-"+mes+"-"+dia+"','"+genero+"','"+editora+"',"+artista_id+");");
- return nome;
- }
- query = executeQuery("SELECT max(id)"+"\"id\""+" FROM album;");
- rs.next();
- int id=rs.getInt("id");
- id=id+1;
- s.executeQuery("INSERT INTO album(id,nome,lancamento,genero,editora,artista_id) values("+id+",'"+nome+"','"+ano+"-"+mes+"-"+dia+"','"+genero+"','"+editora+"',"+artista_id+");");
- return nome;
- } catch (Exception ex) {
- System.out.println();
- }
- return nome;
- }
- private static String inserirMusica() throws SQLException{
- int check = 0;
- //id,nome,genero,tempo, ficheiro, letra, editora,album_id,artista_id
- String ok=null;
- String nome=null;
- String genero = null;
- String editora=null;
- String artista=null;
- String ficheiro = null;
- String letra=null;
- int duracao = 0,artista_id = 0;
- Scanner sc = new Scanner(System.in);
- System.out.println("Inserir Musica\n\n");
- while(check!=1){
- System.out.print("Nome: ");
- nome=sc.nextLine();
- if(!nome.equals("")){
- check=1;
- }else
- System.out.println("O nome da musica não pode ser null");
- }
- check=0;
- while(check!=1){
- System.out.print("Genero: ");
- genero=sc.nextLine();
- if(!genero.equals("")){
- check=1;
- }else
- System.out.println("O genero da musica não pode ser null");
- }
- check=0;
- while(check!=1){
- System.out.print("Letra: ");
- letra=sc.nextLine();
- if(!letra.equals("")){
- check=1;
- }else
- System.out.println("A letra da musica não pode ser null");
- }
- check=0;
- while(check!=1){
- System.out.print("Tipo de Ficheiro: ");
- ficheiro=sc.nextLine();
- if(!ficheiro.equals("")){
- check=1;
- }else
- System.out.println("O tipo de ficheiro da musica não pode ser null");
- }
- check=0;
- while(check!=1){
- System.out.print("Editora: ");
- editora=sc.nextLine();
- if(editora.equals("")){
- System.out.println("O nome da editora nao pode ser null");
- }else{
- ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM editora WHERE nome='"+editora+"';");
- rs.next();
- int contagem=rs.getInt("contagem");
- if(contagem==0){
- System.out.println("Essa editora nao existe");
- check=inserirEditora(editora);
- }else{
- check=1;
- }
- }
- }
- check=0;
- while(check!=1){
- System.out.print("Artista: ");
- artista=sc.nextLine();
- if(artista.equals("")){
- System.out.println("O nome do artista nao pode ser null");
- }else{
- ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM artista WHERE nome='"+artista+"';");
- rs.next();
- int contagem=rs.getInt("contagem");
- if(contagem==0){
- System.out.println("Esse artista nao existe");
- ok= sc.nextLine();
- }else{
- query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+artista+"';");
- rs.next();
- artista_id=rs.getInt("id");
- check=1;
- }
- }
- }
- check=0;
- while(check!=1){
- System.out.print("Duraçao: ");
- duracao=sc.nextInt();
- if(duracao!=0 && duracao>0){
- check=1;
- }else
- System.out.println("O ano que colocou não é possivel");
- }
- check=0;
- ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM artista a,musica m WHERE a.id=m.artista_id and m.nome='"+nome+"' and a.nome='"+artista+"';");
- rs.next();
- check=rs.getInt("contagem");
- if(check==1){
- System.out.println("Esse artista ja tem essa musica");
- ok= sc.nextLine();
- return null;
- }
- int album_id=colocaAlbum(nome,genero,editora,artista_id);
- try{
- query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM musica;");
- rs.next();
- check=rs.getInt("contagem");
- if(check==0){
- int id=1;
- s.executeQuery("INSERT INTO musica(id,nome,genero,tempo,ficheiro,letra,editora_nome,album_id,artista_id) values("+id+",'"+nome+"','"+genero+"',"+duracao+",'"+ficheiro+"','"+letra+"','"+editora+"',"+album_id+","+artista_id+");");
- return nome;
- }
- query = executeQuery("SELECT max(id)"+"\"id\""+" FROM album;");
- rs.next();
- int id=rs.getInt("id");
- id=id+1;
- s.executeQuery("INSERT INTO musica(id,nome,genero,tempo,ficheiro,letra,editora_nome,album_id,artista_id) values("+id+",'"+nome+"','"+genero+"',"+duracao+",'"+ficheiro+"','"+letra+"','"+editora+"',"+album_id+","+artista_id+");");
- return nome;
- } catch (Exception ex) {
- System.out.println();
- }
- return nome;
- }
- static public int inserirEditora(String editora) throws SQLException{
- int teste=0;
- int check = 0;
- Scanner input = new Scanner(System.in);
- while(teste!=1){
- System.out.println("Quer adicionar esta editora a base de dados? (Sim/Nao)");
- String ok= input.nextLine();
- if(ok.equals("Sim")){
- try{
- teste=1;
- check=1;
- s.executeQuery("INSERT INTO editora(nome) values('"+editora+"');");
- }catch (Exception ex) {
- System.out.println();
- }
- }else if(!ok.equals("Nao")){
- System.out.println("Essa opcao nao e valida");
- }else{
- check=0;
- teste=1;
- }
- }
- return check;
- }
- static public int colocaAlbum(String nome,String genero,String editora, int artista_id){
- int id=0;
- try{
- int dia=1,mes=1,ano=2018;
- ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM album;");
- rs.next();
- int check=rs.getInt("contagem");
- if(check==0){
- id=1;
- //id,nome,lancamento(date),genero, editora, artista_id '2008-11-11'
- s.executeQuery("INSERT INTO album(id,nome,lancamento,genero,editora,artista_id) values("+id+",'"+nome+"','"+ano+"-"+mes+"-"+dia+"','"+genero+"','"+editora+"',"+artista_id+");");
- return id;
- }
- query = executeQuery("SELECT max(id)"+"\"id\""+" FROM album;");
- rs.next();
- id=rs.getInt("id");
- id=id+1;
- s.executeQuery("INSERT INTO album(id,nome,lancamento,genero,editora,artista_id) values("+id+",'"+nome+"','"+ano+"-"+mes+"-"+dia+"','"+genero+"','"+editora+"',"+artista_id+");");
- } catch (Exception ex) {
- System.out.println();
- }
- return id;
- }
- private static void removerArtista(String nome) throws SQLException {
- System.out.println("Remover Artista\n\n");
- ResultSet query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+nome+"';");
- rs.next();
- int artista_id=rs.getInt("id");
- try{
- s.executeQuery("DELETE from musica where artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }try{
- s.executeQuery("DELETE from album where artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }try{
- s.executeQuery("DELETE from artista where nome='"+nome+"';");
- } catch (Exception ex) {
- System.out.println();
- }
- }
- //Remover um album de um artista
- private static void removerAlbum(String nome, String artista) throws SQLException {
- System.out.println("Remover Album\n\n");
- ResultSet query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+artista+"';");
- rs.next();
- int artista_id=rs.getInt("id");
- query = executeQuery("SELECT id"+"\"id\""+" FROM album WHERE nome='"+nome+"' and artista_id="+artista_id+";");
- rs.next();
- int album_id=rs.getInt("id");
- try{
- s.executeQuery("DELETE from musica where album_id="+album_id+";");
- } catch (Exception ex) {
- System.out.println();
- }try{
- s.executeQuery("DELETE from album where nome='"+nome+"' and artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }
- }
- private static void removerMusica(String nome, String artista) throws SQLException {
- System.out.println("Remover Musica\n\n");
- ResultSet query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+artista+"';");
- rs.next();
- int artista_id=rs.getInt("id");
- try{
- s.executeQuery("DELETE from musica where nome='"+nome+"' and artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }try{
- s.executeQuery("DELETE from album where nome='"+nome+"' and artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }
- }
- private static void alterarArtista(String nome) throws SQLException{
- Scanner sc = new Scanner(System.in);
- Scanner ss= new Scanner(System.in);
- int check=0;
- String type=null;
- String local=null;
- ResultSet query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+nome+"';");
- rs.next();
- int artista_id=rs.getInt("id");
- while(check!=1){
- System.out.println("O que prentede alterar?\n1-Historia\n2-Type\n3-Adicionar Concerto");
- System.out.print("Opcao: ");
- int opcao= sc.nextInt();
- switch(opcao){
- case 1:
- System.out.println("Nova historia:");
- String historia = ss.nextLine();
- try{
- s.executeQuery("UPDATE artista set historia='"+historia+"' where id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }
- check=1;
- break;
- case 2:
- int teste=0;
- while(teste!=1){
- System.out.println("Novo type:");
- type = ss.nextLine();
- if(type.equals("Banda") || type.equals("Solo")){
- teste=1;
- }else
- System.out.println("O Artista so pode ser uma Banda ou um Solo");
- }
- try{
- s.executeQuery("UPDATE artista set type='"+type+"' where id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }
- check=1;
- break;
- case 3:
- System.out.println("Concerto");
- teste=0;
- while(teste!=1){
- System.out.print("Local: ");
- local=ss.nextLine();
- if(local.equals("")){
- System.out.println("O local nao pode ser null");
- }else teste=1;
- }
- query = executeQuery("SELECT max(id)"+"\"id\""+" FROM concerto;");
- rs.next();
- int id=rs.getInt("id");
- try{
- if(id==0){
- id=1;
- s.executeQuery("INSERT INTO concerto(id,local_concerto,artista_id) values("+id+",'"+local+"',"+artista_id+");");
- }else{
- id=id+1;
- s.executeQuery("INSERT INTO concerto(id,local_concerto,artista_id) values("+id+",'"+local+"',"+artista_id+");");
- }
- } catch (Exception ex) {
- System.out.println();
- }
- check=1;
- break;
- default :
- System.out.println("Essa opcao nao e valida");
- String ok= ss.nextLine();
- break;
- }
- }
- }
- private static void alterarMusica(String nome,String artista) throws SQLException{
- Scanner sc = new Scanner(System.in);
- Scanner ss= new Scanner(System.in);
- int check=0;
- String letra=null;
- String ficheiro=null;
- String editora = null;
- String genero= null;
- ResultSet query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+artista+"';");
- rs.next();
- int teste=0;
- int artista_id=rs.getInt("id");
- while(check!=1){
- System.out.println("O que prentede alterar?\n1-Letra\n2-Duracao\n3-Ficheiro\n4-Editora\n5-Genero");
- System.out.print("Opcao: ");
- int opcao= sc.nextInt();
- switch(opcao){
- case 1:
- teste=0;
- while(teste!=1){
- System.out.print("Nova Letra: ");
- letra=ss.nextLine();
- if(letra.equals("")){
- System.out.println("A letra nao pode ser null");
- }else teste=1;
- }
- try{
- s.executeQuery("UPDATE musica set letra='"+letra+"' where nome='"+nome+"'and artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }
- check=1;
- break;
- case 2:
- int duracao=0;
- teste=0;
- while(teste!=1){
- System.out.println("Nova Duracao:");
- duracao = sc.nextInt();
- if(duracao!=0 && duracao >0) teste=1;
- else{
- System.out.println("Esse valor nao e possivel");
- String ok=ss.nextLine();
- }
- }
- try{
- s.executeQuery("UPDATE musica set tempo='"+duracao+"' where nome='"+nome+"' and artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }
- check=1;
- break;
- case 3:
- teste=0;
- while(teste!=1){
- System.out.print("Novo ficheiro: ");
- ficheiro=ss.nextLine();
- if(ficheiro.equals("")){
- System.out.println("O ficheiro nao pode ser null");
- }else teste=1;
- }
- try{
- s.executeQuery("UPDATE musica set ficheiro='"+ficheiro+"' where nome='"+nome+"' and artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }
- check=1;
- break;
- case 4:
- teste=0;
- while(teste!=1){
- System.out.print("Nova Editora: ");
- editora=ss.nextLine();
- if(editora.equals("")){
- System.out.println("O nome da editora nao pode ser null");
- }else{
- query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM editora WHERE nome='"+editora+"';");
- rs.next();
- int contagem=rs.getInt("contagem");
- if(contagem==0){
- System.out.println("Essa editora nao existe");
- teste=inserirEditora(editora);
- }else{
- teste=1;
- }
- }
- }
- try{
- s.executeQuery("UPDATE musica set editora_nome='"+editora+"' where nome='"+nome+"' and artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }
- check=1;
- break;
- case 5:
- teste=0;
- while(teste!=1){
- System.out.print("Novo genero: ");
- genero=ss.nextLine();
- if(genero.equals("")){
- System.out.println("O genero nao pode ser null");
- }else teste=1;
- }
- try{
- s.executeQuery("UPDATE musica set genero='"+genero+"' where nome='"+nome+"' and artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }
- check=1;
- break;
- default :
- System.out.println("Essa opcao nao e valida");
- String ok= ss.nextLine();
- break;
- }
- }
- }
- private static void alterarAlbum(String nome,String artista) throws SQLException{
- Scanner sc = new Scanner(System.in);
- Scanner ss= new Scanner(System.in);
- int check=0;
- String genero=null;
- String editora = null;
- String escolha=null;
- int dia=0,mes=0,ano = 0;
- ResultSet query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+artista+"';");
- rs.next();
- int teste=0;
- int artista_id=rs.getInt("id");
- query = executeQuery("SELECT id"+"\"id\""+" FROM album WHERE nome='"+nome+"';");
- rs.next();
- int album_id=rs.getInt("id");
- while(check!=1){
- System.out.println("O que prentede alterar?\n1-Editora\n2-Genero\n3-Lancamento\n4-Inserir Musica\n5-Remover Musica");
- System.out.print("Opcao: ");
- int opcao= sc.nextInt();
- switch(opcao){
- case 1:
- teste=0;
- while(teste!=1){
- System.out.print("Nova editora: ");
- editora=ss.nextLine();
- if(editora.equals("")){
- System.out.println("O nome da editora nao pode ser null");
- }else{
- query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM editora WHERE nome='"+editora+"';");
- rs.next();
- int contagem=rs.getInt("contagem");
- if(contagem==0){
- System.out.println("Essa editora nao existe");
- teste=inserirEditora(editora);
- }else{
- teste=1;
- }
- }
- }
- try{
- s.executeQuery("UPDATE album set editora='"+editora+"' where nome='"+nome+"' and artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }
- check=1;
- break;
- case 2:
- teste=0;
- while(teste!=1){
- System.out.print("Novo genero: ");
- genero=ss.nextLine();
- if(genero.equals("")){
- System.out.println("O genero nao pode ser null");
- }else teste=1;
- }
- try{
- s.executeQuery("UPDATE album set genero='"+genero+"' where nome='"+nome+"' and artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }
- check=1;
- break;
- case 3:
- teste=0;
- while(teste!=1){
- System.out.print("Ano: ");
- ano=sc.nextInt();
- if(ano<=2018 && ano>0){
- teste=1;
- }else
- System.out.println("O ano que colocou não é possivel");
- }
- teste=0;
- while(teste!=1){
- System.out.print("Mes: ");
- mes=sc.nextInt();
- if(mes<=12 && mes>0){
- teste=1;
- }else
- System.out.println("O mes que colocou não é possivel");
- }
- teste=0;
- while(teste!=1){
- System.out.print("Dia: ");
- dia=sc.nextInt();
- if(dia<=31 && dia>0){
- teste=1;
- }else
- System.out.println("O dia que colocou não é possivel");
- }
- try{
- s.executeQuery("UPDATE album set lancamento='"+ano+"-"+mes+"-"+dia+"' where nome='"+nome+"' and artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println();
- }
- check=1;
- break;
- case 4:
- query = executeQuery("SELECT count (DISTINCT nome)"+"\"contagem\""+" FROM musica WHERE artista_id="+artista_id+";");
- rs.next();
- int musicas=rs.getInt("contagem");
- teste=0;
- if(musicas==0){
- System.out.println("Este Artista nao tem musicas");
- String ok=ss.nextLine();
- check=1;
- break;
- }
- query = executeQuery("SELECT DISTINCT (nome)"+"\"musica\""+" FROM musica WHERE artista_id="+artista_id+";");
- System.out.println("Musicas do Artista:");
- while(teste!=1){
- for(int i=0;i<musicas;i++){
- rs.next();
- String musica = rs.getString("musica");
- System.out.println("---> "+musica);
- }
- System.out.println("Qual a musica que quer adicionar ao album?");
- escolha=ss.nextLine();
- query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM musica WHERE nome='"+escolha+"' and artista_id="+artista_id+";");
- rs.next();
- musicas=rs.getInt("contagem");
- if(musicas==0){
- System.out.println("A sua escolha nao foi aceite tente novamente");
- String ok= ss.nextLine();
- }else{
- teste=1;
- }
- }
- query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM musica WHERE nome='"+escolha+"' and album_id="+album_id+" and artista_id="+artista_id+";");
- rs.next();
- int repeticao=rs.getInt("contagem");
- if(repeticao!=0){
- System.out.println("Essa musica ja existe no album");
- check=1;
- break;
- }
- query = executeQuery("SELECT genero"+"\"genero\",letra"+"\"letra\",tempo"+"\"tempo\",ficheiro"+"\"ficheiro\",editora_nome"+"\"editora\" FROM musica WHERE nome='"+escolha+"' and artista_id="+artista_id+";");
- rs.next();
- genero=rs.getString("genero");
- String letra= rs.getString("letra");
- int duracao = rs.getInt("tempo");
- String ficheiro = rs.getString("ficheiro");
- editora= rs.getString("editora");
- query = executeQuery("SELECT max(id)"+"\"id\""+" FROM musica;");
- rs.next();
- int id=rs.getInt("id");
- id=id+1;
- try{
- s.executeQuery("INSERT INTO musica(id,nome,genero,tempo,ficheiro,letra,editora_nome,album_id,artista_id) values("+id+",'"+escolha+"','"+genero+"',"+duracao+",'"+ficheiro+"','"+letra+"','"+editora+"',"+album_id+","+artista_id+");");
- } catch (Exception ex) {
- System.out.println();
- }
- check=1;
- break;
- case 5:
- query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM musica WHERE album_id="+album_id+" and artista_id="+artista_id+";");
- rs.next();
- musicas=rs.getInt("contagem");
- teste=0;
- if(musicas==0){
- System.out.println("Este Album nao tem musicas");
- check=1;
- break;
- }
- query = executeQuery("SELECT nome"+"\"musica\""+" FROM musica WHERE album_id="+album_id+" and artista_id="+artista_id+";");
- System.out.println("Musicas do Album:");
- while(teste!=1){
- for(int i=0;i<musicas;i++){
- rs.next();
- String musica = rs.getString("musica");
- System.out.println("---> "+musica);
- }
- System.out.println("Qual a musica que quer remover do album?");
- escolha=ss.nextLine();
- query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM musica WHERE nome='"+escolha+"' and album_id="+album_id+";");
- rs.next();
- musicas=rs.getInt("contagem");
- if(musicas==0){
- System.out.println("A sua escolha nao foi aceite tente novamente");
- String ok= ss.nextLine();
- }else{
- teste=1;
- }
- }
- try{
- s.executeQuery("DELETE from musica where nome='"+escolha+"' and album_id="+album_id+" and artista_id="+artista_id+";");
- } catch (Exception ex) {
- System.out.println(ex);
- }
- check=1;
- break;
- default :
- System.out.println("Essa opcao nao e valida");
- String ok= ss.nextLine();
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement