-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment12_part1.java
More file actions
173 lines (161 loc) · 6.13 KB
/
assignment12_part1.java
File metadata and controls
173 lines (161 loc) · 6.13 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.sql.*;
import java.util.*;
public class assignment12_part1 {
/******************************************************
*Assignment: To implement JDBC.
*Name,RollNo: Pooja Baba,TE 302003.
******************************************************/
// private static final Readable InputStream = null;
static String URL="jdbc:mysql://localhost/assignment"; //final constant optional
/* jdbc-protocol that is used
* mysql-subprotocol
* 172.20.35.20-hostname
*/
static String USER="mysql";
static String PASWD="your_password";
public static void main(String args[])
{
int ch,ans;
try
{
do
{
//1.Get a connection to the database.
Connection con=DriverManager.getConnection(URL,USER,PASWD);
/*getConnection-method which will return an object*/
System.out.println("Connection accquired!!");
//2.Create a statement.
Statement stat=con.createStatement();
System.out.println("***MENU***\n1.Insert a new data.\n2.Display data.\n3.Delete data.\n4.Update data.\n5.Search a value.\nEnter your choice: ");
Scanner s=new Scanner(System.in);
ch=s.nextInt();
/***************************INSERT A NEW DATA IN STUDENTS TABLE**********************************************/
if(ch==1)
{
Scanner scan=new Scanner(System.in);
int roll;
String name,add;
System.out.print("Enter the name: ");
name=scan.next();
System.out.print("Enter the roll number: ");
roll=scan.nextInt();
System.out.print("Enter the address: ");
add=scan.next();
stat.executeUpdate("insert into Student values("+roll+",'"+name+"','"+add+"');");
//above statement returns 1 if the required item is found and it is deleted.Else it is 0
ResultSet r=stat.executeQuery("select *from Student;");
System.out.print("\tRoll_no\t\t\tName\t\tAddress\n");
int i = 0;
while(r.next())
{
String rn=r.getString("Rollno");
String n=r.getString("Name");
String a=r.getString("Address");
System.out.println("\t"+rn+"\t\t\t"+n+"\t\t\t"+a);
i+=1;
}
System.out.println("Record inserted!!");
con.close();
scan.close();
}
/***************************SELECT FROM STUDENT TABLE**********************************************/
if(ch==2)
{
//3.Execute the statement with SQL Query.
ResultSet res=stat.executeQuery("select *from Student;");
/*ResultSet contains the table for the fired Query.
* executeQuery-will execute the select queries.
* executeUpdate-will execute insert,delete,etc
*/
System.out.print("\tRollNo\t\tName\t\tAddress\n");
while(res.next())
{
String rn=res.getString("Rollno");
String name=res.getString("Name");
String add=res.getString("Address");
System.out.println("\t"+rn+"\t\t"+name+"\t\t"+add);
}
}
/***************************DELETE FROM RESULT TABLE**********************************************/
if(ch==3)
{
stat.executeUpdate("delete from Result where Roll_no=5 and Sub_code=101;");
//above statement returns 1 if the required item is found and it is deleted.Else it is 0
ResultSet r=stat.executeQuery("select *from Result;");
System.out.print("\tRoll_no\t\t\tSub_code\t\tMarks\n");
int i = 0;
while(r.next())
{
String rn=r.getString("Roll_no");
String sc=r.getString("Sub_code");
String m=r.getString("Marks");
System.out.println("\t"+rn+"\t\t\t"+sc+"\t\t\t"+m);
i+=1;
}
System.out.println("Record deleted!!");
con.close();
}
/***************************UPDATE A VALUE FROM RESULT TABLE**********************************************/
if(ch==4)
{
Scanner sca=new Scanner(System.in);
int marks,rol,subc;
System.out.print("Enter roll number: ");
rol=sca.nextInt();
System.out.print("Enter new subject code: ");
subc=sca.nextInt();
System.out.print("Enter new marks: ");
marks=sca.nextInt();
stat.executeUpdate("update Result set Marks="+marks+" where Roll_no="+rol+" and Sub_code="+subc+";");
//above statement returns 1 if the required item is found and it is deleted.Else it is 0
ResultSet r=stat.executeQuery("select *from Result;");
System.out.print("\tRoll_no\t\t\tSub_code\t\tMarks\n");
int i = 0;
while(r.next())
{
String rn=r.getString("Roll_no");
String sc=r.getString("Sub_code");
String m=r.getString("Marks");
System.out.println("\t"+rn+"\t\t\t"+sc+"\t\t\t"+m);
i+=1;
}
System.out.println("Record updated!!");
con.close();
}
/***************************SEARCH A VALUE***********************************/
if(ch==5)
{
CallableStatement stmnt =null;
Scanner sca=new Scanner(System.in);
int rolno;
System.out.print("Enter roll number: ");
rolno=sca.nextInt();
String sql="{call fetch_detail(?,?,?)}";
stmnt=con.prepareCall(sql);
//Bind IN parameter first, then bind OUT parameter
stmnt.setInt(1,rolno);// This would set rollno
// Because second parameter is OUT so register it
stmnt.registerOutParameter(2,java.sql.Types.VARCHAR);
stmnt.registerOutParameter(3,java.sql.Types.VARCHAR);
//above statement returns 1 if the required item is found and it is deleted.Else it is 0
//Use execute method to run stored procedure.
stmnt.execute();
//Retrieve employee name with getXXX method
String SName = stmnt.getString(2);
String SAddress=stmnt.getString(3);
System.out.print("Student's Name: "+SName+"\nStudent's Address: "+SAddress);
stmnt.close();
con.close();
sca.close();
}
System.out.print("Do you want to continue: ");
ans=s.nextInt();
}while(ans==1);
}
catch (SQLException se){se.printStackTrace();} //SQL Exception
}
private static int nextInt() {
// TODO Auto-generated method stub
return 0;
}
}