This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildConnection.java
196 lines (165 loc) · 6.28 KB
/
BuildConnection.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class BuildConnection extends JFrame implements ActionListener {
private JTextField hostnameField;
private JTextArea serviceArea;
private JTextField serviceInputField;
private JTextField usernameField;
private JPasswordField passwordField;
private JLabel statusLabel;
private String url;
private String username;
private String password;
public String getUrl() {
return url;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public BuildConnection() {
setTitle("Oracle Database Connection");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(1540, 820));
JLabel background = new JLabel(new ImageIcon("lib\\sql.png"));
background.setLayout(new BorderLayout());
setContentPane(background);
setLayout(new GridBagLayout());
JPanel panel = new JPanel(new GridBagLayout());
panel.setOpaque(false);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weighty = 0.5;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(10, 10, 10, 10);
JLabel titleLabel = new JLabel("Build SQL Connection");
titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
titleLabel.setForeground(Color.GREEN);
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.insets = new Insets(20, 10, 10, 10);
panel.add(titleLabel, gbc);
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 0;
gbc.gridy++;
JLabel hostnameLabel = new JLabel("Hostname:");
hostnameLabel.setForeground(Color.WHITE);
hostnameField = new JTextField(20);
hostnameField.setEditable(false);
panel.add(hostnameLabel, gbc);
gbc.gridx++;
panel.add(hostnameField, gbc);
gbc.gridx++;
gbc.gridx = 0;
gbc.gridy++;
JLabel serviceLabel = new JLabel("Currently available service names:");
serviceLabel.setForeground(Color.WHITE);
serviceArea = new JTextArea(10, 40);
serviceArea.setEditable(false);
JScrollPane serviceScrollPane = new JScrollPane(serviceArea);
panel.add(serviceLabel, gbc);
gbc.gridx++;
panel.add(serviceScrollPane, gbc);
gbc.gridx = 0;
gbc.gridy++;
JLabel serviceInputLabel = new JLabel("Default service name:");
serviceInputLabel.setForeground(Color.WHITE);
serviceInputField = new JTextField(20);
serviceInputField.setText("xepdb1");
serviceInputField.setEditable(false);
panel.add(serviceInputLabel, gbc);
gbc.gridx++;
panel.add(serviceInputField, gbc);
gbc.gridx = 0;
gbc.gridy++;
JLabel usernameLabel = new JLabel("Enter the local Oracle database username:");
usernameLabel.setForeground(Color.WHITE);
usernameField = new JTextField(20);
panel.add(usernameLabel, gbc);
gbc.gridx++;
panel.add(usernameField, gbc);
gbc.gridx = 0;
gbc.gridy++;
JLabel passwordLabel = new JLabel("Enter the local Oracle database password:");
passwordLabel.setForeground(Color.WHITE);
passwordField = new JPasswordField(20);
panel.add(passwordLabel, gbc);
gbc.gridx++;
panel.add(passwordField, gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 3;
gbc.anchor = GridBagConstraints.CENTER;
JButton connectButton = new JButton("Connect to Database");
connectButton.addActionListener(this);
panel.add(connectButton, gbc);
gbc.gridy++;
statusLabel = new JLabel("", SwingConstants.CENTER);
statusLabel.setForeground(Color.WHITE);
panel.add(statusLabel, gbc);
add(panel);
pack();
setVisible(true);
getHostname();
getServiceNames();
}
private void getHostname() {
try {
Process process = Runtime.getRuntime().exec("hostname");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
hostnameField.setText(line);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void getServiceNames() {
try {
Process process = Runtime.getRuntime().exec("lsnrctl status");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder serviceOutput = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
serviceOutput.append(line).append("\n");
}
serviceArea.setText(serviceOutput.toString().trim());
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if ((e.getActionCommand().equals("Connect to Database"))) {
url = "jdbc:oracle:thin:@//" + hostnameField.getText() + ":1521/" + serviceInputField.getText();
username = usernameField.getText();
password = new String(passwordField.getPassword());
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection c = DriverManager.getConnection(url, username, password);
statusLabel.setText("Database connection successful.");
dispose();
c.close();
} catch (ClassNotFoundException | SQLException ex) {
statusLabel.setText("Database connection failed.");
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(BuildConnection::new);
}
}