-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.java
More file actions
206 lines (189 loc) · 7.04 KB
/
database.java
File metadata and controls
206 lines (189 loc) · 7.04 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
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
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class database {
private Connection connection;
database(String url,String username,String password)
{
try {
connection = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
}
public String adm_login(String user,String pass)
{
String sql="SELECT name FROM admin WHERE username='"+user+"' AND pass='"+pass+"'";
String ret=null;
try(PreparedStatement statement=connection.prepareStatement(sql))
{
ResultSet resultSet=statement.executeQuery();
if(resultSet.next())
ret=resultSet.getString("name");
}
catch(SQLException e)
{
e.printStackTrace();
}
return ret;
}
public void addStudent(String name,long rno,long id,String crs,int yr)
{
String sql="INSERT INTO students (id, name, rno, crs, yr) VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
// Set parameter values
statement.setLong(1, id);
statement.setString(2, name);
statement.setLong(3, rno);
statement.setString(4, crs);
statement.setInt(5, yr);
// Execute the insert statement
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void searchStudent(long id, String arr[])
{
String sql = "SELECT name,rno,crs,yr FROM students WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(sql))
{
statement.setLong(1, id);
try (ResultSet resultSet = statement.executeQuery())
{
int i=1;
if (resultSet.next())
{
arr[i++]=resultSet.getString("name");
arr[i++]=Long.toString(resultSet.getLong("rno"));
arr[i++]=resultSet.getString("crs");
arr[i++]=Integer.toString(resultSet.getInt("yr"));
}
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public void addBook(long id,String name,long isbn,int qty,double price)
{
String sql="INSERT INTO books (id, name, isbn, qty, price) VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, id);
statement.setString(2, name);
statement.setLong(3, isbn);
statement.setInt(4, qty);
statement.setDouble(5, price);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void issueBook(long bid[],long sid)
{
String sql1 = "SELECT name,price FROM books WHERE id = ?";
String sql="INSERT INTO issued (st_id,book_id,b_name,price,date_of_issue) VALUES ("+sid+", ?, ?, ?, CURRENT_DATE())";
String sql2="UPDATE books SET qty = qty - 1, issqty=issqty+1 WHERE id=?;";
try (PreparedStatement statement1 = connection.prepareStatement(sql1);PreparedStatement statement = connection.prepareStatement(sql);PreparedStatement statement2 = connection.prepareStatement(sql2)) {
for(int i=0;i<bid.length;i++)
{
statement1.setLong(1, bid[i]);
String x="";
double y=0.0d;
try (ResultSet resultSet = statement1.executeQuery())
{
if (resultSet.next())
{
x=resultSet.getString("name");
y=resultSet.getDouble("price");
}
}
statement.setLong(1,bid[i]);
statement.setString(2, x);
statement.setDouble(3, y);
statement2.setLong(1,bid[i]);
statement.executeUpdate();
statement2.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public Object[][] getIssued(long id,Object data[][]) {
String sql = "SELECT book_id, b_name, price, date_of_issue FROM issued WHERE st_id = ?";
ResultSet resultSet = null;
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, id);
resultSet = statement.executeQuery();
int row=0;
while (resultSet.next())
row+=1;
data = new Object[row+1][5];
row=0;
resultSet = statement.executeQuery();
while(resultSet.next()) {
for (int col = 0; col < 4; col++) {
data[row][col] = resultSet.getObject(col + 1);
}
row++;
}
return data;
} catch (SQLException e) {
e.printStackTrace();
}
return data;
}
public void ret_book(long sid,long bid)
{
String sql1="DELETE FROM issued WHERE st_id="+(int)sid+" AND book_id="+(int)bid;
String sql2="UPDATE books SET qty=qty+1 WHERE id="+bid;
try (PreparedStatement statement1 = connection.prepareStatement(sql1);PreparedStatement statement2 = connection.prepareStatement(sql2)) {
statement1.executeUpdate();
statement2.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Object[][] bkData(Object data[][]) {
String sql = "SELECT * FROM books";
ResultSet resultSet = null;
try (PreparedStatement statement = connection.prepareStatement(sql)) {
resultSet = statement.executeQuery();
int row=0;
while (resultSet.next())
row+=1;
data = new Object[row][7];
row=0;
resultSet = statement.executeQuery();
while(resultSet.next()) {
for (int col = 0; col < 6; col++) {
data[row][col] = resultSet.getObject(col + 1);
}
data[row][6]=(int)data[row][4]+(int)data[row][5];
row++;
}
return data;
} catch (SQLException e) {
e.printStackTrace();
}
return data;
}
public void addQty(long id)
{
String sql="UPDATE books SET qty=qty+1 WHERE id=?";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, id);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
database obj=new database("jdbc:mysql://localhost:3306/stdatabase","root","gautum1234");
long arr[]={1234,2344};
obj.issueBook(arr, 21011714);
}
}