Initial Commit
[habeas.git] / src / habeas / Utility.java
1 /*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package habeas;
7
8 import java.util.ArrayList;
9 import java.util.prefs.Preferences;
10 import java.sql.*;
11 import java.util.Dictionary;
12 import java.util.HashMap;
13 import java.util.logging.Level;
14 import java.util.logging.Logger;
15 import javax.swing.JOptionPane;
16
17 /**
18 *
19 * @author hari
20 */
21 public class Utility {
22
23 static void setConnectionURL(String text) {
24 connectionURL = text;
25 Preferences.userRoot().put("ConnectionURL", text);
26 }
27
28 static ArrayList<Object> getClientDetails(int r) {
29 ArrayList<Object> res = new ArrayList <>();
30 try {
31 Connection conn = DriverManager.getConnection("jdbc:sqlite:"+connectionURL);
32 PreparedStatement st = conn.prepareStatement("SELECT * FROM clients WHERE id=?;");
33 st.setInt(1, r);
34 ResultSet rs = st.executeQuery();
35 while (rs.next()) {
36 res.add (rs.getString("ClientName"));
37 res.add(rs.getString("ClientAddress"));
38 res.add (rs.getString("ContactPerson"));
39 res.add (rs.getString("MailID"));
40 res.add (rs.getString("ContactNumber"));
41 }
42 conn.close ();
43 return res;
44
45 } catch (SQLException ex) {
46 Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
47 return null;
48 }
49
50 }
51
52 static boolean addClient(String client_name, String client_address,
53 String contact_person, String email_id, String phone_number) {
54 if ("".equals(client_name))
55 return false;
56 try {
57 Connection conn = DriverManager.getConnection("jdbc:sqlite:" + connectionURL);
58 PreparedStatement st = conn.prepareStatement("INSERT INTO clients (ClientName,"
59 + "ClientAddress, ContactPerson, MailID, ContactNumber) VALUES (?, ?, ?, ?, ?);");
60 st.setString(1, client_name);
61 st.setString(2, client_address);
62 st.setString(3, contact_person);
63 st.setString (4, email_id);
64 st.setString (5, phone_number);
65 st.execute();
66 conn.close();
67 return true;
68
69 } catch (SQLException ex) {
70 Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
71 return false;
72 }
73
74
75 }
76
77 static boolean deleteClient(int r) {
78 try {
79 Connection conn = DriverManager.getConnection("jdbc:sqlite:" + connectionURL);
80 PreparedStatement st = conn.prepareStatement("DELETE FROM clients WHERE id=?;");
81 PreparedStatement st2 = conn.prepareStatement("DELETE FROM legalnotices WHERE ClientId=?;");
82 st.setInt(1, r);
83 st2.setInt (1, r);
84 st.execute();
85 st2.execute();
86 conn.close();
87 return true;
88 } catch (SQLException ex) {
89 Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
90 return false;
91 }
92
93 }
94
95 static boolean updateClient(int r, String client_name, String client_address,
96 String contact_person, String mail_id, String phone_number) {
97 if ("".equals(client_name))
98 return false;
99 try {
100 Connection conn = DriverManager.getConnection("jdbc:sqlite:" + connectionURL);
101 PreparedStatement st = conn.prepareStatement("UPDATE clients SET "
102 + "ClientName=?, ClientAddress=?,"
103 + "ContactPerson=?, MailID=?, ContactNumber=? WHERE id=?;");
104 st.setString(1, client_name);
105 st.setString(2, client_address);
106 st.setString(3, contact_person);
107 st.setString(4, mail_id);
108 st.setString(5, phone_number);
109 st.setInt (6, r);
110 st.execute();
111 conn.close();
112 return true;
113 } catch (SQLException ex) {
114 Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
115 return false;
116 }
117
118
119 }
120 public Utility () {
121
122 }
123 public static String connectionURL ;
124
125 /**
126 *
127 * @return
128 */
129 public static ArrayList<Object> getClientsNameAndId () {
130 ArrayList<Object> data = new ArrayList<>();
131 try {
132 Connection conn = DriverManager.getConnection("jdbc:sqlite:" + connectionURL);
133 Statement st = conn.createStatement();
134 ResultSet rs = st.executeQuery("SELECT id, ClientName from clients;");
135 while (rs.next()) {
136 data.add(rs.getInt("id"));
137 data.add(rs.getString("ClientName"));
138 }
139 conn.close();
140 return data;
141 } catch (SQLException ex) {
142 Logger.getLogger(Utility.class.getName()).log(Level.SEVERE, null, ex);
143 return null;
144 }
145
146 }
147
148 public static void getConnectionURL () {
149 connectionURL = Preferences.userRoot().get("ConnectionURL", "legaldb");
150 }
151
152 }