Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.*;
- public class programma {
- public static void showTable(String query,Statement st) throws SQLException{
- ResultSet rs = st.executeQuery(query);
- ResultSetMetaData rsmd = rs.getMetaData();
- int numeroColonne = rsmd.getColumnCount();
- for(int i=1;i<=numeroColonne;i++){
- if (i > 1) System.out.print(", ");
- String nomeColonna = rsmd.getColumnLabel(i);
- System.out.print(nomeColonna);
- }
- System.out.println("");
- while(rs.next()){
- for (int i = 1; i <= numeroColonne; i++) {
- if (i > 1) System.out.print(", ");
- if(i < numeroColonne){
- String valoreColonna=rs.getString(i);
- System.out.print(valoreColonna);
- }
- else{
- String valoreColonna = (rs.getString(i) == null)?"NO":"SI";
- System.out.println(valoreColonna);
- }
- }
- }
- }
- public static void main(String[] args) throws ClassNotFoundException, SQLException {
- Class.forName("org.postgresql.Driver");
- String url = "jdbc:postgresql:esercitazione";
- String username = "postgres";
- String password = "esame";
- Connection conn = DriverManager.getConnection(url,username,password);
- boolean stop = false;
- while(!stop){
- 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");
- if(!choice.equals("1") && !choice.equals("2") && !choice.equals("3") && !choice.equals("4"))
- JOptionPane.showMessageDialog(null, "Inserire un numero tra 1 e 4");
- else{
- Statement st = conn.createStatement();
- int c = Integer.parseInt(choice);
- switch(c){
- case 1:
- 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";
- showTable(query,st);
- stop = true;
- break;
- case 2:
- String codice = JOptionPane.showInputDialog("Inserisci il codice");
- String nome = JOptionPane.showInputDialog("Inserisci il nome");
- String categoria = JOptionPane.showInputDialog("Inserisci la categoria");
- String madre = JOptionPane.showInputDialog("Inserisci la madre");
- String insert = "insert into organizzazione values(" + codice + ",'" + nome + "'," + categoria + "," + madre + ")";
- try{
- st.executeUpdate(insert);
- }
- catch(Exception e){
- e.printStackTrace();
- }
- break;
- case 3:
- String organizzazione = JOptionPane.showInputDialog("Inserisci il codice");
- String delete = "delete from organizzazione where codice = " + organizzazione;
- try{
- st.executeUpdate(delete);
- JOptionPane.showMessageDialog(null, "Eliminazione avvenuta con successo");
- }
- catch(Exception e){
- e.printStackTrace();
- JOptionPane.showMessageDialog(null, "Eliminazione non riuscita");
- }
- break;
- case 4:
- String org = JOptionPane.showInputDialog("Inserisci il codice");
- String cat = JOptionPane.showInputDialog("Inserisci la nuova categoria");
- String update = "update organizzazione set categoria = " + cat + " where codice = " + org;
- try{
- st.executeUpdate(update);
- JOptionPane.showMessageDialog(null, "Update avvenuto con successo");
- }
- catch(Exception e){
- e.printStackTrace();
- JOptionPane.showMessageDialog(null, "Update non riuscito");
- }
- break;
- default:
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement