-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseHandler.java
More file actions
143 lines (103 loc) · 2.88 KB
/
DatabaseHandler.java
File metadata and controls
143 lines (103 loc) · 2.88 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
package app;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import mu.*;
public class DatabaseHandler{
static Connection con;
public static void getConnection(){
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","SYS as sysdba","bestcoder091196");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(new JDialog()," "+e);
}
}
public void insert(int id,String name)
{
try{
getConnection();
String q = "insert into list values(?,?)";
PreparedStatement pst = con.prepareStatement(q);
pst.setInt(1,id);
pst.setString(2,name);
pst.executeUpdate();
Sound.success();
JOptionPane.showMessageDialog(new JDialog(),"1 Record Added");
}
catch(IllegalArgumentException e)
{
Sound.failure();
JOptionPane.showMessageDialog(new JDialog(),"Invalid ID or name");
}
catch(Exception e)
{
Sound.failure();
JOptionPane.showMessageDialog(new JDialog(),"Record Already Exists");
}
}
//Insert ends
public String query()
{
StringBuilder sb = new StringBuilder();
try{
getConnection();
Statement s = con.createStatement();
ResultSet rs = s.executeQuery("select rollno,name from list");
sb.append("ID: "+"\t"+"NAME: "+"\n");
while(rs.next())
{
sb.append(" "+rs.getInt(1) +" \t" + rs.getString(2)+"\n");
}
rs.close();
}
catch(SQLException e){
JOptionPane.showMessageDialog(new JDialog()," "+e);
}
return sb.toString();
}
public void delete(int id)
{
try{
getConnection();
String q = "delete from list where rollno = ?";
PreparedStatement pst = con.prepareStatement(q);
pst.setInt(1,id);
int i = pst.executeUpdate();
if(i==0)
throw new Exception();
Sound.success();
JOptionPane.showMessageDialog(new JDialog(), "Record Successfully Deleted");
}
catch(Exception e)
{
Sound.failure();
JOptionPane.showMessageDialog(new JDialog()," Record does not exist.");
//System.out.println(e);
}
}
public void modify(int id,String s)
{
String q = "update list set name=? where rollno = ?";
try{
getConnection();
PreparedStatement pst = con.prepareStatement(q);
pst.setString(1,s);
pst.setInt(2,id);
int i = pst.executeUpdate();
if(i==0)
throw new Exception();
Sound.success();
JOptionPane.showMessageDialog(new JDialog(), "Record Successfully Updated");
}
catch(Exception e)
{
Sound.failure();
JOptionPane.showMessageDialog(new JDialog()," Record does not exists.");
}
}
}