|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package self_encryption; |
| 7 | +import java.io.File; |
| 8 | +import java.io.FileInputStream; |
| 9 | +import java.io.FileNotFoundException; |
| 10 | +import java.io.FileOutputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.sql.Connection; |
| 14 | +import java.sql.DriverManager; |
| 15 | +import java.sql.SQLException; |
| 16 | +import java.sql.Statement; |
| 17 | +import java.sql.PreparedStatement; |
| 18 | +import java.sql.ResultSet; |
| 19 | +/** |
| 20 | + * |
| 21 | + * @author Ayush |
| 22 | + */ |
| 23 | +public class DatabaseConnectivity { |
| 24 | + String user,pass,url; |
| 25 | + Connection conn; |
| 26 | + Statement stmt; |
| 27 | + DatabaseConnectivity(String user,String pass,String url){ |
| 28 | + this.pass=pass; |
| 29 | + this.url=url; |
| 30 | + this.user=user; |
| 31 | + } |
| 32 | + void connect() throws ClassNotFoundException, SQLException{ |
| 33 | + Class.forName("com.mysql.cj.jdbc.Driver"); |
| 34 | + conn = DriverManager.getConnection(this.url, this.user,this.pass); |
| 35 | + stmt = conn.createStatement(); |
| 36 | + System.out.println("Database connection established"); |
| 37 | + |
| 38 | + } |
| 39 | + @SuppressWarnings("ConvertToTryWithResources") |
| 40 | + void storeToDB(String src,String enc) throws FileNotFoundException, IOException{ |
| 41 | + try { |
| 42 | + stmt.execute("create table if not exists filetable(filename varchar(40) not null primary key,data LONGBLOB)"); |
| 43 | + |
| 44 | + PreparedStatement ps=conn.prepareStatement("insert into filetable values(?,?)"); |
| 45 | + |
| 46 | + FileInputStream fin=new FileInputStream(enc); |
| 47 | + ps.setString(1,src); |
| 48 | + ps.setBinaryStream(2, fin); |
| 49 | + int i=ps.executeUpdate(); |
| 50 | + fin.close(); |
| 51 | + |
| 52 | + System.out.println("("+src+") file encrypted and stored in database"); |
| 53 | + |
| 54 | + //delete encrypted file after storing it to database |
| 55 | + File file=new File(enc); |
| 56 | + if(file.delete()){ |
| 57 | + System.out.println("Encrypted src - "+enc+" file deleted successfully!!!"); |
| 58 | + }else{ |
| 59 | + System.out.println("Failed to delete."); |
| 60 | + } |
| 61 | + } catch (SQLException ex) { |
| 62 | + System.out.println("\t"+ex.getMessage()); |
| 63 | + } |
| 64 | + } |
| 65 | + @SuppressWarnings("ConvertToTryWithResources") |
| 66 | + boolean retreiveFromDB(String src,String enc) throws FileNotFoundException, IOException{ |
| 67 | + boolean found=true; |
| 68 | + try { |
| 69 | + PreparedStatement ps_stmt = conn.prepareStatement("select data from filetable where filename=?"); |
| 70 | + ps_stmt.setString(1, src); |
| 71 | + ResultSet rs=ps_stmt.executeQuery(); |
| 72 | + if(rs.next()==false){ |
| 73 | + System.out.println("No such file found in Database."); |
| 74 | + found=false; |
| 75 | + }else{ |
| 76 | + FileOutputStream fout = new FileOutputStream(enc); |
| 77 | + |
| 78 | + do{ |
| 79 | + InputStream input = rs.getBinaryStream("data"); |
| 80 | + input.transferTo(fout); |
| 81 | + }while(rs.next()); |
| 82 | + fout.close(); |
| 83 | + } |
| 84 | + } catch (SQLException ex) { |
| 85 | +// Logger.getLogger(DatabaseConnectivity.class.getName()).log(Level.SEVERE, null, ex); |
| 86 | + System.out.println(ex.getMessage()); |
| 87 | + }finally{ |
| 88 | + return found; |
| 89 | + } |
| 90 | + } |
| 91 | + void disconnect() throws SQLException{ |
| 92 | + conn.close(); |
| 93 | + } |
| 94 | +} |
0 commit comments