Repository navigation
Expand file tree
/
Copy pathSimplejdbc.java
More file actions
172 lines (133 loc) · 5.17 KB
/
Copy pathSimplejdbc.java
File metadata and controls
172 lines (133 loc) · 5.17 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.sql.*;
import java.io.*;
public class Simplejdbc2 {
public static void main(String[] args)
throws Exception, IOException, SQLException, ClassNotFoundException {
// Load the JDBC driver
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Could not load the driver");
}
// Connect to a database
Connection conn = DriverManager.getConnection
("jdbc:mysql://localhost:3306/University?useSSL=false" , "root", "password-deleted-for-privacy");
System.out.println("Database connected");
System.out.println("Connection Established Successful and the DATABASE NAME IS:"
+ conn.getMetaData().getDatabaseProductName());
//Main Menu
boolean done = false;
do {
printMenu();
System.out.print("Type in your option: ");
System.out.flush();String ch = readLine();
System.out.println();
switch(ch.charAt(0)) {
case 'a': CourseTitles(conn);
break;
case'b': DepartmentList(conn);
break;
// case'c': findSections(connection);
// break;
// case'd': findMostWorked(connection);
// break;
// case'e': findMostWorked(connection);
// break;
case'q': done = true;
break;
default: System.out.println(" Not a valid option ");
}//switch
} while(!done);
// Close the connection
conn.close();
}
private static void DepartmentList(Connection conn) {
}
// Create a statement
//Statement statement = connection.createStatement();
// Execute a statement
//ResultSet resultSet = statement.executeQuery
//("select first_name from customer where customer_id = 11119");
private static void CourseTitles(Connection conn)
throws SQLException, IOException {
Statement stmt = conn.createStatement();
String query = "SELECT *" + "FROM Course";
ResultSet rset;
try {
rset = stmt.executeQuery(query);
} catch (SQLException e) {
System.out.println("could not execute query ");
while (e!= null) {
System.out.println ("Message :" + e.getMessage());
e = e.getNextException();
}
stmt.close();
return;
}
System.out.println("The Titles of All Courses");
System.out.println("---------------------------------------\n");
System.out.println(rset);
}
private static String readLine() {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr, 1);
String line = "";
try{
line = br.readLine();
} catch(IOException e) {
System.out.println("Error in SimpleIO.readLine: " +"IOException was thrown");
System.exit(1);
}
return line;
}
private static void printMenu() {
System.out.println("\n QUERY OPTIONS ");
System.out.println("(a) Display the titles of all courses");
System.out.println("(b) Display the list of all departments");
System.out.println("(c) Display the section ID, course ID, and course title of all sections");
System.out.println("(d) Display all course sections offered in Spring 2017");
System.out.println("(e) Display names of students and their majors");
System.out.println("(q) Quit");
}
}
/*static void driverLoop()
throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
// Connect to a database
Connection connection = DriverManager.getConnection
("jdbc:mysql://localhost/University?useSSL=false" , "root", "dlee5217");
System.out.println("Database connected");
while (true) {
System.out.println("Query Options");
System.out.println("(a) Display the titles of all courses");
System.out.println("(b) Display the list of all departments");
System.out.println("(c) Display the section ID, course ID, and course title of all sections");
System.out.println("(d) Display all course sections offered in Spring 2017");
System.out.println("(e) Display names of students and their majors");
System.out.println("(q) Quit");
// Java get String from command line
String option = "a";
if (option.equals("a")) {
// Create a statement
Statement statement = connection.createStatement();
// Execute a statement
ResultSet resultSet = statement.executeQuery
("select first_name from customer where customer_id = 11119");
// Iterate through the result and print the employee names
while (resultSet.next())
System.out.println(resultSet.getString(1) + "\t");
} else if (option.equals("b")) {
// Create a statement
Statement statement = connection.createStatement();
// Execute a statement
ResultSet resultSet = statement.executeQuery
("select first_name from customer where customer_id = 11119");
// Iterate through the result and print the employee names
while (resultSet.next())
System.out.println(resultSet.getString(1) + "\t");
}
}
// connection.close();
}
}*/