-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBConnectConsolVersion.java
42 lines (33 loc) · 1.32 KB
/
DBConnectConsolVersion.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
package DatabasesProjects.First;
import java.io.Console;
import java.sql.*;
public class DBConnectConsolVersion {
public static void main(String[] args) {
try{
String username, password;
Console console = System.console();
username = console.readLine("Username: ");
char[] pass = console.readPassword("Password: ");
password = new String(pass);
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql:**********", username, password);
Statement statement = connection.createStatement();
String order;
order = console.readLine();
ResultSet resultSet = statement.executeQuery(order);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
int columnNumber = resultSetMetaData.getColumnCount();
while(resultSet.next()) {
String line = "";
for (int i = 1; i <= columnNumber; i++) {
line += resultSet.getString(i) + " ";
}
System.out.println(line);
}
connection.close();
}
catch (Exception e) {
System.out.println(e);
}
}
}