Advertisement
Guest User

Untitled

a guest
Jan 4th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.62 KB | None | 0 0
  1. package CBBDD.CBBDD;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.sql.Statement;
  7. import java.sql.Connection;
  8. import java.sql.DriverManager;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.util.Calendar;
  13. import java.sql.Timestamp;
  14. import java.text.ParseException;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Date;
  17.  
  18. /**
  19. * @author mortizf
  20. */
  21.  
  22. public class HolaMundoBaseDeDatos {
  23. public static Connection conn = null;
  24. public static Statement stmt = null;
  25. public static Calendar cal;
  26. public static Timestamp ts;
  27. public static String fich = "C:\\Users\\morti\\OneDrive\\Escritorio\\Ficheros\\Moviments.txt";
  28.  
  29. public static void main(String[] args) throws SQLException, IOException, ParseException {
  30.  
  31. String cadena;
  32. FileReader f = new FileReader(fich);
  33. BufferedReader b = new BufferedReader(f);
  34. String[] datoslinea;
  35. SimpleDateFormat fechaSimple = new SimpleDateFormat("yyyy-MM-dd");
  36. Date dmov;
  37. java.sql.Date sqldmov;
  38. int idvehicle = 0;
  39. int idcontribuent = 0;
  40. int idve = 0;
  41. int contador = 0;
  42.  
  43. try {
  44. Class.forName("com.mysql.jdbc.Driver");
  45. } catch (ClassNotFoundException e) {
  46. System.err.println("El driver no se encuentra");
  47. System.exit(-1);
  48. }
  49. try {
  50. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "");
  51. stmt = conn.createStatement();
  52.  
  53. while ((cadena = b.readLine()) != null) {
  54. datoslinea = cadena.split("\\|");
  55. for (int i = 0; i < datoslinea.length; i++) {
  56. System.out.println(datoslinea[i]);
  57. }
  58. if (contador != 0) {
  59. if (datoslinea != null) {
  60.  
  61. dmov = fechaSimple.parse(datoslinea[2]);
  62. sqldmov = new java.sql.Date(dmov.getTime());
  63. // Falta afegir al exercici ¡¡¡¡¡¡¡¡¡¡ I A LA BASE DE DADES !!!!!!!! el nom de
  64. // fitxer i la data d'importació (data del sistema)
  65. String movi = "INSERT INTO Moviments (ABM,TIPUS,DATA,MATRICULA,N_BASTIDOR,N_MOTOR,DNI,COGNOM_NOM,ADREÇA) VALUES (?,?,?,?,?,?,?,?,?)";
  66. PreparedStatement psmovi = conn.prepareStatement(movi);
  67. cal = Calendar.getInstance();
  68. ts = new java.sql.Timestamp(cal.getTime().getTime());
  69.  
  70. psmovi.setString(1, datoslinea[0]);
  71. psmovi.setString(2, datoslinea[1]);
  72. psmovi.setDate(3, sqldmov);
  73. psmovi.setString(4, datoslinea[3]);
  74. psmovi.setInt(5, Integer.valueOf(datoslinea[4]));
  75. psmovi.setInt(6, Integer.valueOf(datoslinea[5]));
  76. psmovi.setString(7, datoslinea[6]);
  77. psmovi.setString(8, datoslinea[7]);
  78. psmovi.setString(9, datoslinea[8]);
  79.  
  80. psmovi.executeUpdate();
  81. psmovi.close();
  82.  
  83. if (datoslinea[0].equals("A")) {// CREO QUE HE ENTENDIDO MAL LO DEL ALTA
  84.  
  85. NouVehicle(datoslinea);
  86. NouContribuent(datoslinea);
  87. NouHistoric(datoslinea);
  88.  
  89. } else if (datoslinea[0].equals("M")) {// FALTA HACER EL HISTÓRICO
  90.  
  91. if (datoslinea[1].equals("CD")) { // Actualizar domicilio
  92. String upcontri = "UPDATE Contribuents SET DOMICILI = ? WHERE NIF = ?";
  93. PreparedStatement psupcontri = conn.prepareStatement(upcontri);
  94.  
  95. psupcontri.setString(1, datoslinea[8]);
  96. psupcontri.setString(2, datoslinea[6]);
  97.  
  98. psupcontri.executeUpdate();
  99. psupcontri.close();
  100. } else if (datoslinea[1].equals("CP")) { // FALTA Posar data de baixa en el historic
  101. // (UPDATE), crear
  102. // un nou registre a la taula Historic amb el
  103. // nou
  104. // propietari i el vehicle (INSERT)
  105. NouContribuent(datoslinea);
  106.  
  107. ResultSet rscoh = stmt.executeQuery(
  108. "SELECT idVe FROM Vehicles WHERE Matricula = '" + datoslinea[3] + "'");
  109. if (rscoh.next()) {
  110. idve = rscoh.getInt(1);
  111. }
  112. cal = Calendar.getInstance();
  113. ts = new java.sql.Timestamp(cal.getTime().getTime());
  114. String uphis = "UPDATE Historic SET Data_Baixa = ? WHERE idVe = ?";
  115. PreparedStatement psuphis = conn.prepareStatement(uphis);
  116.  
  117. psuphis.setTimestamp(1, ts);
  118. psuphis.setInt(2, idve);
  119. psuphis.executeUpdate();
  120. psuphis.close();
  121. NouHistoric(datoslinea);
  122. }
  123. } else if (datoslinea[0].equals("B")) {// FALTA HACER EL HISTÓRICO
  124. if (datoslinea[1].equals("BD")) {// Falta data de baixa historic (UPDATE)
  125. UpdateVehicle(datoslinea);
  126. ResultSet rs2 = stmt.executeQuery(
  127. "SELECT idVe FROM Vehicles WHERE Matricula = '" + datoslinea[3] + "'");
  128. if (rs2.next()) {
  129. idvehicle = rs2.getInt(1);
  130. }
  131. cal = Calendar.getInstance();
  132. ts = new java.sql.Timestamp(cal.getTime().getTime());
  133. String uphis = "UPDATE Historic SET Data_Baixa = ? WHERE idVe = ?";
  134. PreparedStatement psuphis = conn.prepareStatement(uphis);
  135.  
  136. psuphis.setTimestamp(1, ts);
  137. psuphis.setInt(2, idvehicle);
  138.  
  139. psuphis.executeUpdate();
  140. psuphis.close();
  141. } else if (datoslinea[1].equals("BT")) {
  142. UpdateVehicle(datoslinea);
  143. }
  144. }
  145.  
  146. } else {
  147. System.out.println("la ultima linea esta mal");
  148. }
  149.  
  150. }
  151. contador++;
  152. }
  153. b.close();
  154. stmt.close();
  155. } catch (SQLException e) {
  156. System.err.println("Error en la base de datos: " + e.getMessage());
  157. e.printStackTrace();
  158. } finally {
  159. if (conn != null) {
  160. try {
  161. conn.close();
  162. } catch (SQLException e) {
  163. System.err.println("Error al cerrar la conexión: " + e.getMessage());
  164. }
  165. }
  166. }
  167. }
  168.  
  169. public static void UpdateVehicle(String[] datoslinea) throws SQLException {
  170. cal = Calendar.getInstance();
  171. ts = new java.sql.Timestamp(cal.getTime().getTime());
  172. String upvehitemp = "UPDATE Vehicles SET Tipus_Baixa = ?, Data_Baixa = ? WHERE Matricula = ?";
  173. PreparedStatement psupvehitemp = conn.prepareStatement(upvehitemp);
  174.  
  175. psupvehitemp.setString(1, datoslinea[1]);
  176. psupvehitemp.setTimestamp(2, ts);
  177. psupvehitemp.setString(3, datoslinea[3]);
  178.  
  179. psupvehitemp.executeUpdate();
  180. psupvehitemp.close();
  181. }
  182.  
  183. public static void NouContribuent(String[] datoslinea) throws SQLException {
  184. ResultSet rscontri = stmt.executeQuery("SELECT NIF FROM Contribuents WHERE NIF = '" + datoslinea[6] + "'");
  185. if (rscontri.isBeforeFirst()) {
  186. System.out.println("L'element ja existeix");
  187. } else {
  188. String contri = "INSERT INTO Contribuents (NIF,NOM,DOMICILI) VALUES (?,?,?)";
  189. PreparedStatement pscontri = conn.prepareStatement(contri);
  190.  
  191. pscontri.setString(1, datoslinea[6]);
  192. pscontri.setString(2, datoslinea[7]);
  193. pscontri.setString(3, datoslinea[8]);
  194.  
  195. pscontri.executeUpdate();
  196. pscontri.close();
  197. rscontri.close();
  198. }
  199. }
  200.  
  201. public static void NouHistoric(String[] datoslinea) throws SQLException {
  202.  
  203. int idcontribuent = 0;
  204. int idvehicle = 0;
  205. ResultSet rsv = stmt.executeQuery("SELECT idVe FROM Vehicles WHERE Matricula = '" + datoslinea[3] + "'");
  206. if (rsv.next()) {
  207. idvehicle = rsv.getInt(1);
  208. }
  209. rsv.close();
  210. ResultSet rsc = stmt.executeQuery("SELECT idCon FROM Contribuents WHERE NIF = '" + datoslinea[6] + "'");
  211.  
  212. if (rsc.next()) {
  213. idcontribuent = rsc.getInt(1);
  214. }
  215.  
  216. rsc.close();
  217. ResultSet rshisto = stmt
  218. .executeQuery("SELECT * FROM historic WHERE idCon = " + idcontribuent + " AND idVe = " + idvehicle);
  219. if (rshisto.isBeforeFirst()) {
  220. System.out.println("L'element ja existeix");
  221. } else {
  222. cal = Calendar.getInstance();
  223. ts = new java.sql.Timestamp(cal.getTime().getTime());
  224.  
  225. String his = "INSERT INTO Historic (idCon,idVe,Data_Alta) VALUES (?,?,?)";
  226. PreparedStatement pshis = conn.prepareStatement(his);
  227.  
  228. pshis.setInt(1, idcontribuent);
  229. pshis.setInt(2, idvehicle);
  230. pshis.setTimestamp(3, ts);
  231.  
  232. pshis.executeUpdate();
  233. pshis.close();
  234. rshisto.close();
  235. }
  236. }
  237.  
  238. public static void NouVehicle(String[] datoslinea) throws SQLException {
  239.  
  240. ResultSet rsve = stmt.executeQuery("SELECT Matricula FROM Vehicles WHERE Matricula = '" + datoslinea[3] + "'");
  241. if (rsve.isBeforeFirst()) {
  242. System.out.println("L'element ja existeix");
  243. } else {
  244. cal = Calendar.getInstance();
  245. ts = new java.sql.Timestamp(cal.getTime().getTime());
  246. String vehi = "INSERT INTO Vehicles (Matricula,N_Motor,N_Bastidor,Data_Alta) VALUES (?,?,?,?)";
  247. PreparedStatement psvehi = conn.prepareStatement(vehi);
  248.  
  249. psvehi.setString(1, datoslinea[3]);
  250. psvehi.setInt(2, Integer.valueOf(datoslinea[4]));
  251. psvehi.setInt(3, Integer.valueOf(datoslinea[5]));
  252. psvehi.setTimestamp(4, ts);
  253.  
  254. psvehi.executeUpdate();
  255. psvehi.close();
  256. rsve.close();
  257.  
  258. }
  259. }
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement