×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Yts 95
Added: Feb 28, 2021 1:26 PM
Views: 4927
Tags: no tags
  1. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  2.         JFileChooser chooser = new JFileChooser();
  3.         chooser.showOpenDialog(null);
  4.         File f = chooser.getSelectedFile();
  5.         String filename = f.getAbsolutePath();
  6.         path = filename;
  7.          newpath = path.replace('\\', '/');
  8.     }                                        
  9.  
  10.  
  11.  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  12.  
  13.         try{
  14.         File newpdf = new File(newpath);
  15.         FileInputStream fis = new FileInputStream(newpdf);
  16.         byte[] buff = new byte[2048000];
  17.         for(int readNum; (readNum=fis.read(buff)) !=-1 ; ){
  18.             baos.write(buff,0,readNum);
  19.         }
  20.  
  21.         userpdf=baos.toByteArray();
  22.  
  23.  
  24.     }
  25.     catch(Exception e){
  26.         JOptionPane.showMessageDialog(null, e);
  27.     }
  28.  PreparedStatement pstmt = null;
  29.  
  30.         try{
  31.              Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ppl","root","");
  32.              String proj_name = JOptionPane.showInputDialog("Please enter name of the file");
  33.              /*
  34.              String insert = "INSERT INTO project VALUES ('" + login.admission + "','" + login.yr + "','" + proj_name + "','" + userpdf + "')";
  35.  
  36.              java.sql.PreparedStatement pst = con.prepareStatement(insert);
  37.              pst.executeUpdate(insert);*/
  38.  
  39.              String sql = "INSERT INTO project"+"VALUES (?,?,?,?)";
  40.  
  41.  
  42.         pstmt = (PreparedStatement) con.prepareStatement(sql);
  43.         pstmt.setString(1, login.admission);
  44.         pstmt.setString(2, login.yr);
  45.         pstmt.setString(3, proj_name);
  46.         pstmt.setBlob(4, userpdf); //This line has an error may be because of userpdf.Plz //suggest
  47.  
  48.         pstmt.executeUpdate();
  49.  
  50.  
  51.         JOptionPane.showMessageDialog(null, "Saved");
  52.     }
  53.     catch(Exception e){
  54.         JOptionPane.showMessageDialog(null, e);
  55.     }

1 comment

ankita gupta 7 days ago
Can you post the complete code please?

Write a comment