Advertisement
Guest User

Untitled

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