Advertisement
Guest User

Untitled

a guest
Nov 24th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 39.55 KB | None | 0 0
  1. import static java.lang.System.exit;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;import java.util.Scanner;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. ;
  10.  
  11. /**
  12. *
  13. * @author jpplp
  14. */
  15. public class DbBD {
  16.  
  17.  
  18. /**
  19. * @param args the command line arguments
  20. */
  21. static final String user = "postgres";
  22. static final String senha = "postgres";
  23. static final String url = "jdbc:postgresql://localhost:5432/postgres";
  24. static Statement s=null;
  25. static Connection c= null;
  26. static ResultSet rs=null;
  27.  
  28.  
  29. public static void main(String[] args) throws SQLException {
  30. conexao();
  31. alterarAlbum("L1","Leandro");
  32. encerraConexao();
  33.  
  34.  
  35. }
  36.  
  37.  
  38. private static void conexao(){
  39. try{
  40. Class.forName("org.postgresql.Driver");
  41. c = DriverManager.getConnection(url, user, senha);
  42. s = c.createStatement();
  43. }catch(Exception ex){
  44. System.out.println(ex);
  45. }
  46. }
  47.  
  48. private static void encerraConexao(){
  49. try{
  50. s.close();
  51. c.close();
  52. }catch(Exception ex){
  53. System.out.println(ex);
  54. }
  55. }
  56.  
  57. static private ResultSet executeQuery(String query) throws SQLException {
  58. rs = s.executeQuery(query);
  59. return rs;
  60. }
  61.  
  62. static public void menuInicial() {
  63. System.out.println("BEM VINDO AO DROP MUSIC\n\n");
  64. System.out.println(" 1. Registo");
  65. System.out.println(" 2. Login");
  66. System.out.println(" 0. Exit");
  67.  
  68. }
  69.  
  70.  
  71. static public void menu() throws SQLException{
  72. String opcao = "1";
  73. Scanner input = new Scanner(System.in);
  74. String ok;
  75. while(!"0".equals(opcao)) {
  76. menuInicial();
  77. opcao=input.nextLine();
  78. switch(opcao) {
  79. case "1":
  80. try {
  81. registaUser();
  82. } catch (SQLException ex) {
  83. Logger.getLogger(DbBD.class.getName()).log(Level.SEVERE, null, ex);
  84. }
  85. System.out.println("Enter pra menu principal");
  86. ok =input.nextLine();
  87. break;
  88. case "2":
  89. String user = login();
  90. if(saberTipo(user).equals("n")){
  91. menuUtilizador();
  92. }else
  93. menuEditor();
  94. break;
  95. // run(user);
  96. //break;
  97. case "0":
  98. exit(0);
  99. }
  100. }
  101. }
  102.  
  103.  
  104. private static void registaUser() throws SQLException{
  105. int check = 1;
  106. String user=null,password=null;
  107. Scanner sc = new Scanner(System.in);
  108. System.out.println("Registo\n\n");
  109. do{
  110. System.out.print("USER: ");
  111. user=sc.nextLine();
  112. ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM pessoa WHERE username='"+user+"';");
  113. rs.next();
  114. check=rs.getInt("contagem");
  115.  
  116. }while(check==1);
  117.  
  118. do{
  119. System.out.print("Password: ");
  120. password=sc.nextLine();
  121. }while(password.equals(""));
  122. try {
  123. ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM pessoa;");
  124. rs.next();
  125. check=rs.getInt("contagem");
  126. if(check==0){
  127. s.executeQuery("INSERT INTO pessoa(username,password,type) values('"+user+"','"+password+"','e')");
  128. }else
  129. s.executeQuery("INSERT INTO pessoa(username,password,type) values('"+user+"','"+password+"','n')");
  130. } catch (Exception ex) {
  131. System.out.println();
  132. }
  133. }
  134.  
  135. private static String login() throws SQLException{
  136. int check = 0,i=0;
  137. String user=null,password=null;
  138. Scanner sc = new Scanner(System.in);
  139. System.out.println("Login\n\n");
  140.  
  141. while(check!=1){
  142. i++;
  143. if(i==4){
  144. System.out.println("Esgotou as suas tentativas\nClique Enter");
  145. String m=sc.nextLine();
  146. menu();
  147. }
  148. System.out.print("USER: ");
  149. user=sc.nextLine();
  150. System.out.print("Password: ");
  151. password=sc.nextLine();
  152. ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM pessoa WHERE username='"+user+"' and password='"+password+"';");
  153. rs.next();
  154. check=rs.getInt("contagem");
  155. if(check!=1)
  156. System.out.println("\nUm dos campos não esta correto\nTente novamente\n");
  157. }
  158. return user;
  159. }
  160.  
  161. //saber o tipo do editor
  162. private static String saberTipo(String user) throws SQLException{
  163. ResultSet query = executeQuery("SELECT type"+"\"tipo\""+" FROM pessoa WHERE username='"+user+"';");
  164. rs.next();
  165. String tipo=rs.getString("tipo");
  166. return tipo;
  167. }
  168.  
  169. //menu do utilizador, estatuto=normal
  170. static public void menuUtilizador(){
  171. System.out.println("| MENU DE UTILIZADOR |\n");
  172. System.out.println(" 1. Pesquisa.");
  173. System.out.println(" 2. Playlists.");
  174. System.out.println(" 0. Exit.");
  175. System.out.println(" Opcao: ");
  176. }
  177.  
  178. //menu de um utilizador, estatuto=editor
  179. private static void menuEditor() {
  180. System.out.println("| MENU DE EDITOR |\n");
  181. System.out.println(" 1. Pesquisa.");
  182. System.out.println(" 2. Playlists.");
  183. System.out.println("INSERÇÃO:\n");
  184. System.out.println(" 1. Inserir Artista");
  185. System.out.println(" 2. Inserir Album");
  186. System.out.println(" 3. Inserir Musica");
  187. System.out.println("\n\n 0. Exit.");
  188. System.out.print("\n\n\n Opcao: ");
  189. }
  190.  
  191. private static String inserirArtista() throws SQLException{
  192. int check = 0;
  193. //id,nome, historia, type
  194. String nome=null;
  195. String type = null;
  196. String historia=null;
  197. Scanner sc = new Scanner(System.in);
  198. System.out.println("Inserir Artista\n\n");
  199. while(check!=1){
  200. System.out.print("Nome: ");
  201. nome=sc.nextLine();
  202. if(!nome.equals("")){
  203. check=1;
  204. }else
  205. System.out.println("O nome do artista não pode ser null");
  206.  
  207. }
  208. check=0;
  209. while(check!=1){
  210. System.out.print("Historia: ");
  211. historia=sc.nextLine();
  212. if(!historia.equals("")){
  213. check=1;
  214. }else
  215. System.out.println("A historia do artista nao pode ser null");
  216. }
  217. check=0;
  218. while(check!=1){
  219. System.out.print("Type: ");
  220. type=sc.nextLine();
  221. if(type.equals("Banda") || type.equals("Solo")){
  222. check=1;
  223. }else
  224. System.out.println("O Artista so pode ser uma Banda ou um Solo");
  225. }
  226. check=0;
  227. ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM artista WHERE nome='"+nome+"';");
  228. rs.next();
  229. check=rs.getInt("contagem");
  230. if(check==1){
  231. System.out.println("Esse artista já existe");
  232. String ok= sc.nextLine();
  233. return null;
  234. }
  235. query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM artista;");
  236. rs.next();
  237. check=rs.getInt("contagem");
  238. try{
  239. if(check==0){
  240. int id=1;
  241. s.executeQuery("INSERT INTO artista(id,nome,historia,type) values("+id+",'"+nome+"','"+historia+"','"+type+"');");
  242. return nome;
  243. }
  244. query = executeQuery("SELECT max(id)"+"\"id\""+" FROM artista;");
  245. rs.next();
  246. int id=rs.getInt("id");
  247. id=id+1;
  248. s.executeQuery("INSERT INTO artista(id,nome,historia,type) values("+id+",'"+nome+"','"+historia+"','"+type+"');");
  249. return nome;
  250. } catch (Exception ex) {
  251. System.out.println();
  252. }
  253. return nome;
  254. }
  255.  
  256. private static String inserirAlbum() throws SQLException{
  257. int check = 0;
  258. //id,nome,lancamento(date),genero, editora, artista_id
  259. String nome=null;
  260. String genero = null;
  261. String editora=null;
  262. String artista=null;
  263. int ano = 0, mes = 0, dia = 0,artista_id = 0;
  264. Scanner sc = new Scanner(System.in);
  265. System.out.println("Inserir Album\n\n");
  266. while(check!=1){
  267. System.out.print("Nome: ");
  268. nome=sc.nextLine();
  269. if(!nome.equals("")){
  270. check=1;
  271. }else
  272. System.out.println("O nome do album não pode ser null");
  273.  
  274. }
  275. check=0;
  276. while(check!=1){
  277. System.out.print("Genero: ");
  278. genero=sc.nextLine();
  279. if(!genero.equals("")){
  280. check=1;
  281. }else
  282. System.out.println("O genero do album não pode ser null");
  283.  
  284. }
  285. check=0;
  286. int teste=0;
  287. while(teste!=1){
  288. System.out.print("Editora: ");
  289. editora=sc.nextLine();
  290. if(editora.equals("")){
  291. System.out.println("O nome da editora nao pode ser null");
  292. }else{
  293. ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM editora WHERE nome='"+editora+"';");
  294. rs.next();
  295. int contagem=rs.getInt("contagem");
  296. if(contagem==0){
  297. System.out.println("Essa editora nao existe");
  298. teste=inserirEditora(editora);
  299. }else{
  300. teste=1;
  301. }
  302. }
  303. }
  304. check=0;
  305. while(check!=1){
  306. System.out.print("Artista: ");
  307. artista=sc.nextLine();
  308. if(artista.equals("")){
  309. System.out.println("O nome do artista nao pode ser null");
  310. }else{
  311. ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM artista WHERE nome='"+artista+"';");
  312. rs.next();
  313. int contagem=rs.getInt("contagem");
  314. if(contagem==0){
  315. System.out.println("Esse artista nao existe");
  316. String ok= sc.nextLine();
  317. }else{
  318. query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+artista+"';");
  319. rs.next();
  320. artista_id=rs.getInt("id");
  321. check=1;
  322. }
  323. }
  324. }
  325. check=0;
  326. while(check!=1){
  327. System.out.print("Ano: ");
  328. ano=sc.nextInt();
  329. if(ano<=2018 && ano>0){
  330. check=1;
  331. }else
  332. System.out.println("O ano que colocou não é possivel");
  333. }
  334. check=0;
  335. while(check!=1){
  336. System.out.print("Mes: ");
  337. mes=sc.nextInt();
  338. if(mes<=12 && mes>0){
  339. check=1;
  340. }else
  341. System.out.println("O mes que colocou não é possivel");
  342. }
  343. check=0;
  344. while(check!=1){
  345. System.out.print("Dia: ");
  346. dia=sc.nextInt();
  347. if(dia<=31 && dia>0){
  348. check=1;
  349. }else
  350. System.out.println("O dia que colocou não é possivel");
  351. }
  352. check=0;
  353. 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+"';");
  354. rs.next();
  355. check=rs.getInt("contagem");
  356. if(check==1){
  357. System.out.println("Esse artista ja tem esse album");
  358. String ok= sc.nextLine();
  359. return null;
  360. }
  361. try{
  362. query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM album;");
  363. rs.next();
  364. check=rs.getInt("contagem");
  365. if(check==0){
  366. int id=1;
  367. //id,nome,lancamento(date),genero, editora, artista_id '2008-11-11'
  368. s.executeQuery("INSERT INTO album(id,nome,lancamento,genero,editora,artista_id) values("+id+",'"+nome+"','"+ano+"-"+mes+"-"+dia+"','"+genero+"','"+editora+"',"+artista_id+");");
  369. return nome;
  370. }
  371. query = executeQuery("SELECT max(id)"+"\"id\""+" FROM album;");
  372. rs.next();
  373. int id=rs.getInt("id");
  374. id=id+1;
  375. s.executeQuery("INSERT INTO album(id,nome,lancamento,genero,editora,artista_id) values("+id+",'"+nome+"','"+ano+"-"+mes+"-"+dia+"','"+genero+"','"+editora+"',"+artista_id+");");
  376. return nome;
  377. } catch (Exception ex) {
  378. System.out.println();
  379. }
  380. return nome;
  381. }
  382.  
  383.  
  384. private static String inserirMusica() throws SQLException{
  385. int check = 0;
  386. //id,nome,genero,tempo, ficheiro, letra, editora,album_id,artista_id
  387. String ok=null;
  388. String nome=null;
  389. String genero = null;
  390. String editora=null;
  391. String artista=null;
  392. String ficheiro = null;
  393. String letra=null;
  394. int duracao = 0,artista_id = 0;
  395. Scanner sc = new Scanner(System.in);
  396. System.out.println("Inserir Musica\n\n");
  397. while(check!=1){
  398. System.out.print("Nome: ");
  399. nome=sc.nextLine();
  400. if(!nome.equals("")){
  401. check=1;
  402. }else
  403. System.out.println("O nome da musica não pode ser null");
  404.  
  405. }
  406. check=0;
  407. while(check!=1){
  408. System.out.print("Genero: ");
  409. genero=sc.nextLine();
  410. if(!genero.equals("")){
  411. check=1;
  412. }else
  413. System.out.println("O genero da musica não pode ser null");
  414.  
  415. }
  416. check=0;
  417. while(check!=1){
  418. System.out.print("Letra: ");
  419. letra=sc.nextLine();
  420. if(!letra.equals("")){
  421. check=1;
  422. }else
  423. System.out.println("A letra da musica não pode ser null");
  424.  
  425. }
  426. check=0;
  427. while(check!=1){
  428. System.out.print("Tipo de Ficheiro: ");
  429. ficheiro=sc.nextLine();
  430. if(!ficheiro.equals("")){
  431. check=1;
  432. }else
  433. System.out.println("O tipo de ficheiro da musica não pode ser null");
  434.  
  435. }
  436. check=0;
  437. while(check!=1){
  438. System.out.print("Editora: ");
  439. editora=sc.nextLine();
  440. if(editora.equals("")){
  441. System.out.println("O nome da editora nao pode ser null");
  442. }else{
  443. ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM editora WHERE nome='"+editora+"';");
  444. rs.next();
  445. int contagem=rs.getInt("contagem");
  446. if(contagem==0){
  447. System.out.println("Essa editora nao existe");
  448. check=inserirEditora(editora);
  449. }else{
  450. check=1;
  451. }
  452. }
  453. }
  454. check=0;
  455. while(check!=1){
  456. System.out.print("Artista: ");
  457. artista=sc.nextLine();
  458. if(artista.equals("")){
  459. System.out.println("O nome do artista nao pode ser null");
  460. }else{
  461. ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM artista WHERE nome='"+artista+"';");
  462. rs.next();
  463. int contagem=rs.getInt("contagem");
  464. if(contagem==0){
  465. System.out.println("Esse artista nao existe");
  466. ok= sc.nextLine();
  467. }else{
  468. query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+artista+"';");
  469. rs.next();
  470. artista_id=rs.getInt("id");
  471. check=1;
  472. }
  473. }
  474. }
  475. check=0;
  476. while(check!=1){
  477. System.out.print("Duraçao: ");
  478. duracao=sc.nextInt();
  479. if(duracao!=0 && duracao>0){
  480. check=1;
  481. }else
  482. System.out.println("O ano que colocou não é possivel");
  483. }
  484. check=0;
  485. 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+"';");
  486. rs.next();
  487. check=rs.getInt("contagem");
  488. if(check==1){
  489. System.out.println("Esse artista ja tem essa musica");
  490. ok= sc.nextLine();
  491. return null;
  492. }
  493. int album_id=colocaAlbum(nome,genero,editora,artista_id);
  494. try{
  495. query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM musica;");
  496. rs.next();
  497. check=rs.getInt("contagem");
  498. if(check==0){
  499. int id=1;
  500. 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+");");
  501. return nome;
  502. }
  503. query = executeQuery("SELECT max(id)"+"\"id\""+" FROM album;");
  504. rs.next();
  505. int id=rs.getInt("id");
  506. id=id+1;
  507. 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+");");
  508. return nome;
  509. } catch (Exception ex) {
  510. System.out.println();
  511. }
  512. return nome;
  513. }
  514.  
  515. static public int inserirEditora(String editora) throws SQLException{
  516. int teste=0;
  517. int check = 0;
  518. Scanner input = new Scanner(System.in);
  519. while(teste!=1){
  520. System.out.println("Quer adicionar esta editora a base de dados? (Sim/Nao)");
  521. String ok= input.nextLine();
  522. if(ok.equals("Sim")){
  523. try{
  524. teste=1;
  525. check=1;
  526. s.executeQuery("INSERT INTO editora(nome) values('"+editora+"');");
  527.  
  528.  
  529. }catch (Exception ex) {
  530. System.out.println();
  531. }
  532. }else if(!ok.equals("Nao")){
  533. System.out.println("Essa opcao nao e valida");
  534. }else{
  535. check=0;
  536. teste=1;
  537. }
  538. }
  539. return check;
  540. }
  541.  
  542. static public int colocaAlbum(String nome,String genero,String editora, int artista_id){
  543. int id=0;
  544. try{
  545. int dia=1,mes=1,ano=2018;
  546. ResultSet query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM album;");
  547. rs.next();
  548. int check=rs.getInt("contagem");
  549. if(check==0){
  550. id=1;
  551. //id,nome,lancamento(date),genero, editora, artista_id '2008-11-11'
  552. s.executeQuery("INSERT INTO album(id,nome,lancamento,genero,editora,artista_id) values("+id+",'"+nome+"','"+ano+"-"+mes+"-"+dia+"','"+genero+"','"+editora+"',"+artista_id+");");
  553. return id;
  554. }
  555. query = executeQuery("SELECT max(id)"+"\"id\""+" FROM album;");
  556. rs.next();
  557. id=rs.getInt("id");
  558. id=id+1;
  559. s.executeQuery("INSERT INTO album(id,nome,lancamento,genero,editora,artista_id) values("+id+",'"+nome+"','"+ano+"-"+mes+"-"+dia+"','"+genero+"','"+editora+"',"+artista_id+");");
  560.  
  561. } catch (Exception ex) {
  562. System.out.println();
  563. }
  564. return id;
  565. }
  566.  
  567. private static void removerArtista(String nome) throws SQLException {
  568. System.out.println("Remover Artista\n\n");
  569. ResultSet query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+nome+"';");
  570. rs.next();
  571. int artista_id=rs.getInt("id");
  572. try{
  573. s.executeQuery("DELETE from musica where artista_id="+artista_id+";");
  574. } catch (Exception ex) {
  575. System.out.println();
  576. }try{
  577. s.executeQuery("DELETE from album where artista_id="+artista_id+";");
  578. } catch (Exception ex) {
  579. System.out.println();
  580. }try{
  581. s.executeQuery("DELETE from artista where nome='"+nome+"';");
  582. } catch (Exception ex) {
  583. System.out.println();
  584. }
  585.  
  586. }
  587.  
  588. //Remover um album de um artista
  589. private static void removerAlbum(String nome, String artista) throws SQLException {
  590. System.out.println("Remover Album\n\n");
  591. ResultSet query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+artista+"';");
  592. rs.next();
  593. int artista_id=rs.getInt("id");
  594. query = executeQuery("SELECT id"+"\"id\""+" FROM album WHERE nome='"+nome+"' and artista_id="+artista_id+";");
  595. rs.next();
  596. int album_id=rs.getInt("id");
  597. try{
  598. s.executeQuery("DELETE from musica where album_id="+album_id+";");
  599. } catch (Exception ex) {
  600. System.out.println();
  601. }try{
  602. s.executeQuery("DELETE from album where nome='"+nome+"' and artista_id="+artista_id+";");
  603. } catch (Exception ex) {
  604. System.out.println();
  605. }
  606. }
  607.  
  608. private static void removerMusica(String nome, String artista) throws SQLException {
  609. System.out.println("Remover Musica\n\n");
  610. ResultSet query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+artista+"';");
  611. rs.next();
  612. int artista_id=rs.getInt("id");
  613. try{
  614. s.executeQuery("DELETE from musica where nome='"+nome+"' and artista_id="+artista_id+";");
  615. } catch (Exception ex) {
  616. System.out.println();
  617. }try{
  618. s.executeQuery("DELETE from album where nome='"+nome+"' and artista_id="+artista_id+";");
  619. } catch (Exception ex) {
  620. System.out.println();
  621. }
  622. }
  623.  
  624. private static void alterarArtista(String nome) throws SQLException{
  625. Scanner sc = new Scanner(System.in);
  626. Scanner ss= new Scanner(System.in);
  627. int check=0;
  628. String type=null;
  629. String local=null;
  630. ResultSet query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+nome+"';");
  631. rs.next();
  632. int artista_id=rs.getInt("id");
  633. while(check!=1){
  634. System.out.println("O que prentede alterar?\n1-Historia\n2-Type\n3-Adicionar Concerto");
  635. System.out.print("Opcao: ");
  636. int opcao= sc.nextInt();
  637. switch(opcao){
  638. case 1:
  639. System.out.println("Nova historia:");
  640. String historia = ss.nextLine();
  641. try{
  642. s.executeQuery("UPDATE artista set historia='"+historia+"' where id="+artista_id+";");
  643. } catch (Exception ex) {
  644. System.out.println();
  645. }
  646. check=1;
  647. break;
  648. case 2:
  649. int teste=0;
  650. while(teste!=1){
  651. System.out.println("Novo type:");
  652. type = ss.nextLine();
  653. if(type.equals("Banda") || type.equals("Solo")){
  654. teste=1;
  655. }else
  656. System.out.println("O Artista so pode ser uma Banda ou um Solo");
  657. }
  658. try{
  659. s.executeQuery("UPDATE artista set type='"+type+"' where id="+artista_id+";");
  660. } catch (Exception ex) {
  661. System.out.println();
  662. }
  663. check=1;
  664. break;
  665. case 3:
  666. System.out.println("Concerto");
  667. teste=0;
  668. while(teste!=1){
  669. System.out.print("Local: ");
  670. local=ss.nextLine();
  671. if(local.equals("")){
  672. System.out.println("O local nao pode ser null");
  673. }else teste=1;
  674. }
  675. query = executeQuery("SELECT max(id)"+"\"id\""+" FROM concerto;");
  676. rs.next();
  677. int id=rs.getInt("id");
  678.  
  679. try{
  680. if(id==0){
  681. id=1;
  682. s.executeQuery("INSERT INTO concerto(id,local_concerto,artista_id) values("+id+",'"+local+"',"+artista_id+");");
  683. }else{
  684. id=id+1;
  685. s.executeQuery("INSERT INTO concerto(id,local_concerto,artista_id) values("+id+",'"+local+"',"+artista_id+");");
  686. }
  687. } catch (Exception ex) {
  688. System.out.println();
  689. }
  690. check=1;
  691. break;
  692. default :
  693. System.out.println("Essa opcao nao e valida");
  694. String ok= ss.nextLine();
  695. break;
  696. }
  697. }
  698. }
  699.  
  700. private static void alterarMusica(String nome,String artista) throws SQLException{
  701. Scanner sc = new Scanner(System.in);
  702. Scanner ss= new Scanner(System.in);
  703. int check=0;
  704. String letra=null;
  705. String ficheiro=null;
  706. String editora = null;
  707. String genero= null;
  708. ResultSet query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+artista+"';");
  709. rs.next();
  710. int teste=0;
  711. int artista_id=rs.getInt("id");
  712. while(check!=1){
  713. System.out.println("O que prentede alterar?\n1-Letra\n2-Duracao\n3-Ficheiro\n4-Editora\n5-Genero");
  714. System.out.print("Opcao: ");
  715. int opcao= sc.nextInt();
  716. switch(opcao){
  717. case 1:
  718. teste=0;
  719. while(teste!=1){
  720. System.out.print("Nova Letra: ");
  721. letra=ss.nextLine();
  722. if(letra.equals("")){
  723. System.out.println("A letra nao pode ser null");
  724. }else teste=1;
  725. }
  726. try{
  727. s.executeQuery("UPDATE musica set letra='"+letra+"' where nome='"+nome+"'and artista_id="+artista_id+";");
  728. } catch (Exception ex) {
  729. System.out.println();
  730. }
  731. check=1;
  732. break;
  733. case 2:
  734. int duracao=0;
  735. teste=0;
  736. while(teste!=1){
  737. System.out.println("Nova Duracao:");
  738. duracao = sc.nextInt();
  739. if(duracao!=0 && duracao >0) teste=1;
  740. else{
  741. System.out.println("Esse valor nao e possivel");
  742. String ok=ss.nextLine();
  743. }
  744. }
  745. try{
  746. s.executeQuery("UPDATE musica set tempo='"+duracao+"' where nome='"+nome+"' and artista_id="+artista_id+";");
  747. } catch (Exception ex) {
  748. System.out.println();
  749. }
  750. check=1;
  751. break;
  752. case 3:
  753. teste=0;
  754. while(teste!=1){
  755. System.out.print("Novo ficheiro: ");
  756. ficheiro=ss.nextLine();
  757. if(ficheiro.equals("")){
  758. System.out.println("O ficheiro nao pode ser null");
  759. }else teste=1;
  760. }
  761. try{
  762. s.executeQuery("UPDATE musica set ficheiro='"+ficheiro+"' where nome='"+nome+"' and artista_id="+artista_id+";");
  763. } catch (Exception ex) {
  764. System.out.println();
  765. }
  766. check=1;
  767. break;
  768. case 4:
  769. teste=0;
  770. while(teste!=1){
  771. System.out.print("Nova Editora: ");
  772. editora=ss.nextLine();
  773. if(editora.equals("")){
  774. System.out.println("O nome da editora nao pode ser null");
  775. }else{
  776. query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM editora WHERE nome='"+editora+"';");
  777. rs.next();
  778. int contagem=rs.getInt("contagem");
  779. if(contagem==0){
  780. System.out.println("Essa editora nao existe");
  781. teste=inserirEditora(editora);
  782. }else{
  783. teste=1;
  784. }
  785. }
  786. }
  787. try{
  788. s.executeQuery("UPDATE musica set editora_nome='"+editora+"' where nome='"+nome+"' and artista_id="+artista_id+";");
  789. } catch (Exception ex) {
  790. System.out.println();
  791. }
  792. check=1;
  793. break;
  794. case 5:
  795. teste=0;
  796. while(teste!=1){
  797. System.out.print("Novo genero: ");
  798. genero=ss.nextLine();
  799. if(genero.equals("")){
  800. System.out.println("O genero nao pode ser null");
  801. }else teste=1;
  802. }
  803. try{
  804. s.executeQuery("UPDATE musica set genero='"+genero+"' where nome='"+nome+"' and artista_id="+artista_id+";");
  805. } catch (Exception ex) {
  806. System.out.println();
  807. }
  808. check=1;
  809. break;
  810. default :
  811. System.out.println("Essa opcao nao e valida");
  812. String ok= ss.nextLine();
  813. break;
  814. }
  815. }
  816. }
  817.  
  818. private static void alterarAlbum(String nome,String artista) throws SQLException{
  819. Scanner sc = new Scanner(System.in);
  820. Scanner ss= new Scanner(System.in);
  821. int check=0;
  822. String genero=null;
  823. String editora = null;
  824. String escolha=null;
  825. int dia=0,mes=0,ano = 0;
  826. ResultSet query = executeQuery("SELECT id"+"\"id\""+" FROM artista WHERE nome='"+artista+"';");
  827. rs.next();
  828. int teste=0;
  829. int artista_id=rs.getInt("id");
  830. query = executeQuery("SELECT id"+"\"id\""+" FROM album WHERE nome='"+nome+"';");
  831. rs.next();
  832. int album_id=rs.getInt("id");
  833. while(check!=1){
  834. System.out.println("O que prentede alterar?\n1-Editora\n2-Genero\n3-Lancamento\n4-Inserir Musica\n5-Remover Musica");
  835. System.out.print("Opcao: ");
  836. int opcao= sc.nextInt();
  837. switch(opcao){
  838. case 1:
  839. teste=0;
  840. while(teste!=1){
  841. System.out.print("Nova editora: ");
  842. editora=ss.nextLine();
  843. if(editora.equals("")){
  844. System.out.println("O nome da editora nao pode ser null");
  845. }else{
  846. query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM editora WHERE nome='"+editora+"';");
  847. rs.next();
  848. int contagem=rs.getInt("contagem");
  849. if(contagem==0){
  850. System.out.println("Essa editora nao existe");
  851. teste=inserirEditora(editora);
  852. }else{
  853. teste=1;
  854. }
  855. }
  856. }
  857. try{
  858. s.executeQuery("UPDATE album set editora='"+editora+"' where nome='"+nome+"' and artista_id="+artista_id+";");
  859. } catch (Exception ex) {
  860. System.out.println();
  861. }
  862. check=1;
  863. break;
  864. case 2:
  865. teste=0;
  866. while(teste!=1){
  867. System.out.print("Novo genero: ");
  868. genero=ss.nextLine();
  869. if(genero.equals("")){
  870. System.out.println("O genero nao pode ser null");
  871. }else teste=1;
  872. }
  873. try{
  874. s.executeQuery("UPDATE album set genero='"+genero+"' where nome='"+nome+"' and artista_id="+artista_id+";");
  875. } catch (Exception ex) {
  876. System.out.println();
  877. }
  878. check=1;
  879. break;
  880. case 3:
  881. teste=0;
  882. while(teste!=1){
  883. System.out.print("Ano: ");
  884. ano=sc.nextInt();
  885. if(ano<=2018 && ano>0){
  886. teste=1;
  887. }else
  888. System.out.println("O ano que colocou não é possivel");
  889. }
  890. teste=0;
  891. while(teste!=1){
  892. System.out.print("Mes: ");
  893. mes=sc.nextInt();
  894. if(mes<=12 && mes>0){
  895. teste=1;
  896. }else
  897. System.out.println("O mes que colocou não é possivel");
  898. }
  899. teste=0;
  900. while(teste!=1){
  901. System.out.print("Dia: ");
  902. dia=sc.nextInt();
  903. if(dia<=31 && dia>0){
  904. teste=1;
  905. }else
  906. System.out.println("O dia que colocou não é possivel");
  907. }
  908. try{
  909. s.executeQuery("UPDATE album set lancamento='"+ano+"-"+mes+"-"+dia+"' where nome='"+nome+"' and artista_id="+artista_id+";");
  910.  
  911. } catch (Exception ex) {
  912. System.out.println();
  913. }
  914. check=1;
  915. break;
  916. case 4:
  917. query = executeQuery("SELECT count (DISTINCT nome)"+"\"contagem\""+" FROM musica WHERE artista_id="+artista_id+";");
  918. rs.next();
  919. int musicas=rs.getInt("contagem");
  920. teste=0;
  921. if(musicas==0){
  922. System.out.println("Este Artista nao tem musicas");
  923. String ok=ss.nextLine();
  924. check=1;
  925. break;
  926. }
  927. query = executeQuery("SELECT DISTINCT (nome)"+"\"musica\""+" FROM musica WHERE artista_id="+artista_id+";");
  928. System.out.println("Musicas do Artista:");
  929. while(teste!=1){
  930. for(int i=0;i<musicas;i++){
  931. rs.next();
  932. String musica = rs.getString("musica");
  933. System.out.println("---> "+musica);
  934. }
  935. System.out.println("Qual a musica que quer adicionar ao album?");
  936. escolha=ss.nextLine();
  937. query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM musica WHERE nome='"+escolha+"' and artista_id="+artista_id+";");
  938. rs.next();
  939. musicas=rs.getInt("contagem");
  940. if(musicas==0){
  941. System.out.println("A sua escolha nao foi aceite tente novamente");
  942. String ok= ss.nextLine();
  943. }else{
  944. teste=1;
  945. }
  946. }
  947. query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM musica WHERE nome='"+escolha+"' and album_id="+album_id+" and artista_id="+artista_id+";");
  948. rs.next();
  949. int repeticao=rs.getInt("contagem");
  950. if(repeticao!=0){
  951. System.out.println("Essa musica ja existe no album");
  952. check=1;
  953. break;
  954. }
  955. query = executeQuery("SELECT genero"+"\"genero\",letra"+"\"letra\",tempo"+"\"tempo\",ficheiro"+"\"ficheiro\",editora_nome"+"\"editora\" FROM musica WHERE nome='"+escolha+"' and artista_id="+artista_id+";");
  956. rs.next();
  957. genero=rs.getString("genero");
  958. String letra= rs.getString("letra");
  959. int duracao = rs.getInt("tempo");
  960. String ficheiro = rs.getString("ficheiro");
  961. editora= rs.getString("editora");
  962. query = executeQuery("SELECT max(id)"+"\"id\""+" FROM musica;");
  963. rs.next();
  964. int id=rs.getInt("id");
  965. id=id+1;
  966. try{
  967. 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+");");
  968. } catch (Exception ex) {
  969. System.out.println();
  970. }
  971. check=1;
  972. break;
  973. case 5:
  974. query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM musica WHERE album_id="+album_id+" and artista_id="+artista_id+";");
  975. rs.next();
  976. musicas=rs.getInt("contagem");
  977. teste=0;
  978. if(musicas==0){
  979. System.out.println("Este Album nao tem musicas");
  980. check=1;
  981. break;
  982. }
  983. query = executeQuery("SELECT nome"+"\"musica\""+" FROM musica WHERE album_id="+album_id+" and artista_id="+artista_id+";");
  984. System.out.println("Musicas do Album:");
  985. while(teste!=1){
  986. for(int i=0;i<musicas;i++){
  987. rs.next();
  988. String musica = rs.getString("musica");
  989. System.out.println("---> "+musica);
  990. }
  991. System.out.println("Qual a musica que quer remover do album?");
  992. escolha=ss.nextLine();
  993. query = executeQuery("SELECT count(1)"+"\"contagem\""+" FROM musica WHERE nome='"+escolha+"' and album_id="+album_id+";");
  994. rs.next();
  995. musicas=rs.getInt("contagem");
  996. if(musicas==0){
  997. System.out.println("A sua escolha nao foi aceite tente novamente");
  998. String ok= ss.nextLine();
  999. }else{
  1000. teste=1;
  1001. }
  1002. }
  1003. try{
  1004. s.executeQuery("DELETE from musica where nome='"+escolha+"' and album_id="+album_id+" and artista_id="+artista_id+";");
  1005. } catch (Exception ex) {
  1006. System.out.println(ex);
  1007. }
  1008.  
  1009. check=1;
  1010. break;
  1011. default :
  1012. System.out.println("Essa opcao nao e valida");
  1013. String ok= ss.nextLine();
  1014. break;
  1015. }
  1016. }
  1017. }
  1018.  
  1019.  
  1020. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement