Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class PruebasJDBC {
- public static void main(String[] args) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Connection cx=null;
- Libro l= new Libro(0,"El resplandor","Stephen King","El Susto",600,Seccion.TERROR,null);
- try {
- cx=DriverManager.getConnection("jdbc:mysql://localhost:3306/biblioteca","root","root");
- //STATEMENT (CUTRE)
- Statement st= cx.createStatement();//siempre java.sql
- // st.executeUpdate("INSERT INTO libro " +
- // "(TITULO,AUTOR,EDITORIAL,NUMERO_PAGINAS,SECCION) " +
- // "VALUES " +
- // "('"+l.getTitulo()+"','"+l.getAutor()+"','"+l.getEditorial()+"',"+l.getNumeroDePaginas()+",'"+l.getSeccion()+"')");
- //vital cerrar las conexiones
- //PREPARED STATEMENT (FETEN)
- Libro l2=new Libro (1,"El Resplandor","Stephen King, el loco","El Lobo",666,Seccion.TERROR,null);
- PreparedStatement pst= cx.prepareStatement("UPDATE libro " +
- "SET TITULO=? ,AUTOR=?, NUMERO_PAGINAS=? " +
- "WHERE ID_LIBRO=? ");
- pst.setString(1,l2.getTitulo());
- pst.setString(2,l2.getAutor());
- pst.setInt(3,l2.getNumeroDePaginas());
- pst.setInt(4,l2.getIdLibro());
- pst.executeUpdate();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- try {
- cx.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment