//Nate Harris, CS 364, Assignment 2

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.sql.*;

public class Assign2 implements ActionListener {

       JFrame window;
       JTextField user;
       JTextField pass;
       JTextField partnum;

       JLabel userlabel;
       JLabel passlabel;
       JLabel partlabel;

       JButton search;
       JButton connect;
       JTextArea area;
       JLabel areaTitle;
       JScrollPane pane;
       Connection con;

       public static void main(String[] args){
              Assign2 myassignment = new Assign2();
              myassignment.creategui();
       }

       public void creategui(){
              window = new JFrame("Parts Lookup");
              window.setBounds(100, 200, 650, 800);
              window.getContentPane().setLayout(null);
              window.setVisible(true);

              user = new JTextField();
              user.setBounds(10, 20, 200, 40);
              user.setBackground(Color.white);
              window.add(user);

              pass = new JTextField();
              pass.setBounds(220, 20, 200, 40);
              pass.setBackground(Color.white);
              window.add(pass);

              partnum = new JTextField();
              partnum.setBounds(10, 150, 200, 40);
              partnum.setBackground(Color.white);
              window.add(partnum);


              search = new JButton("Search");
              search.setBounds(250, 150, 200, 40);
              search.addActionListener(this);
              window.add(search);


              connect = new JButton("Connect");
              connect.setBounds(430, 20, 200, 40);
              connect.addActionListener(this);
              window.add(connect);

              area = new JTextArea();
              area.setEditable(false);
              pane = new JScrollPane(area);
              pane.setBounds(10, 270, 400, 300);
              window.add(pane);

              userlabel = new JLabel("Username", JLabel.CENTER);
              userlabel.setBounds(10, 60, 200, 40);
              window.add(userlabel);

              passlabel = new JLabel("Password", JLabel.CENTER);
              passlabel.setBounds(220, 60, 200, 40);
              window.add(passlabel);

              partlabel = new JLabel("Part Number", JLabel.CENTER);
              partlabel.setBounds(10, 210, 200, 40);
              window.add(partlabel);

              areaTitle = new JLabel("Suppliers", JLabel.CENTER);
              areaTitle.setBounds(10, 590, 400, 40);
              window.add(areaTitle);
              window.repaint();
       }

public void actionPerformed(ActionEvent e) {
if(e.getSource() == search){
     try {
         Statement s = con.createStatement();
         String exp = "select sname, price from catalog c, supplier s where c.pnum = '" + partnum.getText()+"' and c.sid = s.sid";
         //System.out.println(exp);
         area.setText("");
         ResultSet r = s.executeQuery(exp);

         while (r.next()) {
               area.append(r.getString(1)+" with price "+r.getString(2)+"\n");
         }
     }
     catch (SQLException e1) {

     }
  }

if(e.getSource() == connect){
                 try {
                     Class.forName("org.gjt.mm.mysql.Driver");
                     con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Assign2", user.getText(), pass.getText());

                 }
                 catch (Exception e2) {
                       System.out.println(e2);
                 }

}

}

}