Guest User

Untitled

a guest
Mar 30th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.19 KB | None | 0 0
  1. package appartment;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.Statement;
  7. import javax.swing.JOptionPane;
  8.  
  9.  
  10. public class DB
  11. {
  12. Connection con;
  13. String serv[]={"Maintanance","Swimming","Gym","Club","Backup Power"};
  14. DB()
  15. {
  16. try
  17. {
  18. //housekeeping
  19. Class.forName("com.mysql.jdbc.Driver");
  20.  
  21. con=DriverManager.getConnection("jdbc:mysql://localhost/appartment","aks","hay");
  22. // Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/akshay","aki","papu");
  23.  
  24. }
  25. catch(Exception e)
  26. {
  27. System.out.println("Error:db connection: "+ e.toString());
  28. }
  29. }
  30.  
  31. void connect()
  32. {
  33. try
  34. {
  35. // statements allow to issue SQL queries to the database
  36. Statement statement = con.createStatement();
  37. // resultSet gets the result of the SQL query
  38. ResultSet obj = statement.executeQuery("SELECT flatno,name FROM info ORDER BY name");
  39. while(obj.next())
  40. {
  41. System.out.println(""+obj.getString("flatno")+" "+obj.getString("name"));
  42. }
  43. }
  44. catch(Exception e)
  45. {
  46. System.out.println("Error:dbmanger-connect: "+ e.toString());
  47. }
  48. }
  49. ResultSet selectq(String q)
  50. {
  51. try
  52. {
  53. // statements allow to issue SQL queries to the database
  54. Statement statement = con.createStatement();
  55. // resultSet gets the result of the SQL query
  56. ResultSet o1 = statement.executeQuery(q);
  57. //System.out.println("Name: "+o1.getString("name")+"<br/>"+"Flat No.: "+o1.getString("flatno")+"<br/>"+"Contact No: "+o1.getNString("phone"));
  58. System.out.println("selectq() works");
  59. return o1;
  60. }
  61. catch(Exception e)
  62. {
  63. System.out.println("Error:dbmanger-selectq: "+ e.toString());
  64. return null;
  65. }
  66. }
  67. boolean insertq(String q)
  68.  
  69. {
  70. try
  71. {
  72. // statements allow to issue SQL queries to the database
  73. Statement statement = con.createStatement();
  74. // resultSet gets the result of the SQL query
  75. //if(statement.executeUpdate("INSERT INTO info (flatno, name, phone) VALUES ('4B','Om Makhija',9991112293)")>0)
  76. if(statement.executeUpdate(q)>0)
  77. {System.out.println("Inserted Success full");
  78. return true;
  79. }
  80. else
  81. {
  82. System.out.println("Failed to Insert");
  83. return false;
  84. }
  85. //System.out.println("Name: "+o1.getString("name")+"<br/>"+"Flat No.: "+o1.getString("flatno")+"<br/>"+"Contact No: "+o1.getNString("phone"));
  86.  
  87.  
  88. }
  89. catch(Exception e)
  90. {
  91. System.out.println("Error:dbmanger-selectq: "+ e.toString());
  92. return false;
  93. }
  94. }
  95.  
  96. boolean updateq(String q)
  97.  
  98. {
  99. try
  100. {
  101. // statements allow to issue SQL queries to the database
  102. Statement statement = con.createStatement();
  103. // resultSet gets the result of the SQL query
  104. //if(statement.executeUpdate("INSERT INTO info (flatno, name, phone) VALUES ('4B','Om Makhija',9991112293)")>0)
  105. //if(statement.executeUpdate("UPDATE servcost SET price=550 WHERE serv='S'")>0)
  106. if(statement.executeUpdate(q)>0)
  107. {System.out.println("Updated Success full");
  108. return true;
  109. }
  110. else
  111. {
  112. System.out.println("Failed to Update");
  113. return false;
  114. }
  115. //System.out.println("Name: "+o1.getString("name")+"<br/>"+"Flat No.: "+o1.getString("flatno")+"<br/>"+"Contact No: "+o1.getNString("phone"));
  116.  
  117.  
  118. }
  119. catch(Exception e)
  120. {
  121. System.out.println("Error:dbmanger-selectq: "+ e.toString());
  122. return false;
  123. }
  124. }
  125.  
  126. boolean deleteq(String q)
  127.  
  128. {
  129. try
  130. {
  131. // statements allow to issue SQL queries to the database
  132. Statement statement = con.createStatement();
  133. // resultSet gets the result of the SQL query
  134. //if(statement.executeUpdate("INSERT INTO info (flatno, name, phone) VALUES ('4B','Om Makhija',9991112293)")>0)
  135. //if(statement.executeUpdate("UPDATE servcost SET price=550 WHERE serv='S'")>0)
  136. if(statement.executeUpdate(q)>0)
  137. {System.out.println("Delete Success full");
  138. return true;
  139. }
  140. else
  141. {
  142. System.out.println("Failed to Delete");
  143. return false;
  144. }
  145. //System.out.println("Name: "+o1.getString("name")+"<br/>"+"Flat No.: "+o1.getString("flatno")+"<br/>"+"Contact No: "+o1.getNString("phone"));
  146.  
  147.  
  148. }
  149. catch(Exception e)
  150. {
  151. System.out.println("Error:dbmanger-selectq: "+ e.toString());
  152. return false;
  153. }
  154. }
  155.  
  156.  
  157. String calcBill(String flat)
  158. {
  159. int count=0;
  160. String text="error";
  161. try
  162. {
  163. text="<html>";
  164. ResultSet o1=selectq("SELECT * FROM info WHERE flatno='"+flat+"'");
  165. while(o1.next())
  166. {
  167. count++;
  168. text+="Name: "+o1.getString("name")+"<br/>"+"Flat No.: "+o1.getString("flatno")+"<br/>"+"Contact No: "+o1.getString("phone");
  169. }
  170. if(count==0)
  171. {
  172. text="Enter flat no";
  173. JOptionPane.showMessageDialog(null,"Flat is either vacant or not registed for maintanace!","Flat Not Found :(",JOptionPane.PLAIN_MESSAGE);
  174. }
  175. else
  176. {
  177.  
  178. //="Name: "+o1.getString("name")+"<br/>"+"Flat No.: "+o1.getString("flatno")+"<br/>"+"Contact No: "+o1.getNString("phone");
  179. o1=selectq("SELECT services.serv,price FROM info,services,servcost WHERE info.flatno=services.flatno AND services.serv=servcost.serv AND info.flatno='"+flat+"'");
  180. text+="<br/> <br/> Bill:- <hr/>";
  181. double cost=0;
  182. while(o1.next())
  183. {
  184. text+=fullservConv(o1.getString("serv"))+" Rs:"+o1.getString("price")+"/-<br/>" ;
  185. cost+=Double.parseDouble(o1.getString("price"));
  186. }
  187. text+="<hr/> Total: Rs: "+cost+"/- <br/> <br/> For any queries, please contact admin";
  188. text+="</html>";
  189. }
  190. }
  191. catch(Exception e)
  192. {
  193. System.out.println("Error:dbmanger-calcBill: "+ e.toString());
  194. text="error";
  195. }
  196. return text;
  197. }
  198. String fullservConv(String ch)
  199. {
  200. String temp="Other";
  201. for(int i=0;i<=serv.length;i++)
  202. {
  203. if(serv[i].startsWith(ch))
  204. {
  205. temp=serv[i];
  206. break;
  207. }
  208. }
  209. return temp;
  210. }
  211. String genrateList()
  212. {
  213.  
  214. String text="error";
  215. String flatno="";
  216. try
  217. {
  218. text="<html>";
  219. text+="<table border='1' text-align='center' style='width:100%'> <tr> <th>Flat No.</th><th>Amount to paid (Rs)</th> </tr>";
  220.  
  221. ResultSet o1=selectq("SELECT flatno FROM info");
  222.  
  223. while(o1.next())
  224. {
  225. flatno=o1.getString("flatno");
  226.  
  227. //="Name: "+o1.getString("name")+"<br/>"+"Flat No.: "+o1.getString("flatno")+"<br/>"+"Contact No: "+o1.getNString("phone");
  228. double cost=0;
  229. try
  230. {
  231. // statements allow to issue SQL queries to the database
  232. Statement statement = con.createStatement();
  233. // resultSet gets the result of the SQL query
  234. String q="SELECT services.serv,price FROM info,services,servcost WHERE info.flatno=services.flatno AND services.serv=servcost.serv AND info.flatno='"+flatno+"'";
  235.  
  236. ResultSet o2 = statement.executeQuery(q);
  237. //System.out.println("Name: "+o1.getString("name")+"<br/>"+"Flat No.: "+o1.getString("flatno")+"<br/>"+"Contact No: "+o1.getNString("phone"));
  238.  
  239. while(o2.next())
  240. {
  241. // text+=fullservConv(o1.getString("serv"))+" Rs:"+o1.getString("price")+"/-<br/>" ;
  242. cost+=Double.parseDouble(o2.getString("price"));
  243. }
  244.  
  245. }
  246. catch(Exception e)
  247. {
  248. System.out.println("Error:dbmanger-selectq: "+ e.toString());
  249. cost=0;
  250. }
  251.  
  252.  
  253.  
  254. text+="<tr><td>"+flatno+"</td><td>"+cost+"</td></tr>";
  255.  
  256. }
  257. text+="</table></html>";
  258. }
  259. catch(Exception e)
  260. {
  261. System.out.println("Error:dbmanger-calcBill: "+ e.toString());
  262. text="error";
  263. }
  264. return text;
  265. }
  266.  
  267.  
  268. boolean login(String user, String pass)
  269. {
  270. String q="SELECT * FROM `admin` WHERE admin='"+user+"'";
  271. ResultSet obj=selectq(q);
  272.  
  273. try
  274. {
  275. String passR="";
  276. while(obj.next())
  277. {
  278. passR=obj.getString("pass");
  279. System.out.println("Its "+passR);
  280. }
  281. if(passR.equalsIgnoreCase(pass))
  282. {
  283. System.out.println("Its True..!");
  284. return true;
  285. }
  286. }
  287. catch(Exception e)
  288. {
  289. System.out.println(e.toString()+ "Error:dbmanger-login: ");
  290.  
  291. }
  292. return false;
  293. }
  294. String stats()
  295. {
  296. String q="SELECT serv,count(*) AS co from services group by serv";
  297. String text="";
  298. try
  299. {
  300. ResultSet o1=selectq(q);
  301. text="<html><table border=\"1\"><tr><th>Services</th><th>No. of Flats</th></tr>";
  302.  
  303.  
  304. while(o1.next())
  305. {
  306. text+="<tr><td>"+fullservConv(o1.getString("serv"))+"</td> <td>"+o1.getString("co")+"</td></tr>";
  307. }
  308. text+="</table></html>";
  309. }catch(Exception e)
  310. {
  311. System.out.println("Error:dbmanger-stats: "+ e.toString());
  312. text="error";
  313. }
  314. return text;
  315. }
  316.  
  317. void updAdd(String[] nfp, boolean[] choice) {
  318. //To change body of generated methods, choose Tools | Templates.
  319. int lnfp=nfp.length,lc=choice.length;
  320. //String q="INSERT INTO `info`(`flatno`, `name`, `phone`) VALUES ("+nfp[0]+","+nfp[1]+","+Integer.parseInt(nfp[2])+");";
  321. // String q=String.format("INSERT INTO info(flatno,name,phone) VALUES('%s','%s',%)","12B","Karan",91234912);
  322. int f=1;
  323. //String q=String.format"INSERT INTO info(flatno,name,phone) VALUES('%s','%s',%s)", "Aks","22b","9999999");
  324. while(f==1)
  325. { if( !insertq("INSERT INTO info(name,flatno,phone) VALUES('"+nfp[0]+"','"+nfp[1]+"',"+nfp[2]+")"))
  326. {
  327. JOptionPane.showMessageDialog(null,"Some problem occured while updating, retry again !","Error Occured :(",JOptionPane.PLAIN_MESSAGE);
  328. f=0;
  329. break;
  330. }
  331. for(int i=0;i<lc;i++)
  332. {
  333. if(choice[i])
  334. {
  335. if(!updateq("INSERT INTO services(flatno, serv) VALUES ('"+nfp[1]+"','"+serv[i].charAt(0)+"')"))
  336. {
  337. JOptionPane.showMessageDialog(null,"Problem occured while updating services, Edit again with same flatno !","Error Occured :(",JOptionPane.PLAIN_MESSAGE);
  338. f=0;
  339. break;
  340. }
  341. }
  342. }
  343. if(f==1)
  344. {
  345. JOptionPane.showMessageDialog(null,"Member Succesfull Added !","Success :)",JOptionPane.PLAIN_MESSAGE);
  346. }
  347. f=0;
  348. }
  349. System.out.println("upAdd....1"+nfp[0]+nfp[1]+nfp[2]);
  350. //insertq(q);
  351. }
  352. boolean editService(String q)
  353. {
  354. boolean status=false;
  355. String t[][]=new String[6][2];
  356. try
  357. {
  358. status=insertq("SELECT * FROM servcost");
  359.  
  360. return true;
  361. }
  362. catch(Exception e)
  363. {
  364. System.out.println("Error:dbmanger-editservice: "+ e.toString());
  365. return false;
  366. }
  367. }
  368.  
  369. boolean checkFlat(String flatno) {
  370. //To change body of generated methods, choose Tools | Templates.
  371. String q="SELECT flatno FROM info WHERE flatno='"+flatno+"'";
  372.  
  373. int f=0;
  374. try
  375. {
  376. ResultSet obj=selectq(q);
  377.  
  378. while(obj.next())
  379. {
  380. f++;
  381. }
  382.  
  383. }
  384. catch(Exception e)
  385. {
  386. System.out.println(e.toString()+ "Error:dbmanger-check: ");
  387.  
  388. }
  389. if(f>0)
  390. return true;
  391. else
  392. return false;
  393.  
  394. }
  395.  
  396. void updEdit(String string, boolean[] choice) {
  397. //To change body of generated methods, choose Tools | Templates.
  398. // String q=String.format("INSERT INTO info(flatno,name,phone) VALUES('%s','%s',%)","12B","Karan",91234912);
  399. int f=1;
  400. //String q=String.format"INSERT INTO info(flatno,name,phone) VALUES('%s','%s',%s)", "Aks","22b","9999999");
  401. while(f==1)
  402. {
  403. for(int i=0;i<5;i++)
  404. {
  405. if(choice[i])
  406. {
  407. if(!updateq("INSERT INTO services(flatno, serv) VALUES ('"+string+"','"+serv[i].charAt(0)+"')"))
  408. {
  409. JOptionPane.showMessageDialog(null,"Problem occured while updating services, Edit again with same flatno !","Error Occured :(",JOptionPane.PLAIN_MESSAGE);
  410. f=0;
  411. break;
  412. }
  413. }
  414. }
  415. if(f==1)
  416. {
  417. JOptionPane.showMessageDialog(null,"Member Succesfull Edited !","Success :)",JOptionPane.PLAIN_MESSAGE);
  418. }
  419. f=0;
  420. }
  421.  
  422. }
  423. }
Add Comment
Please, Sign In to add comment