-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentList.java
More file actions
133 lines (117 loc) · 4.11 KB
/
ComponentList.java
File metadata and controls
133 lines (117 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*******************************************************************/
/** Class Description: **/
/** The main functionality of this class is to connect **/
/** to the database and display all of the infomation **/
/** stored on the database in a table. **/
/*******************************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.sql.ResultSet;
public class ComponentList extends JFrame implements ActionListener
{
//creating a JFRAME
JFrame jf;
JLabel l1,l2,l3;
Connection con;
PreparedStatement ps;
Statement stmt;
ResultSet rs;
JMenuBar mbar;
JMenu m1,m2;
JMenuItem m1_1, m2_1;
DefaultTableModel model = new DefaultTableModel();
JTable tabGrid = new JTable(model);
JScrollPane scrlPane = new JScrollPane(tabGrid);
public ComponentList()
{
jf = new JFrame();
jf.setLayout(null);
l1= new JLabel("Component List");
l1.setFont(new Font("Arial",Font.BOLD,25));
l1.setBounds(300,20,300,40);
l1.setForeground(Color.white);
jf.add(l1);
mbar = new JMenuBar();
jf.setJMenuBar(mbar);
m1 = new JMenu("Main Menu");
mbar.add(m1);
m2 = new JMenu("Exit");
mbar.add(m2);
m1_1 = new JMenuItem("Main Menu");
m1.add(m1_1);
m1_1.addActionListener(this);
m2_1 = new JMenuItem("Exit");
m2.add(m2_1);
m2_1.addActionListener(this);
ImageIcon xx = new ImageIcon ("src/owl_mini.png");
l2 = new JLabel(xx);
l2.setBounds(750,10,100,120);
jf.add(l2);
ImageIcon yy = new ImageIcon ("src/gray.png");
l3 = new JLabel(yy);
l3.setBounds(0,0,900,200);
jf.add(l3);
scrlPane.setBounds(0,200,900,500);
jf.add(scrlPane);
tabGrid.setFont(new Font ("Arial",0,15));
model.addColumn("Component_Name");
model.addColumn("Component_ID");
model.addColumn("Component_Brand");
model.addColumn("Component_Price");
model.addColumn("Component_Rating");
model.addColumn("Component_Speed");
model.addColumn("Component_Storage");
int r = 0;
try
{
//accessing the database to print the stored data
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://sql11.freemysqlhosting.net:3306/sql11159021","sql11159021","veqjgTfu5N");
System.out.println("Connected to database.");
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery("SELECT * FROM Units");
while(rs.next())
{
model.insertRow(r++,new Object[]{rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7)});
}
con.close();
}
catch(SQLException se)
{
System.out.println(se);
JOptionPane.showMessageDialog(null,"SQL Error:"+se);
}
catch(Exception e)
{
System.out.println(e);
JOptionPane.showMessageDialog(null,"Error:"+e);
}
//setting size and name of the created JFrame
jf.setTitle("Component List");
jf.setSize(900,700);
jf.setLocation(500,100);
jf.setResizable(false);
jf.getContentPane().setBackground(Color.cyan);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()== m2_1)
{
System.exit(0);
}
else if(ae.getSource()== m1_1)
{
new MainMenu();
jf.setVisible(false);
}
}
public static void main(String args[])
{
new ComponentList();
}
}