Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.36 KB | None | 0 0
  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 db;
  7.  
  8. import java.sql.*;
  9.  
  10. /**
  11. *
  12. * @author vali
  13. */
  14. public class JavaBean {
  15.  
  16. String error;
  17. Connection con;
  18.  
  19. public JavaBean() {
  20. }
  21.  
  22. public void connect() throws ClassNotFoundException, SQLException, Exception {
  23. try {
  24. Class.forName("com.mysql.jdbc.Driver");
  25. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/biblioteca?useSSL=false", "root", "qwerty97");
  26. } catch (ClassNotFoundException cnfe) {
  27. error = "ClassNotFoundException: Nu s-a gasit driverul bazei de date.";
  28. throw new ClassNotFoundException(error);
  29. } catch (SQLException cnfe) {
  30. error = "SQLException: Nu se poate conecta la baza de date.";
  31. throw new SQLException(error);
  32. } catch (Exception e) {
  33. error = "Exception: A aparut o exceptie neprevazuta in timp ce se stabilea legatura la baza de date.";
  34. throw new Exception(error);
  35. }
  36. } // connect()
  37.  
  38. public void connect(String bd) throws ClassNotFoundException, SQLException, Exception {
  39. try {
  40. Class.forName("com.mysql.jdbc.Driver");
  41. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + bd, "root", "qwerty97");
  42. } catch (ClassNotFoundException cnfe) {
  43. error = "ClassNotFoundException: Nu s-a gasit driverul bazei de date.";
  44. throw new ClassNotFoundException(error);
  45. } catch (SQLException cnfe) {
  46. error = "SQLException: Nu se poate conecta la baza de date.";
  47. throw new SQLException(error);
  48. } catch (Exception e) {
  49. error = "Exception: A aparut o exceptie neprevazuta in timp ce se stabilea legatura la baza de date.";
  50. throw new Exception(error);
  51. }
  52. } // connect(String bd)
  53.  
  54. public void connect(String bd, String ip) throws ClassNotFoundException, SQLException, Exception {
  55. try {
  56. Class.forName("com.mysql.jdbc.Driver");
  57. con = DriverManager.getConnection("jdbc:mysql://" + ip + ":3306/" + bd, "root", "qwerty97");
  58. } catch (ClassNotFoundException cnfe) {
  59. error = "ClassNotFoundException: Nu s-a gasit driverul bazei de date.";
  60. throw new ClassNotFoundException(error);
  61. } catch (SQLException cnfe) {
  62. error = "SQLException: Nu se poate conecta la baza de date.";
  63. throw new SQLException(error);
  64. } catch (Exception e) {
  65. error = "Exception: A aparut o exceptie neprevazuta in timp ce se stabilea legatura la baza de date.";
  66. throw new Exception(error);
  67. }
  68. } // connect(String bd, String ip)
  69.  
  70. public void disconnect() throws SQLException {
  71. try {
  72. if (con != null) {
  73. con.close();
  74. }
  75. } catch (SQLException sqle) {
  76. error = ("SQLException: Nu se poate inchide conexiunea la baza de date.");
  77. throw new SQLException(error);
  78. }
  79. } // disconnect()
  80.  
  81. public void adaugaCarte(String Titlu, String ISBN, String Editura)
  82. throws SQLException, Exception {
  83. if (con != null) {
  84. try {
  85. // create a prepared SQL statement
  86. Statement stmt;
  87. stmt = con.createStatement();
  88. stmt.executeUpdate("insert into carti(Titlu, ISBN, Editura) values('" + Titlu + "' , '" + ISBN + "', '" + Editura + "');");
  89.  
  90. } catch (SQLException sqle) {
  91. error = "ExceptieSQL: Reactualizare nereusita; este posibil sa existe duplicate.";
  92. throw new SQLException(error);
  93. }
  94. } else {
  95. error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
  96. throw new Exception(error);
  97. }
  98. } // end of adaugaPacient()
  99.  
  100. public void adaugaAutor(String Nume, String Prenume, String Nationalitate)
  101. throws SQLException, Exception {
  102. if (con != null) {
  103. try {
  104. // create a prepared SQL statement
  105. Statement stmt;
  106. stmt = con.createStatement();
  107. stmt.executeUpdate("insert into autori(Nume, Prenume, Nationalitate) values('" + Nume + "' , '" + Prenume + "', '" + Nationalitate + "');");
  108.  
  109. } catch (SQLException sqle) {
  110. error = "ExceptieSQL: Reactualizare nereusita; este posibil sa existe duplicate.";
  111. throw new SQLException(error);
  112. }
  113. } else {
  114. error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
  115. throw new Exception(error);
  116. }
  117. } // end of adaugaMedic()
  118.  
  119. public void adaugaOpera(int idCarte, int idAutor, String NumeOpera, String GenOpera, String NrPagini)
  120. throws SQLException, Exception {
  121. if (con != null) {
  122. try {
  123. // create a prepared SQL statement
  124. Statement stmt;
  125. stmt = con.createStatement();
  126. stmt.executeUpdate("insert into opera(idCarte, idAutor, Nume, Gen, NrPagini) values('" + idCarte + "' , '" + idAutor + "', '" + NumeOpera + "', '" + GenOpera + "', '" + NrPagini+ "');");
  127. // se poate modifica valoarea datei astfel incat sa se ia data curenta a sistemului:
  128. // stmt.executeUpdate("insert into consultatie(idpacient, idmedic, DataConsultatie, Diagnostic, Medicament) values('" + idpacient + "' , '" + idmedic + "', CURDATE(), '" + Diagnostic + "', '" + Medicament + "');");
  129.  
  130. } catch (SQLException sqle) {
  131. error = "ExceptieSQL: Reactualizare nereusita; este posibil sa existe duplicate.";
  132. throw new SQLException(error);
  133. }
  134. } else {
  135. error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
  136. throw new Exception(error);
  137. }
  138. } // end of adaugaConsultatie()
  139.  
  140. public ResultSet vedeTabela(String tabel) throws SQLException, Exception {
  141. ResultSet rs = null;
  142. try {
  143. String queryString = ("select * from `biblioteca`.`" + tabel + "`;");
  144. Statement stmt = con.createStatement(/*ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY*/);
  145. rs = stmt.executeQuery(queryString);
  146. } catch (SQLException sqle) {
  147. error = "SQLException: Interogarea nu a fost posibila. qweqwfwefwef";
  148. throw new SQLException(error);
  149. } catch (Exception e) {
  150. error = "A aparut o exceptie in timp ce se extrageau datele.";
  151. throw new Exception(error);
  152. }
  153. return rs;
  154. } // vedeTabela()
  155.  
  156. public ResultSet vedeOpera() throws SQLException, Exception {
  157. ResultSet rs = null;
  158. try {
  159. String queryString = ("select a.Titlu TitluCarte, a.ISBN , a.Editura EdituraCarte, b.Nume NumeAutor, b.Prenume PrenumeAutor, b.Nationalitate, c.idopera, c.idAutor, c.idCarte, c.Nume NumeOpera, c.Gen GenOpera, c.NrPagini from carti a, autori b, opera c where a.idCarte = c.idCarte and b.idAutor = c.idAutor;");
  160. Statement stmt = con.createStatement(/*ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY*/);
  161. rs = stmt.executeQuery(queryString);
  162. } catch (SQLException sqle) {
  163. error = "SQLException: Interogarea nu a fost posibila. dgdg";
  164. throw new SQLException(error);
  165. } catch (Exception e) {
  166. error = "A aparut o exceptie in timp ce se extrageau datele.";
  167. throw new Exception(error);
  168. }
  169. return rs;
  170. } // vedeConsultatie()
  171.  
  172. public void stergeDateTabela(String[] primaryKeys, String tabela, String dupaID) throws SQLException, Exception {
  173. if (con != null) {
  174. try {
  175. // create a prepared SQL statement
  176. long aux;
  177. PreparedStatement delete;
  178. delete = con.prepareStatement("DELETE FROM " + tabela + " WHERE " + dupaID + "=?;");
  179. for (int i = 0; i < primaryKeys.length; i++) {
  180. aux = java.lang.Long.parseLong(primaryKeys[i]);
  181. delete.setLong(1, aux);
  182. delete.execute();
  183. }
  184. } catch (SQLException sqle) {
  185. error = "ExceptieSQL: Reactualizare nereusita; este posibil sa existe duplicate.";
  186. throw new SQLException(error);
  187. } catch (Exception e) {
  188. error = "A aparut o exceptie in timp ce erau sterse inregistrarile.";
  189. throw new Exception(error);
  190. }
  191. } else {
  192. error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
  193. throw new Exception(error);
  194. }
  195. } // end of stergeDateTabela()
  196.  
  197. public void stergeTabela(String tabela) throws SQLException, Exception {
  198. if (con != null) {
  199. try {
  200. // create a prepared SQL statement
  201. Statement stmt;
  202. stmt = con.createStatement();
  203. stmt.executeUpdate("delete from " + tabela + ";");
  204. } catch (SQLException sqle) {
  205. error = "ExceptieSQL: Stergere nereusita; este posibil sa existe duplicate.";
  206. throw new SQLException(error);
  207. }
  208. } else {
  209. error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
  210. throw new Exception(error);
  211. }
  212. } // end of stergeTabela()
  213.  
  214. public void modificaTabela(String tabela, String IDTabela, int ID, String[] campuri, String[] valori) throws SQLException, Exception {
  215. String update = "update " + tabela + " set ";
  216. String temp = "";
  217. if (con != null) {
  218. try {
  219. for (int i = 0; i < campuri.length; i++) {
  220. if (i != (campuri.length - 1)) {
  221. temp = temp + campuri[i] + "='" + valori[i] + "', ";
  222. } else {
  223. temp = temp + campuri[i] + "='" + valori[i] + "' where " + IDTabela + " = '" + ID + "';";
  224. }
  225. }
  226. update = update + temp;
  227. // create a prepared SQL statement
  228. Statement stmt;
  229. stmt = con.createStatement();
  230. stmt.executeUpdate(update);
  231. } catch (SQLException sqle) {
  232. error = "ExceptieSQL: Reactualizare nereusita; este posibil sa existe duplicate.";
  233. throw new SQLException(error);
  234. }
  235. } else {
  236. error = "Exceptie: Conexiunea cu baza de date a fost pierduta.";
  237. throw new Exception(error);
  238. }
  239. } // end of modificaTabela()
  240.  
  241. public ResultSet intoarceLinie(String tabela, int ID) throws SQLException, Exception {
  242. ResultSet rs = null;
  243. try {
  244. // Execute query
  245. String queryString = ("SELECT * FROM " + tabela + " where idpacient=" + ID + ";");
  246. Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  247. rs = stmt.executeQuery(queryString); //sql exception
  248. } catch (SQLException sqle) {
  249. error = "SQLException: Interogarea nu a fost posibila.";
  250. throw new SQLException(error);
  251. } catch (Exception e) {
  252. error = "A aparut o exceptie in timp ce se extrageau datele.";
  253. throw new Exception(error);
  254. }
  255. return rs;
  256. } // end of intoarceLinie()
  257.  
  258. public ResultSet intoarceLinieDupaId(String tabela, String denumireId, int ID) throws SQLException, Exception {
  259. ResultSet rs = null;
  260. try {
  261. // Execute query
  262. String queryString = ("SELECT * FROM " + tabela + " where " + denumireId + "=" + ID + ";");
  263. Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  264. rs = stmt.executeQuery(queryString); //sql exception
  265. } catch (SQLException sqle) {
  266. error = "SQLException: Interogarea nu a fost posibila. 453";
  267. throw new SQLException(error);
  268. } catch (Exception e) {
  269. error = "A aparut o exceptie in timp ce se extrageau datele.";
  270. throw new Exception(error);
  271. }
  272. return rs;
  273. } // end of intoarceLinieDupaId()
  274.  
  275. public ResultSet intoarceOperaId(int ID) throws SQLException, Exception {
  276. ResultSet rs = null;
  277. try {
  278. // Execute query
  279. String queryString = ("SELECT a.Titlu , a.ISBN , a.Editura EdituraCarte, b.Nume NumeAutor, b.Prenume PrenumeAutor, b.Nationalitate, c.idopera, c.idAutor, c.idCarte, c.Nume NumeOpera, c.Gen GenOpera, c.NrPagini from carti a, autori b, opera c where a.idCarte = c.idCarte and b.idAutor = c.idAutor and idopera = '" + ID + "';");
  280. Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  281. rs = stmt.executeQuery(queryString); //sql exception
  282. } catch (SQLException sqle) {
  283. error = "SQLException: Interogarea nu a fost posibila. rg";
  284. throw new SQLException(error);
  285. } catch (Exception e) {
  286. error = "A aparut o exceptie in timp ce se extrageau datele.";
  287. throw new Exception(error);
  288. }
  289. return rs;
  290. } // end of intoarceLinieDupaId()
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement