-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlay.java
146 lines (124 loc) · 3.43 KB
/
Play.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
/**
* @Class : Play
* @Extend Class : Configuration
* @Description : Class to do several play operations of Music player
* playAllSongs() -> Function to play all songs sorted by Song Title
* playSongsRandomly() -> Function to play all songs randomly using RAND()
* playAParticularSong() -> Function to play a particular song by getting Song Title
*/
package com.onebill.trainingAssessment.musicplayer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
/**
* @author Rathesh Prabakar
* @version 2.0 03/06/2021
*/
public class Play extends Configuration {
Configuration conf = new Configuration();
public void playAllSongs() {
Connection con = null;
Statement stmt = null;
ResultSet songs = null;
try {
con = conf.connectionConfig(); // To get the Database connection
// Step 3 Issuing Query
String query = "select * from MusicFiles order by Song_Title";
// Step 4 : Execute and process the query
stmt = con.createStatement();
songs = stmt.executeQuery(query);
while (songs.next()) {
System.out.println("Playing " + songs.getString("Song_Title") + "...");
Thread.sleep(200);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Step 5 : Close all the objects
finally {
try {
if (con != null)
con.close();
if (stmt != null)
stmt.close();
if (songs != null)
songs.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
public void playSongsRandomly() {
Connection con = null;
Statement stmt = null;
ResultSet songs = null;
try {
con = conf.connectionConfig(); // To get the Database connection
// Step 3 Issuing Query
String query = "select * from MusicFiles order by RAND()";
// Step 4 : Execute and process the query
stmt = con.createStatement();
songs = stmt.executeQuery(query);
while (songs.next()) {
System.out.println("Playing " + songs.getString("Song_Title") + "...");
Thread.sleep(400);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Step 5 : Close all the objects
finally {
try {
if (con != null)
con.close();
if (stmt != null)
stmt.close();
if (songs != null)
songs.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
public void playAParticularSong(String querySong) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet songs = null;
try {
con = conf.connectionConfig(); // To get the Database connection
// Step 3 Issuing Query
String query = "select * from MusicFiles where Song_Title = ?";
// Step 4 : Execute and process the query
pstmt = con.prepareStatement(query);
Scanner input = new Scanner(System.in);
pstmt.setString(1, querySong);
songs = pstmt.executeQuery();
if (songs.next()) {
System.out.print("Playing " + songs.getString("Song_Title") + "....\t");
} else {
System.out.println("OOPS, No song is available in the name of" + querySong);
}
input.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Step 5 : Close all the objects
finally {
try {
if (con != null)
con.close();
if (pstmt != null)
pstmt.close();
if (songs != null)
songs.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}