uploading a text file into a database

Ads
 

uploading a text file into a database

how to upload a text file into a database using jchooser

View Answers

April 24, 2012 at 4:28 PM

import java.io.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;

public class UploadTextFile {
     static File file;
        public static void main(String[] args) throws Exception {

               JLabel label=new JLabel("Choose File: ");
               final JTextField text=new JTextField(20);
               JButton b1=new JButton("Browse");
               JButton b2=new JButton("Upload");
               b1.addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent e){
                   JFileChooser chooser=new  JFileChooser();
               int returnVal = chooser.showOpenDialog(null);
               if(returnVal == JFileChooser.APPROVE_OPTION) {
                file = chooser.getSelectedFile();
                text.setText(file.getPath());
                   }
                   }
               });

               b2.addActionListener(new ActionListener(){
                   public void actionPerformed(ActionEvent e){
                       try{
                FileInputStream fis = new FileInputStream(file);
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
                PreparedStatement pstmt = conn.prepareStatement("insert into myfile( file_name, file_data) values ( ?, ?)");
                pstmt.setString(1, file.getName());
                pstmt.setBinaryStream(2, fis, (int) file.length());
                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(null,"Uploaded successfully to database");
                pstmt.close();
                conn.close();
                       }
                       catch(Exception ex){
                       System.out.println(ex);
                       }
                   }
               });
               JFrame f=new JFrame();
               f.setLayout(null);
               label.setBounds(10,10,100,20);
               text.setBounds(110,10,100,20);
               b1.setBounds(220,10,80,20);
               b2.setBounds(310,10,80,20);
               f.add(label);
               f.add(text);
               f.add(b1);
               f.add(b2);
               f.setVisible(true);
               f.setSize(450,200);               
        }
}

Ads









Related Tutorials/Questions & Answers:
uploading a text file into a database
uploading a text file into a database  how to upload a text file into a database using jchooser   import java.io.*; import java.sql.... { static File file; public static void main(String[] args) throws Exception
Uploading a Profile with image[file] into a sql database
Uploading a Profile with image[file] into a sql database  I need to upload a file along with the some text into a database ......[ Similar to http... , but upload file to database ] ... Pls help
Advertisements
Question about "Insert text file data into Database"
Question about "Insert text file data into Database"  Hey I was reading the tutorial "Insert text file data into Database", (awesome btw), and noticed that both a FileInputStream, a DataInputStream and a BufferedReader
file uploading
file uploading  How to upload music files onto the server and save the file information to the mysql database fetch and play the music files on a display page without download thus streaming the files
uploading a file
uploading a file  When I am trying to upload a file to another system in lan at a location "http://192.168.12.5:8080/tomcat-docs/myapps",then it is giving the following error message "http://192.168.12.5:8080/tomcat-docs/myapps
uploading audio file iphone
uploading audio file iphone  uploading audio file iphone
query related to uploading file
to save the uploading time and date in database. please help me it is urgent...query related to uploading file  hello friends i am doing my project in servlet and i want to upload a file and store file in local hard drive
Uploading a Software and storing in the database
Uploading a Software and storing in the database  I want to upload a software(may be of maximum 20mb) through JSP, and store it in the database. The coding present in the site for uploading and storing in the database
Uploading File on Server
Uploading File on Server  Hello, Can someone explain or suggest example. How do i uploading files on the FTP Server. Thanks
File Uploading Notification
File Uploading Notification  I am uploading files in my application and i want to know how can i know or be notified when file is uploaded. is there any file uploading event there which can tell me that process is going
Uploading and download pdf or .txt file.
Uploading and download pdf or .txt file.  I want admin user to upload pdf file into database and the users can download those pdf format from database
uploading file to tomcat server
uploading file to tomcat server  please tell me how to upload a file to the URL "http://192.168.12.7:8000/tomcat-docs/" ? thanks
upload a file into database and progrm should support excel and text and csv file formats
upload a file into database and progrm should support excel and text and csv file formats  Hai all, I need a program to upload a file into database table... and the program should support .excel ,.txt ,.csv file formats. can
File Uploading Problem
File Uploading Problem  I have a file uploading code but it create... = FileUpload.isMultipartContent(request); // Create a new file upload handler... { System.out.println("its a file"); System.out.println
File Uploading Problem
File Uploading Problem  I have a file uploading code but it create... = FileUpload.isMultipartContent(request); // Create a new file upload handler... { System.out.println("its a file"); System.out.println
file uploading using jsp
file uploading using jsp  below file uploading code has one error... = " + formDataLength); //String file = new String(dataBytes); //out.println("FileContents:" + file +""); byte[] line = new byte[128]; if (totalBytesRead
Problem in uploading image to to mysql database
="text"></td> </tr> <tr> <td><b>Choose the file...Problem in uploading image to to mysql database  Hi, need some help... be save in the database. and the image will also save in the desired folder. i
uploading a file - JSP-Interview Questions
uploading a file   uploading a file and storing in database using...; public File currentDir; public DropTarget target; JButton addButton...,BorderLayout.SOUTH); currentDir = new File(System.getProperty("user.dir")); } public
Problem in uploading image to to mysql database
Problem in uploading image to to mysql database  Hi, need some help... be save in the database. and the image will also save in the desired folder. i... the record to mysql database. heres my code... index.jsp <%@ page
Uploading a single file by using JSp
Uploading a single file by using JSp  u have said about submit button..but in program u have not used submit button..and where file will be stored..where should we specify the output folder name..   Visit Here
jsp with excel sheet data uploading into database
jsp with excel sheet data uploading into database  how to upload data in excel sheet with jsp into oracle 10g database
Getting path name of uploading file in struts 2.8.1
Getting path name of uploading file in struts 2.8.1  Hai! I am using struts 2.8.1. I want to upload file into database with the path name. How can I can get the original path name of the file in struts 2.8.1? It only display
Uploading file in servlet from a html form
Uploading file in servlet from a html form  Sir, I want to upload a picture from my html file and save it to my database as BLOB,but what JAR should i use for this purpose i am really confused about.And also is it possible to do
Uploading a single file by using JSp
Uploading a single file by using JSp  u have said about submit button..but in program u have not used submit button..and where file will be stored...;b>Choose the file To Upload:</b></td> <td><INPUT NAME
text file
text file  Hello can I modify the program below so that all the numerical data is stored in an external text file,that is the data contained in the array list.Thank you! mport java.util.*; import java.text.*; import
Getting path name of uploading file in struts 2.1.8
Getting path name of uploading file in struts 2.1.8  In my question I used struts 2.8.1 instead of 2.1.8. Sorry
what are the codes for uploading and downloading files in and from database?
what are the codes for uploading and downloading files in and from database?  We are designing a student's website where they can get and post study... the database. we will be very gratefull if you provide us those with a bit
text file
text file  Hi can you help me I have to modify the program below so that all the data held in it is stored in an external text file.So there should... at the start of the program from a seperate external text file.Thank you! mport
uploading a file at another system in lan using jsp
uploading a file at another system in lan using jsp  Thanks for the code at "http://www.roseindia.net/jsp/fileupload/Sinleupload.xhtml.shtml... to save or upload the file at another system in lan at location "http
Reading a text file in java
Reading a text file in java  What is the code for Reading a text file... in java.io.* package for reading and writing to a file in Java. To learn more about reading text file in Java see the tutorial Read File in Java. Thanks
Convert Text File to PDF file
Convert Text File to PDF file  Here is the way how to covert your Text file to PDF File, public class TextFileToPDF { private static void...(inLine); System.out.println("Text is inserted into pdf file

Ads