-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJDBCTest.java
More file actions
71 lines (50 loc) · 2.14 KB
/
JDBCTest.java
File metadata and controls
71 lines (50 loc) · 2.14 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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
/*
* This code works fine on clg PC! We use Java + JDBC + Oracle
*/
public class JDBCTest{
static final synchronized public void main(String... abc) throws Exception{
//Take value from console
Scanner s = new Scanner(System.in);
System.out.print("Enter ID: ");
int id = s.nextInt();
System.out.print("Enter Name: ");
String name = s.next();
s.nextLine();
System.out.print("Enter Address: ");
String address = s.nextLine();
System.out.print("Enter Mobile Number: ");
long mobile = s.nextLong();
//JDBC connection!
//Step0 : Import Packages
//Step1 : Load the Driver
// Class.forName("com.mysql.jdbc.Driver"); // outdated!
// Class.forName("com.mysql.cj.jdbc.Driver"); // Latest!
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver Loaded Successfully");
//Step2 : Conn Establishment
//LOCAL MYSQL - College PC
// Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kgisl_fourth_batch","scott","tiger");
//LOCAL ORACLE DB - College PC
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@KITEORACLE38191.kgisledu.com:1521/orcl","scott","tiger");
//LOCAL ORACLE DB - My Laptop!
//this is under observation....!
// Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","sys","password");
System.out.println("Driver Connection established successfully");
//Step3: Create PreparedStatement
PreparedStatement ps = con.prepareStatement("insert into student_jd values(?,?,?,?)");
ps.setInt(1,id);
ps.setString(2,name);
ps.setString(3,address);
ps.setLong(4,mobile);
//Step4: Execute the query
int ch = ps.executeUpdate();
System.out.println(ch+" rows(s) affected");
//Step5: Release the resources
ps.close();
con.close();
}
}