Advertisement
Guest User

Untitled

a guest
Oct 26th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.ResultSetMetaData;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import javax.swing.*;
  8.  
  9.  
  10.  
  11. public class programma {
  12.  
  13. public static void showTable(String query,Statement st) throws SQLException{
  14. ResultSet rs = st.executeQuery(query);
  15. ResultSetMetaData rsmd = rs.getMetaData();
  16. int numeroColonne = rsmd.getColumnCount();
  17. for(int i=1;i<=numeroColonne;i++){
  18. if (i > 1) System.out.print(", ");
  19. String nomeColonna = rsmd.getColumnLabel(i);
  20. System.out.print(nomeColonna);
  21. }
  22. System.out.println("");
  23. while(rs.next()){
  24. for (int i = 1; i <= numeroColonne; i++) {
  25. if (i > 1) System.out.print(", ");
  26. if(i < numeroColonne){
  27. String valoreColonna=rs.getString(i);
  28. System.out.print(valoreColonna);
  29. }
  30. else{
  31. String valoreColonna = (rs.getString(i) == null)?"NO":"SI";
  32. System.out.println(valoreColonna);
  33. }
  34.  
  35. }
  36. }
  37. }
  38.  
  39. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  40. Class.forName("org.postgresql.Driver");
  41. String url = "jdbc:postgresql:esercitazione";
  42. String username = "postgres";
  43. String password = "esame";
  44. Connection conn = DriverManager.getConnection(url,username,password);
  45.  
  46. boolean stop = false;
  47. while(!stop){
  48. String choice = JOptionPane.showInputDialog("Inserisci un numero tra 1 e 4\n\t1: stampa tabella\n\t2: inserisci un'oraganizzazione\n\t3: cancella un'organizzazione\n\t4: aggiorna la categoria di un'organizzazione");
  49. if(!choice.equals("1") && !choice.equals("2") && !choice.equals("3") && !choice.equals("4"))
  50. JOptionPane.showMessageDialog(null, "Inserire un numero tra 1 e 4");
  51. else{
  52. Statement st = conn.createStatement();
  53. int c = Integer.parseInt(choice);
  54. switch(c){
  55. case 1:
  56. String query = "select o.codice,o.nome,o.categoria,o.madre,t.codice as toporg from organizzazione o left join orgtop t on o.codice = t.codice";
  57. showTable(query,st);
  58. stop = true;
  59. break;
  60. case 2:
  61. String codice = JOptionPane.showInputDialog("Inserisci il codice");
  62. String nome = JOptionPane.showInputDialog("Inserisci il nome");
  63. String categoria = JOptionPane.showInputDialog("Inserisci la categoria");
  64. String madre = JOptionPane.showInputDialog("Inserisci la madre");
  65. String insert = "insert into organizzazione values(" + codice + ",'" + nome + "'," + categoria + "," + madre + ")";
  66. try{
  67. st.executeUpdate(insert);
  68. }
  69. catch(Exception e){
  70. e.printStackTrace();
  71. }
  72. break;
  73. case 3:
  74. String organizzazione = JOptionPane.showInputDialog("Inserisci il codice");
  75. String delete = "delete from organizzazione where codice = " + organizzazione;
  76. try{
  77. st.executeUpdate(delete);
  78. JOptionPane.showMessageDialog(null, "Eliminazione avvenuta con successo");
  79. }
  80. catch(Exception e){
  81. e.printStackTrace();
  82. JOptionPane.showMessageDialog(null, "Eliminazione non riuscita");
  83. }
  84. break;
  85. case 4:
  86. String org = JOptionPane.showInputDialog("Inserisci il codice");
  87. String cat = JOptionPane.showInputDialog("Inserisci la nuova categoria");
  88. String update = "update organizzazione set categoria = " + cat + " where codice = " + org;
  89. try{
  90. st.executeUpdate(update);
  91. JOptionPane.showMessageDialog(null, "Update avvenuto con successo");
  92. }
  93. catch(Exception e){
  94. e.printStackTrace();
  95. JOptionPane.showMessageDialog(null, "Update non riuscito");
  96. }
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. }
  103. }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement