-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.java
More file actions
88 lines (73 loc) · 2.94 KB
/
Search.java
File metadata and controls
88 lines (73 loc) · 2.94 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
import java.sql.*;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class Search {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/LiteGroww";
// Database credentials
static final String USER = "root";
static final String PASS = "rikbithi";
public static void render(String un, String PAN){
System.out.print("\033[H\033[2J");
System.out.flush();
System.out.println("----- WELCOME TO LITEGROWW -----");
System.out.println();
System.out.println("Welcome, "+un);
Scanner sc = new Scanner(System.in);
System.out.print("Enter category to search: ");
String cat = sc.nextLine();
Connection conn = null;
Statement stmt = null;
try{
//STEP 1: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 2: Open a connection
//System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
String sql;
sql = "SELECT SName,CurValue,MarketCap,AvailShares from Stock where Stock.category= \'"+cat+"\';";
//STEP 4: Execute query
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
System.out.println("Stocks in category "+cat+": \n---");
while(rs.next()){
String SName = rs.getString("SName");
double CurValue = rs.getDouble("CurValue");
double MarketCap = rs.getDouble("MarketCap");
int AvailShares = rs.getInt("AvailShares");
System.out.println("Stock Name: "+SName);
System.out.println("Current Value: "+CurValue);
System.out.println("Market Cap: "+MarketCap);
System.out.println("Number of shares available: "+AvailShares);
System.out.println("---");
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
TimeUnit.SECONDS.sleep(5);
Home.render(un, PAN);
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}
}
}