-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertPre.java
More file actions
73 lines (61 loc) · 2.01 KB
/
Copy pathInsertPre.java
File metadata and controls
73 lines (61 loc) · 2.01 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
import java.sql.*;
import java.util.*;
class InsertPre
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
try
{
int reg,i;
String id,name;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","1234");
PreparedStatement stmt;
System.out.println("enter value:");
/// insert
stmt=con.prepareStatement("insert into T values(?,?,?)");
reg=sc.nextInt();
id=sc.next();
name=sc.next();
stmt.setInt(1,reg);//1 specifies the first parameter in the query
stmt.setString(2,id);
stmt.setString(3,name);
i=stmt.executeUpdate();
System.out.println(i+" records inserted");
/// update
System.out.println("enter value:");
stmt=con.prepareStatement("update T set NAME=? where REG=?");
reg=sc.nextInt();
name=sc.next();
stmt.setString(1,name);//1 specifies the first parameter in the query i.e. name
stmt.setInt(2,reg);
i=stmt.executeUpdate();
System.out.println(i+" records updated");
///delete
System.out.println("enter value:");
stmt=con.prepareStatement("delete from T where REG=?");
reg=sc.nextInt();
stmt.setInt(1,reg);
i=stmt.executeUpdate();
System.out.println(i+" records deleted");
///retrieve
stmt=con.prepareStatement("select * from T");
ResultSet rs=stmt.executeQuery();
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
/*
103 10000 aahah
102 hjhjj
102
*/