-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathReadDataEx2.java
More file actions
41 lines (31 loc) · 1.21 KB
/
ReadDataEx2.java
File metadata and controls
41 lines (31 loc) · 1.21 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
// Program to demonstrate on how to fetch specific data from the database
// using userinput.
package src.college.understanding_DBConnectivity;
import java.sql.*;
import java.io.*;
public class ReadDataEx2 {
public static void main(String[] args) {
Console c = System.console();
Connection con = null;
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "";
String id = c.readLine("Enter the id to view the Data : ");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection(url, user, password);
System.out.println("Searching...");
String query = String.format("SELECT * FROM t1 WHERE id = %s", id);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
if (rs.next()){
System.out.println(rs.getString("id")+"\t"+rs.getString("name"));
}else {
System.out.println(String.format("No such data exist with id = %s", id));
}
} catch (Exception e) {
System.out.println(e);
}
System.out.println("Exiting...");
}
}