Advertisement
Guest User

Untitled

a guest
Oct 8th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.*;
  3. class StoreImage
  4.  
  5. {
  6.   public static void main(String args[])
  7.   {
  8.    
  9.     Connection con = null;
  10.     ResultSet  rs  = null;
  11.     PreparedStatement stmt = null;
  12.     FileInputStream fis;
  13.    
  14.     try
  15.     {
  16.       Class.forName("org.postgresql.Driver");
  17.       String url      = "jdbc:postgresql://localhost:5432/postgres";
  18.       String user     = "postgres";
  19.       String password = "postgres";
  20.       Connection con  = DriverManager.getConnection(url, user, password);
  21.      
  22.       File image = new File("/home/postgres/pgimage/e1.jpeg");
  23.       stmt = con.prepareStatement("insert into empimg(empno, img) "+ "values(?,?)");
  24.      
  25.       stmt.setInt(1,1);
  26.       fis = new FileInputStream(image);
  27.       stmt.setBinaryStream(2, (InputStream)fis, (int)(image.length()));
  28.      
  29.       int s = stmt.executeUpdate();
  30.       if (s>0) {
  31.       System.out.println("Uploaded Successfully !");
  32.       }      
  33.       }
  34.      }
  35.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement