Guest User

Untitled

a guest
Jan 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7.  
  8. public class PruebasJDBC {
  9.  
  10. public static void main(String[] args) {
  11.  
  12. try {
  13.  
  14. Class.forName("com.mysql.jdbc.Driver");
  15. } catch (ClassNotFoundException e) {
  16. // TODO Auto-generated catch block
  17. e.printStackTrace();
  18. }
  19. Connection cx=null;
  20.  
  21. Libro l= new Libro(0,"El resplandor","Stephen King","El Susto",600,Seccion.TERROR,null);
  22. try {
  23. cx=DriverManager.getConnection("jdbc:mysql://localhost:3306/biblioteca","root","root");
  24.  
  25.  
  26. //STATEMENT (CUTRE)
  27. Statement st= cx.createStatement();//siempre java.sql
  28.  
  29. // st.executeUpdate("INSERT INTO libro " +
  30. // "(TITULO,AUTOR,EDITORIAL,NUMERO_PAGINAS,SECCION) " +
  31. // "VALUES " +
  32. // "('"+l.getTitulo()+"','"+l.getAutor()+"','"+l.getEditorial()+"',"+l.getNumeroDePaginas()+",'"+l.getSeccion()+"')");
  33.  
  34. //vital cerrar las conexiones
  35.  
  36. //PREPARED STATEMENT (FETEN)
  37.  
  38. Libro l2=new Libro (1,"El Resplandor","Stephen King, el loco","El Lobo",666,Seccion.TERROR,null);
  39. PreparedStatement pst= cx.prepareStatement("UPDATE libro " +
  40. "SET TITULO=? ,AUTOR=?, NUMERO_PAGINAS=? " +
  41. "WHERE ID_LIBRO=? ");
  42. pst.setString(1,l2.getTitulo());
  43. pst.setString(2,l2.getAutor());
  44. pst.setInt(3,l2.getNumeroDePaginas());
  45. pst.setInt(4,l2.getIdLibro());
  46. pst.executeUpdate();
  47.  
  48.  
  49. } catch (SQLException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }finally{
  53. try {
  54. cx.close();
  55. } catch (SQLException e) {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. }
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65. }
  66.  
  67. }
Add Comment
Please, Sign In to add comment