Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.05 KB | None | 0 0
  1. -----------------------------conexion------------------------
  2.  
  3. package com.conexion;
  4. import java.sql.*;
  5.  
  6. /**
  7. * Nombre de la clase: Conexion
  8. * @author Isaac
  9. * Copyright Isaac
  10. * Fecha: 09/08/2017
  11. *
  12. */
  13. public class Conexion {
  14. private Connection con;
  15.  
  16. public Connection getCon() {
  17. return con;
  18. }
  19.  
  20. public void setCon(Connection con) {
  21. this.con = con;
  22. }
  23.  
  24.  
  25. public void conectar() throws Exception
  26. {
  27. try {
  28. Class.forName("com.mysql.jdbc.Driver");
  29. con=DriverManager.getConnection("jdbc:mysql://localhost:3306/bdBiblioteca?user=root&password=");
  30. } catch (Exception e) {
  31. throw e;
  32. }
  33.  
  34. }
  35.  
  36. public void desconectar() throws Exception
  37. {
  38. try
  39. {
  40. if(con!=null)
  41. {
  42. if(con.isClosed()==false)
  43. {
  44. con.close();
  45. }
  46. }
  47. } catch (Exception e) {
  48. }
  49. }
  50.  
  51. }
  52.  
  53.  
  54. ------------------------daobiblioteca------------------------
  55.  
  56. package com.dao;
  57.  
  58. import com.conexion.Conexion;
  59. import com.modelo.Biblioteca;
  60. import java.util.*;
  61. import java.sql.*;
  62.  
  63. /**
  64. * Nombre de la clase: DaoBiblioteca
  65. * @author Isaac
  66. * Copyright Isaac
  67. * Fecha: 09/08/2017
  68. *
  69. */
  70. public class DaoBiblioteca extends Conexion {
  71.  
  72. public void insertarBiblioteca (Biblioteca bi) throws Exception
  73. {
  74. try
  75. {
  76. this.conectar();
  77. String sql="insert into biblioteca(titulo, autor,editorial,precio) values(?,?,?,?)";
  78. PreparedStatement pre=this.getCon().prepareStatement(sql);
  79. pre.setString(1,bi.getTitulo());
  80. pre.setString(2, bi.getAutor());
  81. pre.setString(3, bi.getEditorial());
  82. pre.setDouble(4, bi.getPrecio());
  83. pre.executeUpdate();
  84.  
  85. } catch (Exception e) {
  86. throw e;
  87. }
  88. finally
  89. {
  90. this.desconectar();
  91. }
  92. }
  93.  
  94. public List mostrarBiblioteca() throws Exception
  95. {
  96. List listaBiblioteca=new ArrayList();
  97. ResultSet res;
  98. try
  99. {
  100. this.conectar();
  101. String sql="select * from biblioteca";
  102. PreparedStatement pre=this.getCon().prepareCall(sql);
  103. res=pre.executeQuery();
  104. while(res.next())
  105. {
  106. Biblioteca bi=new Biblioteca();
  107. bi.setId(res.getInt("idLibro"));
  108. bi.setTitulo(res.getString("titulo"));
  109. bi.setAutor(res.getString("autor"));
  110. bi.setEditorial(res.getString("editorial"));
  111. bi.setPrecio(res.getDouble("precio"));
  112. listaBiblioteca.add(bi);
  113. }
  114. } catch (Exception e) {
  115. throw e;
  116. }
  117. finally
  118. {
  119. this.desconectar();
  120. }
  121. return listaBiblioteca;
  122. }
  123.  
  124. public void modificarBiblioteca(Biblioteca bi) throws Exception
  125. {
  126. try {
  127. this.conectar();
  128. String sql="update biblioteca set titulo=?, autor=?, editorial=?,precio=? where idLibro=?";
  129. PreparedStatement pre=this.getCon().prepareStatement(sql);
  130. pre.setString(1, bi.getTitulo());
  131. pre.setString(2, bi.getAutor());
  132. pre.setString(3, bi.getEditorial());
  133. pre.setDouble(4, bi.getPrecio());
  134. pre.setInt(5, bi.getId());
  135. pre.executeUpdate();
  136. } catch (Exception e) {
  137. throw e;
  138. }
  139. finally{
  140. this.desconectar();
  141. }
  142.  
  143. }
  144. public void eliminarBiblioteca (Biblioteca bi) throws Exception
  145. {
  146. try
  147. {
  148. this.conectar();
  149. String sql="delete from biblioteca where idLibro=?";
  150. PreparedStatement pre= this.getCon().prepareStatement(sql);
  151. pre.setInt(1, bi.getId());
  152. pre.executeUpdate();
  153.  
  154. } catch (Exception e) {
  155. throw e;
  156. }
  157. finally{
  158. this.desconectar();
  159. }
  160. }
  161. }
  162. ------------------------------------biblioteca-------------------
  163.  
  164. package com.modelo;
  165.  
  166. /**
  167. * Nombre de la clase: Biblioteca
  168. * @author Isaac
  169. * Copyright Isaac
  170. * Fecha: 09/08/2017
  171. *
  172. */
  173. public class Biblioteca {
  174. private int id;
  175. private String titulo;
  176. private String autor;
  177. private String editorial;
  178. private double precio;
  179.  
  180. public Biblioteca() {
  181. }
  182.  
  183. public int getId() {
  184. return id;
  185. }
  186.  
  187. public void setId(int id) {
  188. this.id = id;
  189. }
  190.  
  191. public String getTitulo() {
  192. return titulo;
  193. }
  194.  
  195. public void setTitulo(String titulo) {
  196. this.titulo = titulo;
  197. }
  198.  
  199. public String getAutor() {
  200. return autor;
  201. }
  202.  
  203. public void setAutor(String autor) {
  204. this.autor = autor;
  205. }
  206.  
  207. public String getEditorial() {
  208. return editorial;
  209. }
  210.  
  211. public void setEditorial(String editorial) {
  212. this.editorial = editorial;
  213. }
  214.  
  215. public double getPrecio() {
  216. return precio;
  217. }
  218.  
  219. public void setPrecio(double precio) {
  220. this.precio = precio;
  221. }
  222.  
  223. }
  224.  
  225. --------------------------------formulario----------------------
  226. package com.vista;
  227. import com.dao.DaoBiblioteca;
  228. import com.modelo.Biblioteca;
  229. import java.text.*;
  230. import java.util.*;
  231. import javax.swing.JOptionPane;
  232. import javax.swing.table.DefaultTableModel;
  233.  
  234.  
  235. /**
  236. * Nombre de la clase: FrmBiblioteca
  237. * @author Isaac
  238. * Copyright Isaac
  239. * Fecha: 09/08/2017
  240. *
  241. */
  242. public class FrmBiblioteca extends javax.swing.JFrame {
  243.  
  244. public FrmBiblioteca() {
  245. initComponents();
  246. tablaE();
  247. }
  248. Biblioteca bi=new Biblioteca();
  249. DaoBiblioteca daob=new DaoBiblioteca();
  250.  
  251. public void tablaE(){
  252. String [] columnas={"Id","Titulo","Autor","Editorial","Precio"};
  253. Object[] obj=new Object[5];
  254. DefaultTableModel tabla=new DefaultTableModel(null, columnas);
  255. List ls;
  256. DecimalFormat df=new DecimalFormat("#,###.00");
  257. try
  258. {
  259. ls=daob.mostrarBiblioteca();
  260. for(int i=0;i<ls.size();i++)
  261. {
  262. bi=(Biblioteca)ls.get(i);
  263. obj[0]=bi.getId();
  264. obj[1]=bi.getTitulo();
  265. obj[2]=bi.getAutor();
  266. obj[3]=bi.getEditorial();
  267. obj[4]=df.format(bi.getPrecio());
  268. tabla.addRow(obj);
  269. }
  270.  
  271. ls=daob.mostrarBiblioteca();
  272. this.jTbBiblioteca.setModel(tabla);
  273. }
  274. catch (Exception e)
  275. {
  276. JOptionPane.showMessageDialog(this, "Error al mostrar datos"+e.toString());
  277.  
  278. }
  279. }
  280.  
  281. public void insertar() throws Exception
  282. {
  283. bi.setId(ICONIFIED);
  284. bi.setTitulo(this.jTxtTitulo.getText());
  285. bi.setAutor(this.jTxtAutor.getText());
  286. bi.setEditorial(this.jTxtEditorial.getText());
  287. bi.setPrecio(Double.parseDouble(this.jTxtPrecio.getText()));
  288. daob.insertarBiblioteca(bi);
  289. JOptionPane.showMessageDialog(null, "Datos insertados correctamente");
  290. daob.mostrarBiblioteca();
  291. }
  292.  
  293. public void limpiar()
  294. {
  295. this.jtxtId.setText("");
  296. this.jTxtAutor.setText("");
  297. this.jTxtEditorial.setText("");
  298. this.jTxtPrecio.setText("");
  299. this.jTxtTitulo.setText("");
  300. }
  301.  
  302. public void modificar()
  303. {
  304. try
  305. {
  306. bi.setId(Integer.parseInt(this.jtxtId.getText()));
  307. bi.setTitulo(this.jTxtTitulo.getText());
  308. bi.setAutor(this.jTxtAutor.getText());
  309. bi.setEditorial(this.jTxtEditorial.getText());
  310. bi.setPrecio(Double.parseDouble(this.jTxtPrecio.getText()));
  311. int SiONo=JOptionPane.showConfirmDialog(this, "Desea modificar el libro", "Modificar Libro",JOptionPane.YES_NO_OPTION);
  312. if(SiONo==0)
  313. {
  314. daob.modificarBiblioteca(bi);
  315. JOptionPane.showMessageDialog(rootPane, "Libro modificado correctamente","Confirmacion", JOptionPane.INFORMATION_MESSAGE);
  316. tablaE();
  317. limpiar();
  318. }
  319. else
  320. {
  321. limpiar();
  322. }
  323. } catch (Exception e) {
  324. e.printStackTrace();
  325. }
  326. }
  327.  
  328. public void eliminar()
  329. {
  330. try
  331. {
  332. bi.setId(Integer.parseInt(this.jtxtId.getText()));
  333. int SiONo=JOptionPane.showConfirmDialog(this, "Desea eliminar el libro", "Confirmacion", JOptionPane.YES_NO_OPTION);
  334. if(SiONo==0)
  335. {
  336. daob.eliminarBiblioteca(bi);
  337. JOptionPane.showMessageDialog(rootPane, "Eliminado con exito","Confirmacion",JOptionPane.INFORMATION_MESSAGE);
  338. tablaE();
  339. limpiar();
  340. }
  341. else
  342. {
  343. limpiar();
  344. }
  345.  
  346. } catch (Exception e) {
  347. JOptionPane.showMessageDialog(rootPane, e.toString(),"Error",JOptionPane.ERROR_MESSAGE);
  348. }
  349. }
  350.  
  351. public void llenaTabla()
  352. {
  353. int fila=this.jTbBiblioteca.getSelectedRow();
  354. this.jtxtId.setText(String.valueOf(this.jTbBiblioteca.getValueAt(fila, 0)));
  355. this.jTxtTitulo.setText(String.valueOf(this.jTbBiblioteca.getValueAt(fila, 1)));
  356. this.jTxtAutor.setText(String.valueOf(this.jTbBiblioteca.getValueAt(fila, 2)));
  357. this.jTxtEditorial.setText(String.valueOf(this.jTbBiblioteca.getValueAt(fila, 3)));
  358. this.jTxtPrecio.setText(String.valueOf(this.jTbBiblioteca.getValueAt(fila, 4)));
  359. }
  360.  
  361. private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
  362. try {
  363. insertar();
  364. } catch (Exception e) {
  365. e.printStackTrace();
  366. }
  367. tablaE();
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement