-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShowNames.java
More file actions
156 lines (131 loc) · 5.37 KB
/
ShowNames.java
File metadata and controls
156 lines (131 loc) · 5.37 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package StudentDatabase.gitRepo.StudentDatabaseSystem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.border.Border;
public class ShowNames extends JFrame implements ActionListener {
int width, height;
Point loc;
ResultSet result;
Vector<JButton> names = new Vector();
Vector<JLabel> rollNumbers = new Vector();
int size = 0;
boolean resultExists = false;
public ShowNames(Point loc, int width, int height, ResultSet results){
this.width = width;
this.height = height;
this.loc = loc;
result = results;
CreateFrame();
}
public void CreateFrame(){
try {
setLayout(null);
setTitle("Student Database System");
// getContentPane().setLayout(null);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(null);
final JScrollPane scrollPanel = new JScrollPane(
mainPanel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS
);
scrollPanel.setBounds(0, 0, 480, 250);
getContentPane().add(scrollPanel);
JLabel sn = new JLabel("S.No.");
sn.setBounds(20, 20, 100, 30);
mainPanel.add(sn);
JLabel Nameheading = new JLabel("Name");
Nameheading.setBounds(130, 20, 100, 30);
mainPanel.add(Nameheading);
JLabel rollNoHeading = new JLabel("Roll No");
rollNoHeading.setBounds(260, 20, 100, 30);
mainPanel.add(rollNoHeading);
int initialHeight = 80;
while(result.next()){
size++;
String name = result.getString(1);
names.add(new JButton(name));
String rollno = result.getString(2);
rollNumbers.add(new JLabel(rollno));
sn = new JLabel("" + size);
sn.setBounds(20, initialHeight + 5, 60, initialHeight + 10);
sn.setSize(60, 20);
// System.out.println(sn.getBounds());
// Border border = BorderFactory.createLineBorder(Color.blue, 2);
// sn.setBorder(border);
mainPanel.add(sn);
names.get(size - 1).setBounds(100, initialHeight, 150, initialHeight + 30);
names.get(size - 1).setSize(100, 30);
names.get(size - 1).setBackground(Color.WHITE);
names.get(size - 1).addActionListener((ActionListener) this);
// System.out.println(names.get(size - 1).getBounds());
mainPanel.add(names.get(size - 1));
rollNumbers.get(size - 1).setBounds(270, initialHeight + 5, 150, initialHeight + 30);
rollNumbers.get(size-1).setSize(100, 20);
mainPanel.add(rollNumbers.get(size - 1));
// System.out.println(rollNumbers.get(size - 1).getBounds());
initialHeight += 40;
}
int height = ((initialHeight > 250) ? initialHeight : 250);
mainPanel.setPreferredSize(new Dimension(450, height));
setSize(500, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(loc);
setVisible(true);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(rootPane, "Some error occurred");
}
}
public static void main(String args[]){
// new ShowNames();
}
@Override
public void actionPerformed(ActionEvent e) {
String rollNumber;
int i;
for(i = 0; i<size; i++){
if(e.getSource() == names.get(i)){
break;
}
}
rollNumber = rollNumbers.get(i).getText();
getData(rollNumber);
if(resultExists)
try {
new ShowFetchedData(loc, result, width, height);
dispose();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(rootPane, "Some error occured");
}
}
private void getData(String data){
try{
Class.forName("com.mysql.jdbc.Driver");
String link = "jdbc:mysql://localhost:3308/StudentDatabase?autoReconnect=true&useSSL=false";
String username = "root";
String password = "";
Connection con = DriverManager.getConnection(link, username, password);
String sql = "";
sql = "select * from student where RollNo = " + data;
Statement stmt = con.createStatement();
result = stmt.executeQuery(sql);
if(!result.isBeforeFirst()){
JOptionPane.showMessageDialog(rootPane, "No student from Provided roll no.");
return;
}
else{
resultExists = true;
}
}
catch(Exception e){
// System.out.println(e);
JOptionPane.showMessageDialog(rootPane, "Some error occurred while connecting to database");
}
}
}