-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjdbc_5.java
More file actions
73 lines (66 loc) · 2.4 KB
/
jdbc_5.java
File metadata and controls
73 lines (66 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
package jdbcsamples;
import java.io.IOException;
import java.sql.*;
import java.util.Scanner;
public class jdbc_5 {
public static void main(String[] args) throws IOException{
Connection conn = null;
try
{
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("Driver loaded Successfully");
conn = DriverManager.getConnection("jdbc:oracle:thin:@//mypc-PC:1521/xe","advjavabatch","students");
System.out.println("Connected Successfully to the DB");
Scanner kb = new Scanner(System.in);
int ans = 0;
char choice;
do{
PreparedStatement ps = conn.prepareStatement("insert into allbooks values (?,?,?,?)");
System.out.println("Enter Book Id: ");
int id = kb.nextInt();
ps.setInt(1,id);
System.out.println("Enter Book Name: ");
kb.nextLine();
String bn = kb.nextLine();
ps.setString(2,bn);
System.out.println("Enter Book price");
int bp = kb.nextInt();
ps.setInt(3,bp);
System.out.println("Enter Book Subject: ");
kb.nextLine();
String bsub = kb.nextLine();
ps.setString(4, bsub);
ans = ps.executeUpdate()+ans;
System.out.println("Insert more press Y/N");
choice = (char) System.in.read();
}while(choice != 'N');
System.out.println("Row updated: "+ans);
}
catch(ClassNotFoundException ex)
{
System.out.println("Cannot load the driver: "+ex);
ex.printStackTrace();
}
catch(SQLException ex)
{
System.out.println("Problem in closing the connection "+ex);
ex.printStackTrace();
}
finally
{
if(conn!=null)
{
try
{
conn.close();
System.out.println("Disconnected Successfully from the db");
}
catch(SQLException ex)
{
System.out.println("Problem in closing the connection "+ex);
ex.printStackTrace();
}
}
}
}
}