-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlJdbc.java
More file actions
76 lines (57 loc) · 2.4 KB
/
Copy pathSqlJdbc.java
File metadata and controls
76 lines (57 loc) · 2.4 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
import java.io.*;
import java.sql.*;
import java.util.Properties;
//import java.util.*;
class JDBCTest {
String url;
String user;
String password;
InputStream inputStream;
JDBCTest() throws IOException {
Properties prop = new Properties();
String propFileName = "config.properties";
BufferedReader f = new BufferedReader(new FileReader("config.properties"));
if (f != null) {
prop.load(f);
user = prop.getProperty("username");
url = prop.getProperty("db_url");
password = prop.getProperty("password");
System.out.print(url);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
}
public static void main(String args[]) throws IOException {
JDBCTest t = new JDBCTest();
Statement stmt = null;
try {
Connection con = DriverManager.getConnection(t.url, t.user, t.password);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Success");
System.out.println("Enter Student name");
//Scanner s = new Scanner(System.in);
String str = br.readLine();
stmt = con.createStatement();
String sql = "SELECT STUDENTSNAME.enroll_no, STUDENTSNAME.name, STUDENTS.marks"
+ " FROM STUDENTS " +
"INNER JOIN STUDENTSNAME " +
"ON STUDENTS.enroll_no = STUDENTSNAME.enroll_no" +
" order by STUDENTS.enroll_no";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
//Retrieve by column name
int enroll = rs.getInt("enroll_no");
String name = rs.getString("name");
int age = rs.getInt("marks");
if (name.equals(str)) {
System.out.print("Enroll_no : " + enroll + " ");
System.out.print("Name : " + name + " ");
System.out.print(", marks :" + age + "\n");
}
}
rs.close();
con.close();
} catch (Exception e) {
}
}
}