-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
263 lines (243 loc) · 11.3 KB
/
Main.java
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//import javax.management.Query;
//import javax.swing.plaf.nimbus.State;
import java.util.*;
import java.sql.*;
public class Main {
// private static final String url = "jdbc:mysql://localhost:3306/mydb";
private static final String url = "jdbc:mysql://localhost:3306/lenden";
private static final String username = "root";
private static final String password = "1234nitesh@#$";
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
// try{
// Connection connection = DriverManager.getConnection(url, username, password);
// Statement statement = connection.createStatement();
// String query = "select * from students";
// ResultSet resultSet = statement.executeQuery(query);
// while(resultSet.next()){
// int id = resultSet.getInt("id");
// String name = resultSet.getString("name");
// int age = resultSet.getInt("age");
// double marks = resultSet.getDouble("marks");
// System.out.println("ID: "+ id);
// System.out.println("NAME: "+ name);
// System.out.println("AGE: "+ age);
// System.out.println("MARKS: "+ marks);
// }
//
// }catch(SQLException e){
// System.out.println(e.getMessage());
//
// try{
// Connection connection = DriverManager.getConnection(url, username, password);
// Statement statement = connection.createStatement();
// String query = String.format("insert into students(name, age, marks) values('%s', %o, %f)", "Rahul", 23, 74.6);
// int rowAffected = statement.executeUpdate(query);
// if(rowAffected>0){
// System.out.println("Data Inserted Successfully!");
// }else{
// System.out.println("Data not Inserted");
// }
// }catch(SQLException e){
// System.out.println(e.getMessage());
// update
// try{
// Connection connection = DriverManager.getConnection(url, username, password);
// Statement statement = connection.createStatement();
//// String query = String.format("update students set marks= %f where id =%d", 89.5,2);
// String query = String.format("delete from students where id = 3");
// int rowAffected = statement.executeUpdate(query);
// if(rowAffected>0){
//// System.out.println("Data updated Successfully!");
// System.out.println("Data deleted Successfully!");
// }else{
//// System.out.println("Data not updated");
// System.out.println("Data not deleted");
// }
// }catch(SQLException e){
// System.out.println(e.getMessage());
/*PREPRED STATEMENT*/
// try{
// Connection connection = DriverManager.getConnection(url, username, password);
// Statement statement = connection.createStatement();
////
// String query = "insert into students (name, aage , marks) values(?, ?, ?)";
// PreparedStatement preparedStatement = connection.preparedStatement(query);
// preparedStatement.setString(1, "Ankita");
// preparedStatement.setInt(2, 34);
// preparedStatement.setDouble(3, 56.4);
// int rowAffected = preparedStatement.executeUpdate();
// if(rowAffected>0){
//// System.out.println("Data updated Successfully!");
// System.out.println("Data deleted Successfully!");
// }else{
//// System.out.println("Data not updated");
// System.out.println("Data not deleted");
// }
// }catch(SQLException e){
// System.out.println(e.getMessage());
// try{
// Connection connection = DriverManager.getConnection(url, username, password);
// Statement statement = connection.createStatement();
////
// String query = "insert into students (name, aage , marks) values(?, ?, ?)";
// PreparedStatement preparedStatement = connection.prepareStatement(query);
// preparedStatement.setInt(1,1);
// ResultSet resultSet = preparedStatement.executeQuery();
// if (resultSet.next()) {
// System.out.println("Marks not found!");
// } else {
// double marks = resultSet.getDouble("marks");
// System.out.println("Marks: " + marks);
// }
// }catch(SQLException e){
// System.out.println(e.getMessage());
// try{
// Connection connection = DriverManager.getConnection(url, username, password);
//// Statement statement = connection.createStatement();
// String query ="update students set marks= %f where id %d =?";
// PreparedStatement preparedStatement= connection.prepareStatement(query);
// preparedStatement.setDouble(1,87.5);
// preparedStatement.setInt(2,3);
// int rowsAffected = preparedStatement.executeUpdate();
// if(rowsAffected>0){
// System.out.println("Data updated Successfully!");
// }else{
// System.out.println("Data not updated");
// }
// }catch(SQLException e){
// System.out.println(e.getMessage());
// Batch procesing
// try{
// Connection connection = DriverManager.getConnection(url,username,password);
// Statement statement= connection.createStatement();
// Scanner sc=new Scanner(System.in);
// while(true){
// System.out.println("Enter name");
// String name =sc.next();
// System.out.println("Enter age");
// int age= sc.nextInt();
// System.out.println("Enter Marks");
// double marks=sc.nextDouble();
// System.out.println("Enter more Data(Y/N): ");
// String choice =sc.next();
// String query = String.format("insert into students(name,age ,marks) values('%s', %d, %f)", name, age, marks);
// statement.addBatch(query);
// if(choice.toUpperCase().equals("N")){
// break;
// }
// }
// int[] arr = statement.executeBatch();
//// if(rowsAffected>0){
//// System.out.println("Data updated");
//// }else{
//// System.out.println("Data not Updated");
//// }
// for(int i =0; i<arr.length; i++){
// if(arr[i] == 0){
// System.out.println("query: "+i+ "not executed Successfully!");
// }
// }
//
// }catch(SQlExecution e){
// System.out.println(e.getMessage());
// }
// Batch proecessing with prepared Statement
// try{
// Connection connection = DriverManager.getConnection(url,username,password);
// String query ="insert into students(name,age ,marks) values(?,?,?)";
// PreparedStatement preparedStatement = connection.prepareStatement(query);
// Scanner sc=new Scanner(System.in);
// while(true){
// System.out.println("Enter name");
// String name =sc.next();
// System.out.println("Enter age");
// int age= sc.nextInt();
// System.out.println("Enter Marks");
// double marks=sc.nextDouble();
// System.out.println("Enter more Data(Y/N): ");
// String choice =sc.next();
// preparedStatement.setString(1, "name");
// preparedStatement.setInt(2,age);
// preparedStatement.setDouble(3,marks);
//
// preparedStatement.addBatch();
// if(choice.toUpperCase().equals("N")){
// break;
// }
// }
// int[] arr = preparedStatement.executeBatch();
//// if(rowsAffected>0){
//// System.out.println("Data updated");
//// }else{
//// System.out.println("Data not Updated");
//// }
// for(int i =0; i<arr.length; i++){
// if(arr[i] == 0){
// System.out.println("query: "+i+ "not executed Successfully!");
// }
// }
//
// }catch(SQLException e){
// System.out.println(e.getMessage());
// }
// exceptional handling
try {
Connection connection = DriverManager.getConnection(url, username, password);
connection.setAutoCommit(false);
String debit_query = "update accounts set = balance - ? where account_number ";
String credit_query = "update account set = balance + ? where account_number";
PreparedStatement debitPreparedStatement = connection.prepareStatement(debit_query);
PreparedStatement creditPreparedStatement = connection.prepareStatement(credit_query);
Scanner sc = new Scanner (System.in);
System.out.println("Enter the account number:");
int account_number = sc.nextInt();
System.out.println("Enter Amount:");
double amount = sc.nextDouble();
debitPreparedStatement.setDouble(1, amount);
debitPreparedStatement.setInt(2, account_number);
creditPreparedStatement.setDouble(1, amount);
creditPreparedStatement.setInt(2, 102);
// if(isSufficient(connection, 101, amount))
if(isSufficient(connection, account_number, amount)){
connection.commit();
System.out.println("Trsnsaction Successfull");
// int affectedRows1 = debitPreparedStatement.executeUpdate();
// int affectedRows2 = creditPreparedStatement.executeUpdate();
// debitPreparedStatement.executeUpdate();
// creditPreparedStatement.executeUpdate();
}else {
connection.rollback();
System.out.println("transaction failed");
// System.out.println("Insufficient balance!!");
}
// debitPreparedStatement.executeUpdate();
// creditPreparedStatement.executeUpdate();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
static boolean isSufficient(Connection connection, int account_number, double amount) {
try {
String query = "select balance from accounts where account_number= ?";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, account_number);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
double current_balance = resultSet.getDouble("balance");
if(amount > current_balance){
return false;
} else {
return true;
}
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return false;
}
}