Advertisement
Guest User

Untitled

a guest
Jan 9th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. package db;
  2.  
  3. import java.sql.*;
  4.  
  5. public class javabean {
  6. String error;
  7. Connection con;
  8.  
  9. public javabean() {
  10. }
  11.  
  12. public void connect() throws ClassNotFoundException, SQLException, Exception {
  13. try {
  14. Class.forName("com.mysql.jdbc.Driver");
  15. con = DriverManager.getConnection("jdbc:mysql://localhost:8080/proiect3", "root", "refresh123");
  16. } catch (ClassNotFoundException cnfe) {
  17. error = "ClassNotFoundException: Nu s-a gasit driverul bazei de date.";
  18. throw new ClassNotFoundException(error);
  19. } catch (SQLException cnfe) {
  20. error = "SQLException: Nu se poate conecta la baza de date.";
  21. throw new SQLException(error);
  22. } catch (Exception e) {
  23. error = "Exception: A aparut o exceptie neprevazuta in timp ce se stabilea legatura la baza de date.";
  24. throw new Exception(error);
  25. }
  26. } // connect()
  27.  
  28. public void connect(String bd) throws ClassNotFoundException, SQLException, Exception {
  29. try {
  30. Class.forName("com.mysql.jdbc.Driver");
  31. con = DriverManager.getConnection("jdbc:mysql://localhost:8080/" + bd, "root", "refresh123");
  32. } catch (ClassNotFoundException cnfe) {
  33. error = "ClassNotFoundException: Nu s-a gasit driverul bazei de date.";
  34. throw new ClassNotFoundException(error);
  35. } catch (SQLException cnfe) {
  36. error = "SQLException: Nu se poate conecta la baza de date.";
  37. throw new SQLException(error);
  38. } catch (Exception e) {
  39. error = "Exception: A aparut o exceptie neprevazuta in timp ce se stabilea legatura la baza de date.";
  40. throw new Exception(error);
  41. }
  42. } // connect(String bd)
  43.  
  44. public void connect(String bd, String ip) throws ClassNotFoundException, SQLException, Exception {
  45. try {
  46. Class.forName("com.mysql.jdbc.Driver");
  47. con = DriverManager.getConnection("jdbc:mysql://" + ip + ":8080/" + bd, "root", "refresh123");
  48. } catch (ClassNotFoundException cnfe) {
  49. error = "ClassNotFoundException: Nu s-a gasit driverul bazei de date.";
  50. throw new ClassNotFoundException(error);
  51. } catch (SQLException cnfe) {
  52. error = "SQLException: Nu se poate conecta la baza de date.";
  53. throw new SQLException(error);
  54. } catch (Exception e) {
  55. error = "Exception: A aparut o exceptie neprevazuta in timp ce se stabilea legatura la baza de date.";
  56. throw new Exception(error);
  57. }
  58. } // connect(String bd, String ip)
  59.  
  60. public void disconnect() throws SQLException {
  61. try {
  62. if (con != null) {
  63. con.close();
  64. }
  65. } catch (SQLException sqle) {
  66. error = ("SQLException: Nu se poate inchide conexiunea la baza de date.");
  67. throw new SQLException(error);
  68. }
  69. } // disconnect()
  70.  
  71. public void adaugafarmacie(String Denumire, String Adresa, String DataInfiintare)
  72. throws SQLException, Exception
  73. {
  74. if (con != null)
  75. {
  76. try
  77. {
  78. // create a prepared SQL statement
  79. Statement stmt;
  80. stmt = con.createStatement();
  81. stmt.executeUpdate("insert into `proiect3`.`farmacie`(Denumire, Adresa, DataInfiintare) values('"+Denumire+"', '"+Adresa+"', '"+DataInfiintare+"');");
  82. }
  83. catch (SQLException sqle)
  84. {
  85. error = "ExceptieSQL: Reactualizare nereusita; este posibil sa existe duplicate.";
  86. throw new SQLException(error);
  87. }
  88. }
  89. else
  90. {
  91. error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
  92. throw new Exception(error);
  93. }
  94. } // end
  95. // of
  96. // adaugaPacient()
  97.  
  98. public ResultSet vedeTabela(String tabel) throws SQLException, Exception
  99. {
  100. ResultSet rs = null;
  101. try
  102. {
  103. String queryString = ("select * from `proiect3`.`" + tabel + "`;");
  104. Statement stmt = con.createStatement(/*ResultSet.TYPE_SCROLL_INSENSITIVE,
  105. ResultSet.CONCUR_READ_ONLY*/);
  106. rs = stmt.executeQuery(queryString);
  107. //System.out.println("111***********************************************************************");
  108. }
  109. catch (SQLException sqle)
  110. {
  111. error = "SQLException: Interogarea nu a fost posibila.";
  112. throw new SQLException(error);
  113. }
  114. catch (Exception e)
  115. {
  116. error = "A aparut o exceptie in timp ce se extrageau datele.";
  117. throw new Exception(error);
  118. } return rs;
  119. } // vedeTabela()
  120.  
  121. public void stergeDateTabela(String[] primaryKeys, String tabela, String dupaID) throws SQLException, Exception {
  122. if (con != null) {
  123. try {
  124. // create a prepared SQL statement
  125. long aux;
  126. PreparedStatement delete;
  127. delete = con.prepareStatement("DELETE FROM " + tabela + " WHERE " + dupaID + "=?;");
  128. for (int i = 0; i < primaryKeys.length; i++) {
  129. aux = java.lang.Long.parseLong(primaryKeys[i]);
  130. delete.setLong(1, aux);
  131. delete.execute();
  132. }
  133. } catch (SQLException sqle) {
  134. error = "ExceptieSQL: Reactualizare nereusita; este posibil sa existe duplicate.";
  135. throw new SQLException(error);
  136. } catch (Exception e) {
  137. error = "A aparut o exceptie in timp ce erau sterse inregistrarile.";
  138. throw new Exception(error);
  139. }
  140. } else {
  141. error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
  142. throw new Exception(error);
  143. }
  144. } // end of stergeDateTabela()
  145.  
  146. public void stergeTabela(String tabela) throws SQLException, Exception {
  147. if (con != null) {
  148. try {
  149. // create a prepared SQL statement
  150. Statement stmt;
  151. stmt = con.createStatement();
  152. stmt.executeUpdate("delete from " + tabela + ";");
  153. } catch (SQLException sqle) {
  154. error = "ExceptieSQL: Stergere nereusita; este posibil sa existe duplicate.";
  155. throw new SQLException(error);
  156. }
  157. } else {
  158. error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
  159. throw new Exception(error);
  160. }
  161. } // end of stergeTabela()
  162.  
  163. public void modificaTabela(String tabela, String IDTabela, long ID, String[] campuri, String[] valori)
  164. throws SQLException, Exception {
  165. String update = "update " + tabela + " set ";
  166. String temp = "";
  167. if (con != null) {
  168. try {
  169. for (int i = 0; i < campuri.length; i++) {
  170. if (i != (campuri.length - 1))
  171. temp = temp + campuri[i] + "='" + valori[i] + "', ";
  172. else
  173. temp = temp + campuri[i] + "='" + valori[i] + "' where " + IDTabela + " = '" + ID + "';";
  174. }
  175. update = update + temp;
  176. // create a prepared SQL statement
  177. Statement stmt;
  178. stmt = con.createStatement();
  179. stmt.executeUpdate(update);
  180. } catch (SQLException sqle) {
  181. error = "ExceptieSQL: Reactualizare nereusita; este posibil sa existe duplicate.";
  182. throw new SQLException(error);
  183. }
  184. } else {
  185. error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
  186. throw new Exception(error);
  187. }
  188. } // end of modificaTabela()
  189.  
  190. public ResultSet intoarceLinie(String tabela, long ID) throws SQLException, Exception {
  191. ResultSet rs = null;
  192. try {
  193. // Execute query
  194. String queryString = ("SELECT * FROM " + tabela + " where BoalaID=" + ID + ";");
  195. Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  196. rs = stmt.executeQuery(queryString); // sql exception
  197. } catch (SQLException sqle) {
  198. error = "SQLException: Interogarea nu a fost posibila.";
  199. throw new SQLException(error);
  200. } catch (Exception e) {
  201. error = "A aparut o exceptie in timp ce se extrageau datele.";
  202. throw new Exception(error);
  203. }
  204. return rs;
  205. } // end of intoarceLinie()
  206.  
  207. public ResultSet intoarceLinieDupaId(String tabela, String denumireId, long ID) throws SQLException, Exception {
  208. ResultSet rs = null;
  209. try {
  210. // Execute query
  211. String queryString = ("SELECT * FROM " + tabela + " where " + denumireId + "=" + ID + ";");
  212. Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  213. rs = stmt.executeQuery(queryString); // sql exception
  214. } catch (SQLException sqle) {
  215. error = "SQLException: Interogarea nu a fost posibila.";
  216. throw new SQLException(error);
  217. } catch (Exception e) {
  218. error = "A aparut o exceptie in timp ce se extrageau datele.";
  219. throw new Exception(error);
  220. }
  221. return rs;
  222. } // end of intoarceLinieDupaId()
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement