Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2018
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.67 KB | None | 0 0
  1. package modelo;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.InputStream;
  6. import java.sql.*;
  7.  
  8. public class Registro {
  9. // ------------------------------ FIELDS ------------------------------
  10.  
  11.     private int cedula;
  12.     private String classfor = "oracle.jdbc.driver.OracleDriver";
  13.     private String clave = "campofe3750319";
  14.     private Connection con = null;
  15.     private String foto;
  16.     private InputStream imagen;
  17.     private String nombre;
  18.     private PreparedStatement pr = null;
  19.     private ResultSet rs = null;
  20.     private String url = "jdbc:oracle:thin:@localhost:1521:XE";
  21.     private String usuario = "root";
  22.  
  23. // --------------------- GETTER / SETTER METHODS ---------------------
  24.  
  25.     public int getCedula() {
  26.         return cedula;
  27.     }
  28.  
  29.     public void setCedula(int cedula) {
  30.         this.cedula = cedula;
  31.     }
  32.  
  33.     public String getFoto() {
  34.         return foto;
  35.     }
  36.  
  37.     public void setFoto(String foto) {
  38.         this.foto = foto;
  39.     }
  40.  
  41.     public InputStream getImagen() {
  42.         return imagen;
  43.     }
  44.  
  45.     public void setImagen(InputStream imagen) {
  46.         this.imagen = imagen;
  47.     }
  48.  
  49.     public String getNombre() {
  50.         return nombre;
  51.     }
  52.  
  53.     public void setNombre(String nombre) {
  54.         this.nombre = nombre;
  55.     }
  56.  
  57. // -------------------------- OTHER METHODS --------------------------
  58.  
  59.     public void insertar(int cedula, String nombre, String foto) {
  60.         String sql = "Insert into datos values(?,?,?)";
  61.         try {
  62.             Class.forName(classfor);
  63.             con = DriverManager.getConnection(url, usuario, clave);
  64.             /*
  65.             PreparedStatement stmt = con.prepareStatement("CREATE TABLE datos (ID INT PRIMARY KEY, NOMBRE VARCHAR(64))");
  66.             stmt.execute();
  67.             stmt.close();
  68.             */
  69.  
  70.             File archivo = new File("C:\\Users\\SKIMO\\Pictures\\wallpapers" + foto);
  71.             FileInputStream entrada = new FileInputStream(archivo);
  72.  
  73.             pr = con.prepareStatement(sql);
  74.             pr.setInt(1, cedula);
  75.             pr.setString(2, nombre);
  76.             pr.setBinaryStream(3, entrada, (int) archivo.length());
  77.             pr.executeUpdate();
  78.         } catch (Exception e) {
  79.             e.printStackTrace();
  80.         } finally {
  81.             if (con != null) {
  82.                 try {
  83.                     con.close();
  84.                 } catch (SQLException e) {
  85.                     e.printStackTrace();
  86.                 }
  87.             }
  88.         }
  89.     }
  90.  
  91.     private void commit() {
  92.         throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement