Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @(#)MusicDatabase.java
- *
- * Initiates MusicDatabase and handles the layout of the tabs.
- *
- * @author Rami Awad, Erik Stenström, Sakib Pathan
- * @date 2011/12/05
- */
- import javax.swing.JTabbedPane;
- import javax.swing.ImageIcon;
- import javax.swing.JPanel;
- import javax.swing.JFrame;
- import javax.swing.JComponent;
- import javax.swing.JTextField;
- import javax.swing.SwingUtilities;
- import javax.swing.UIManager;
- import java.awt.BorderLayout;
- import java.awt.GridLayout;
- import java.awt.TextField;
- import java.awt.event.KeyEvent;
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.JFrame;
- import javax.swing.JScrollPane;
- import javax.swing.JTable;
- import javax.swing.table.TableModel;
- import com.sun.java_cup.internal.runtime.Symbol;
- public class MusicDatabase extends JPanel {
- private Panels panel;
- /**
- * Handles the layout of the tabs.
- */
- public MusicDatabase() {
- super(new GridLayout(1, 1));
- panel = new Panels();
- JTabbedPane tabbedPane = new JTabbedPane();
- JComponent panel1 = panel.addArtistPanel();
- tabbedPane.addTab("Add Artist/Album",
- createImageIcon("album.png"), panel1,
- "Add Artist or/and Album");
- JComponent panel2 = panel.addTrackPanel();
- tabbedPane.addTab("Add Track", createImageIcon("add.png"),
- panel2, "Add Tracks to Albums");
- JComponent panel3 = panel.searchPanel();
- tabbedPane.addTab("Search", createImageIcon("search.png"),
- panel3, "Search after Tracks");
- JComponent panel4 = panel.removePanel();
- tabbedPane.addTab("Remove", createImageIcon("remove.png"),
- panel4, "Remove Tracks from Artists or Albums");
- // Add the tabbed pane to this panel.
- add(tabbedPane);
- // The following line enables to use scrolling tabs.
- tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
- }
- /** Returns an ImageIcon, or null if the path was invalid. */
- public static ImageIcon createImageIcon(String path) {
- java.net.URL imgURL = MusicDatabase.class.getResource(path);
- if (imgURL != null) {
- return new ImageIcon(imgURL);
- } else {
- System.err.println("Couldn't find file: " + path);
- return null;
- }
- }
- /**
- * Create the GUI and show it.
- */
- public static void createAndShowGUI() {
- JFrame frame = new JFrame("Music Database");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(new MusicDatabase(), BorderLayout.CENTER);
- frame.setResizable(false);
- frame.setSize(700, 400);
- frame.setVisible(true);
- }
- /**
- * The main function, where the program starts. Creating and initialize the
- * application's GUI
- */
- public static void main(String[] args) {
- // Schedule a job for the event dispatch thread:
- // creating and showing this application's GUI.
- JTable table = new JTable(myModel());
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- // Turn off metal's use of bold fonts
- UIManager.put("swing.boldMetal", Boolean.FALSE);
- createAndShowGUI();
- }
- });
- /* if(args.length != 2) {
- System.out.println("Usage: java JDBCTest <user> <password>");
- System.exit(0);
- }
- */
- String user = "erik"; // username, e.g. "postgres"
- String pwd = "12345"; // password to your database
- String database = "lab_databas";
- String server =
- "jdbc:postgresql://localhost:5432/" + database +
- "?UseClientEnc=UTF8";
- Connection con = null;
- /*
- 1. Ladda in drivrutinerna
- 2. Skapa en anslutning (Connection)
- 3. Skapa ett statement - Ställ en fråga.
- 4. Ressultat levereras i form av ResultSet
- 5. Stäng anslutningen
- */
- try {
- Class.forName("org.postgresql.Driver").newInstance();
- con = DriverManager.getConnection(server, user, pwd);
- System.out.println("Connected!");
- Statement stmt = null;
- try
- {
- stmt = con.createStatement();
- stmt.executeUpdate("insert into artist " +
- "values('9','Robert')");
- stmt.close();
- con.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- //executeQuery(con, "SELECT * FROM artist");
- String sql = "SELECT * FROM artist";
- ResultSet rs = stmt.executeQuery(sql);
- FillTable model = new FillTable(rs);
- return;
- /* String sql = "INSERT INTO artist ("
- +"namn)"
- + "values('"+"
- */
- /* Statement stmt = con.createStatement();
- String insertString;
- String artist1 = "'Erikooow'";
- insertString = "INSERT INTO artist("
- + "(namn) "
- + "VALUES(" + artist1 +");"; */
- // "INSERT INTO my_table (col_string) VALUES(?)";
- // stmt.executeUpdate(insertString);
- }
- catch(Exception e) {
- javax.swing.JOptionPane.showMessageDialog(null,
- "Database error, " + e.toString());
- }
- finally {
- try {
- if(con != null) {
- con.close();
- System.out.println("Connection closed.");
- }
- }
- catch(SQLException e) {}
- }
- }
- private static TableModel myModel() {
- // TODO Auto-generated method stub
- return null;
- }
- /*
- public static void executeQuery(Connection con, String query) throws SQLException {
- Statement stmt = null;
- try {
- // Execute the SQL statement
- stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(query);
- // Get the attribute names
- ResultSetMetaData metaData = rs.getMetaData();
- int ccount = metaData.getColumnCount();
- for(int c = 1; c <= ccount; c++) {
- System.out.print(metaData.getColumnName(c) + "\t");
- /* ResultSetMetaData metaData1 = rs.getMetaData();
- int crow = ((ResultSet) metaData1).getRow();
- for(int p = 1; p <= crow; c++) {
- System.out.print(((ResultSet) metaData1).getRow() + "\t");
- }*/
- //System.out.print(ccount + "\t");
- //System.out.println();
- }
- // Get the attribute values
- /* while (rs.next()) {
- System.out.println();
- // NB! This is an example, not the preferred way to retrieve data.
- // You should use methods that return a specific data type, like
- // rs.getInt(), rs.getString() or such.
- // It's also advisable to store each tuple (row) in an object of
- // custom type (e.g.Employee).
- for(int c1 = 1; c1 <= ccount; c1++) {
- System.out.print(rs.getObject(c1) + "\t");
- }
- int rowCount = rs.getRow();
- // System.out.println("Number of Rows=" + rowCount);
- /* for(int p = 1; p <= crow; p++) {
- System.out.print(rs.getObject(p) + "\t");
- } */
- // System.out.println();
- /* }
- }
- finally {
- if (stmt != null) {
- stmt.close();
- }
- }
- } */
- /*
- public void insertData(Connection conn)
- {
- Statement stmt;
- try
- {
- stmt = conn.createStatement();
- stmt.executeUpdate("insert into artist " +
- "values('1','country1')");
- stmt.close();
- conn.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- */
Add Comment
Please, Sign In to add comment