Guest User

Untitled

a guest
Apr 26th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.67 KB | None | 0 0
  1. /**                                                                            
  2.  * @(#)MusicDatabase.java                                                                                                                                                                                                              
  3.  *  
  4.  * Initiates MusicDatabase and handles the layout of the tabs.
  5.  *                                                                          
  6.  * @author Rami Awad, Erik Stenström, Sakib Pathan                                                  
  7.  * @date 2011/12/05                                                      
  8.  */
  9.  
  10. import javax.swing.JTabbedPane;
  11. import javax.swing.ImageIcon;
  12. import javax.swing.JPanel;
  13. import javax.swing.JFrame;
  14. import javax.swing.JComponent;
  15. import javax.swing.JTextField;
  16. import javax.swing.SwingUtilities;
  17. import javax.swing.UIManager;
  18. import java.awt.BorderLayout;
  19. import java.awt.GridLayout;
  20. import java.awt.TextField;
  21. import java.awt.event.KeyEvent;
  22. import java.awt.BorderLayout;
  23. import java.awt.Container;
  24. import java.sql.Connection;
  25. import java.sql.DriverManager;
  26. import java.sql.PreparedStatement;
  27. import java.sql.ResultSet;
  28. import java.sql.ResultSetMetaData;
  29. import java.sql.SQLException;
  30. import java.sql.Statement;
  31.  
  32. import javax.swing.JFrame;
  33. import javax.swing.JScrollPane;
  34. import javax.swing.JTable;
  35. import javax.swing.table.TableModel;
  36.  
  37. import com.sun.java_cup.internal.runtime.Symbol;
  38. public class MusicDatabase extends JPanel {
  39.     private Panels panel;
  40.  
  41.     /**
  42.      * Handles the layout of the tabs.
  43.      */
  44.     public MusicDatabase() {
  45.         super(new GridLayout(1, 1));
  46.         panel = new Panels();
  47.  
  48.         JTabbedPane tabbedPane = new JTabbedPane();
  49.  
  50.         JComponent panel1 = panel.addArtistPanel();
  51.         tabbedPane.addTab("Add Artist/Album",
  52.                 createImageIcon("album.png"), panel1,
  53.                 "Add Artist or/and Album");
  54.  
  55.         JComponent panel2 = panel.addTrackPanel();
  56.         tabbedPane.addTab("Add Track", createImageIcon("add.png"),
  57.                 panel2, "Add Tracks to Albums");
  58.  
  59.         JComponent panel3 = panel.searchPanel();
  60.         tabbedPane.addTab("Search", createImageIcon("search.png"),
  61.                 panel3, "Search after Tracks");
  62.  
  63.         JComponent panel4 = panel.removePanel();
  64.         tabbedPane.addTab("Remove", createImageIcon("remove.png"),
  65.                 panel4, "Remove Tracks from Artists or Albums");
  66.  
  67.         // Add the tabbed pane to this panel.
  68.         add(tabbedPane);
  69.  
  70.         // The following line enables to use scrolling tabs.
  71.         tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
  72.     }
  73.  
  74.     /** Returns an ImageIcon, or null if the path was invalid. */
  75.     public static ImageIcon createImageIcon(String path) {
  76.         java.net.URL imgURL = MusicDatabase.class.getResource(path);
  77.         if (imgURL != null) {
  78.             return new ImageIcon(imgURL);
  79.         } else {
  80.             System.err.println("Couldn't find file: " + path);
  81.             return null;
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * Create the GUI and show it.
  87.      */
  88.     public static void createAndShowGUI() {
  89.         JFrame frame = new JFrame("Music Database");
  90.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  91.         frame.add(new MusicDatabase(), BorderLayout.CENTER);
  92.         frame.setResizable(false);
  93.  
  94.         frame.setSize(700, 400);
  95.         frame.setVisible(true);
  96.     }
  97.  
  98.     /**
  99.      * The main function, where the program starts. Creating and initialize the
  100.      * application's GUI
  101.      */
  102.     public static void main(String[] args) {
  103.         // Schedule a job for the event dispatch thread:
  104.         // creating and showing this application's GUI.
  105.        
  106.         JTable table = new JTable(myModel());
  107.         SwingUtilities.invokeLater(new Runnable() {
  108.             public void run() {
  109.                 // Turn off metal's use of bold fonts
  110.                 UIManager.put("swing.boldMetal", Boolean.FALSE);
  111.                 createAndShowGUI();
  112.             }
  113.         });
  114.        
  115.        
  116.         /*  if(args.length != 2) {
  117.         System.out.println("Usage: java JDBCTest <user> <password>");
  118.         System.exit(0);
  119.     }
  120. */
  121.     String user = "erik"; // username, e.g. "postgres"
  122.     String pwd = "12345"; // password to your database
  123.     String database = "lab_databas";
  124.     String server =
  125.         "jdbc:postgresql://localhost:5432/" + database +
  126.         "?UseClientEnc=UTF8";
  127.    
  128.     Connection con = null;
  129.  
  130. /*
  131. 1. Ladda in drivrutinerna
  132. 2. Skapa en anslutning (Connection)
  133. 3. Skapa ett statement - Ställ en fråga.
  134. 4. Ressultat levereras i form av ResultSet
  135. 5. Stäng anslutningen
  136. */
  137.    
  138.     try {  
  139.         Class.forName("org.postgresql.Driver").newInstance();
  140.         con = DriverManager.getConnection(server, user, pwd);
  141.         System.out.println("Connected!");
  142.        
  143.         Statement stmt = null;
  144.        
  145.         try
  146.         {
  147.             stmt = con.createStatement();
  148.             stmt.executeUpdate("insert into artist " +
  149.                  "values('9','Robert')");
  150.  
  151.  
  152.             stmt.close();
  153.             con.close();
  154.         }  
  155.         catch (Exception e)
  156.         {
  157.             e.printStackTrace();
  158.         }
  159.         //executeQuery(con, "SELECT * FROM artist");
  160.  
  161.         String sql = "SELECT * FROM artist";
  162.         ResultSet rs = stmt.executeQuery(sql);
  163.         FillTable model = new FillTable(rs);
  164.         return;
  165.        
  166.     /*  String sql = "INSERT INTO artist ("
  167.                 +"namn)"
  168.                 + "values('"+"
  169.         */
  170.        
  171.     /*  Statement stmt = con.createStatement();
  172.         String insertString;
  173.         String artist1 = "'Erikooow'";
  174.         insertString = "INSERT INTO artist("
  175.                 + "(namn) "
  176.                 + "VALUES(" + artist1 +");";  */
  177.     //  "INSERT INTO my_table (col_string) VALUES(?)";
  178.     //  stmt.executeUpdate(insertString);
  179.  
  180.     }
  181.     catch(Exception e) {
  182.         javax.swing.JOptionPane.showMessageDialog(null,
  183.             "Database error, " + e.toString());
  184.     }
  185.     finally {
  186.         try {
  187.             if(con != null) {
  188.                 con.close();
  189.                 System.out.println("Connection closed.");
  190.             }
  191.         }
  192.         catch(SQLException e) {}
  193.     }
  194. }
  195.  
  196.  
  197. private static TableModel myModel() {
  198.         // TODO Auto-generated method stub
  199.         return null;
  200.     }
  201. /*
  202.   public static void executeQuery(Connection con, String query) throws SQLException {
  203.    
  204.     Statement stmt = null;
  205.     try {
  206.         // Execute the SQL statement
  207.         stmt = con.createStatement();
  208.         ResultSet rs = stmt.executeQuery(query);
  209.        
  210.         // Get the attribute names
  211.         ResultSetMetaData metaData = rs.getMetaData();
  212.         int ccount = metaData.getColumnCount();
  213.         for(int c = 1; c <= ccount; c++) {
  214.             System.out.print(metaData.getColumnName(c) + "\t");
  215.            
  216.       /*    ResultSetMetaData metaData1 = rs.getMetaData();
  217.             int crow = ((ResultSet) metaData1).getRow();
  218.             for(int p = 1; p <= crow; c++) {
  219.                 System.out.print(((ResultSet) metaData1).getRow() + "\t");  
  220.            
  221.         }*/
  222.         //System.out.print(ccount + "\t");
  223.         //System.out.println();
  224.         }
  225.  
  226. // Get the attribute values
  227. /*      while (rs.next()) {
  228.             System.out.println();
  229.             // NB! This is an example, not the preferred way to retrieve data.
  230.             // You should use methods that return a specific data type, like
  231.             // rs.getInt(), rs.getString() or such.
  232.             // It's also advisable to store each tuple (row) in an object of
  233.             // custom type (e.g.Employee).
  234.             for(int c1 = 1; c1 <= ccount; c1++) {
  235.                 System.out.print(rs.getObject(c1) + "\t");
  236.             }
  237.            
  238.            
  239.             int rowCount = rs.getRow();
  240.            // System.out.println("Number of Rows=" + rowCount);
  241.            
  242.             /*  for(int p = 1; p <= crow; p++) {
  243.                 System.out.print(rs.getObject(p) + "\t");
  244.             } */
  245.            // System.out.println();
  246.   /*    }
  247.        
  248.    
  249.    
  250.     }
  251.     finally {
  252.         if (stmt != null) {
  253.             stmt.close();
  254.         }
  255.     }
  256. } */
  257. /*
  258. public void insertData(Connection conn)
  259. {
  260.     Statement stmt;
  261.     try
  262.     {
  263.         stmt = conn.createStatement();
  264.         stmt.executeUpdate("insert into artist " +
  265.              "values('1','country1')");
  266.  
  267.  
  268.         stmt.close();
  269.         conn.close();
  270.     }
  271.     catch (Exception e)
  272.     {
  273.         e.printStackTrace();
  274.     }
  275. }  
  276.     */
Add Comment
Please, Sign In to add comment