-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoomDatabase.java
154 lines (130 loc) · 4.74 KB
/
RoomDatabase.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
/*
======================================================
Room Database
@author William Clift
This class provides the connection to the Room Assignment Database that stores all of the room information.
=====================================================
*/
import java.sql.*;
import java.util.Date;
import java.util.Scanner;
public class RoomDatabase{
private static Connection conn;
private static Statement stmt;
/**
Room Database Constructor
*/
public RoomDatabase(){}
/**
Prompts the user to input the username and password information; No parameters needed
*/
public void userLogin(){
Scanner in = new Scanner(System.in);
boolean loggedIn = false;
boolean exit = false;
//Loops until the user enters correct login or chooses to exit
while(!loggedIn || !exit){
String username, password, databaseName;
System.out.print("--Connect to Database--\nUsername: ");
username = in.nextLine();
System.out.print("Password: ");
password = in.nextLine();
loggedIn = login(username, password);
//Failed Login Procedure - Reprompt the user for Identification
if(!loggedIn){
//Prompts the User to retry the Username/Password Combination
System.out.print("Try again (y/n)?: ");
String retry = in.nextLine();
//Exit only if user inputs "n"
if(retry == "n"){
exit = true;
}
}
}
}
/**
Auto Login using hard coded username and password to access the database
@param username
@param password
*/
public void autoLogin(String username, String password){
boolean exit = false;
while(!exit){
System.out.println("--Connect to Database--\nProcessing...");
boolean loggedIn = login(username, password);
//Failed Login Procedure
if(!loggedIn)
exit = true;
}
}
/**
Provides the User an opprotunity to manually login, using the command line.
*/
private boolean login(String username, String password){
boolean loggedIn = false;
//Checks the login information to ensure validity -- Accesses the database if it is correct
if(checkLogin(username, password)){
establishConnectionToDatabase("RoomAssignment", "sa", "reallyStrongPwd123");
loggedIn = true;
}
//Once Login is successful, inform the user.
if(loggedIn)
System.out.println("--Login Successful--");
else
System.out.println("Username or Password not recognized.");
return loggedIn;
}
/**
Checks to see if the login is valid and logs in using the proper identification if it is correct
@param username
@param password
@return boolean checkLogin
*/
private boolean checkLogin(String username, String password){
boolean valid = false;
if(username.equals("residencelife") && password.equals("bears")){
valid = true;
}
return valid;
}
/**
Establish the Connection to the database, catches errors
@param databaseName
@param username
@param password
*/
private void establishConnectionToDatabase(String databaseName, String username, String password) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String dbURL = "jdbc:sqlserver://localhost;databaseName="+databaseName+";user="+username+";password="+password;
conn = DriverManager.getConnection(dbURL);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
/**
Executes the query statement for the String s
@param s -- the intended statement to execute
@return rs -- the Result set of the sql query
*/
private ResultSet executeQueryStatement(String s) throws SQLException {
ResultSet rs;
rs = stmt.executeQuery(s);
return rs;
}
/**
Display the Query Results
@param rs -- the result set of the query results
*/
private void displayQueryResults(ResultSet rs) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
int numOfCols = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i < numOfCols + 1; i++) {
System.out.print((rsmd.getColumnLabel(i) + ": " + rs.getString(i)
+ " "));
}
System.out.println();
}
}
}