-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewTableWindow.java
149 lines (120 loc) · 4.64 KB
/
ViewTableWindow.java
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author alatt
*/
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class ViewTableWindow extends JFrame {
JLabel orderIdLabel;
JTextField orderIdText;
JButton show;
Color pink = new Color(254, 172, 177);
Color gold = new Color(185, 157, 78);
Connection con;
JTable table;
String[] columnNames = new String[]{"Order ID", "Order Date", "Branch Number", "Customer ID"};
String url = "jdbc:mysql://localhost:3306/flowersstore";//(flowersstore) is the name of my database
String userName = "root";
String password = "123456";
DefaultTableModel model;
JScrollPane scroll;
int t=0;
String roll = "";
String name = "";
String cl = "";
String sec = "";
Font Sub_heading= new Font("Serif",Font.BOLD,25);
Font Sub_heading2= new Font("Serif",Font.BOLD,20);
public ViewTableWindow() {
setTitle("RETRIVE AN INFORMATION OF ORDER TABLES");
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
this.setResizable(false);
//setVisible(true);
setSize(938,1146);
setLocationRelativeTo(null);
setLayout(null);
setContentPane(new JLabel(new ImageIcon(getClass().getResource("wallpaper2.png"))));
orderIdLabel = new JLabel("Order Id : ");
orderIdLabel.setBounds(220, 275, 150, 50);
orderIdLabel.setForeground(gold);
orderIdLabel.setFont(Sub_heading);
add(orderIdLabel);
orderIdText = new JTextField();
orderIdText.setBounds(350, 287, 200, 30);
orderIdText.setBackground(pink);
orderIdText.setForeground(Color.WHITE);
orderIdText.setFont(Sub_heading2);
add(orderIdText);
show = new JButton(" Display ");
show.setBackground(Color.WHITE);
show.setForeground(gold);
show.setBorder(new LineBorder(gold));
show.setBounds(590, 287, 130, 30);
this.add(show);
//--------------------------------------------------table code------------------------------------
JPanel tp= new JPanel( new GridLayout(1,1));
tp.setBounds(160, 350, 600, 300);
model = new DefaultTableModel() ;
model.setColumnIdentifiers(columnNames);
table = new JTable(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
scroll = new JScrollPane(table);
scroll.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
tp.add(scroll);
add(tp);
show.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ac();
}
});
}
//----------------------------Connecting to database---------------------------------------
public void ac() {
t++;
String textvalue = orderIdText.getText();
int teV = Integer.parseInt(textvalue);
try {
if(t>1){model.removeRow(0); t=1;}
con = DriverManager.getConnection(url, userName, password);
String sql = "select * from Orders where O_id = " + teV;
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
int i = 0;
if (rs.next()) {
roll = rs.getString("O_id");
name = rs.getString("O_date");
cl = rs.getString("b_no");
sec = rs.getString("c_id");
model.addRow(new Object[]{roll, name, cl, sec});
i++;
}
if (i < 1) {
JOptionPane.showMessageDialog(null, "No Record Found", "Error",
JOptionPane.ERROR_MESSAGE);
}
if (i == 1) {
System.out.println(i + " Record Found");
} else {
System.out.println(i + " Records Found");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}