Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet upload the text file through html and insert those data into the table
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


naveen
upload the text file through html and insert those data into the table
1 Answer(s)      2 years and 8 months ago
Posted in : JSP-Servlet


i want to take input form a text file.there is a table in database oracle contains lot of columns.text file also contains lot of data to insert into table.now i want to upload the text file through html and insert those data into the table.for this i want to write servlet program.can anyone helpme how to write the code to insert data from text file


Advertisement
View Answers

April 25, 2012 at 4:59 PM


1)page.jsp:

<HTML>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD> 
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<br><br><br>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr><center><td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td></tr>
<tr><td colspan="2" align="center"> </td></tr>
<tr><td><b>Choose the file To Upload:</b></td><td><INPUT NAME="file" TYPE="file"></td></tr>
<tr><td colspan="2" align="center"> </td></tr>
<tr><td colspan="2" align="center"><input type="submit" value="Send File"> </td></tr>
<table>
</center> 
</FORM>
</BODY>
</HTML>

2)upload.jsp:

<%@ page import="java.io.*,java.sql.*" %>
<%
String saveFile="";
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
saveFile="C:/UploadedFiles/"+saveFile;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully upload the file by the name of:</b>
<% out.println(saveFile);%></td></tr></table>
<%

try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/register", "root", "root");
Statement st=con.createStatement();
File f=new File(saveFile);
FileInputStream fstream = new FileInputStream(f);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
ArrayList list=new ArrayList();
while ((strLine = br.readLine()) != null){
list.add(strLine);
}
Iterator itr;
for (itr=list.iterator(); itr.hasNext(); ){
String str=itr.next().toString();
String [] splitSt =str.split(" ");
String id="",name="",course="",deptt="";
for (int i = 0 ; i < splitSt.length ; i++) {
id=splitSt[0];
name=splitSt[1];
course=splitSt[2];
deptt=splitSt[3];
}

int k=st.executeUpdate("insert into student(id,name,course,department) values('"+id+"','"+name+"','"+course+"','"+deptt+"')");

}
out.println("Uploaded successfully !");

}
catch(Exception e){}
}
%>

Here we have upload a text file that consists of four fields(id,name,course,department).



Related Tutorials/Questions & Answers:
upload the text file through html and insert those data into the table
upload the text file through html and insert those data into the table ... into table.now i want to upload the text file through html and insert those data... the code to insert data from text file   1)page.jsp: <HTML> <
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
Advertisements
To Upload and insert the CSV file into Database
to upload a CSV file through JSP and insert it into the database. For this, we have... table file (   'file_id' int ,   'file_datatext... To Upload and insert the CSV file into Database
Insert text file data into Database
Insert text file data into Database In this section, you will learn how to insert the text file data into the database. For this purpose, we have created... the data to the ArrayList. This list is then iterated through the Iterator
Ant Script to Insert Data in Mysql Table
Ant Script to Insert Data in Mysql Table       This example illustrates how to insert data in table through the build.xml file by simply running the ant command
To Upload and insert the file into Database with Current Date and Time In JSP
; In this tutorial, you will learn how to upload a file through JSP and insert... file upload and insert into database with current date and time. Table Structure...` bigint(20) NOT NULL auto_increment, `file_data` text, `file_date` datetime
Java insert file data to JTable
); } }  Through the above code, you can insert the file data into JTable...Java insert file data to JTable In this section, you will learn how to insert text file data into JTable. Swing has provide useful and sophisticated set
Insert Data From File
to insert data from file. To understand this example, we create a table 'Stu_Table... Insert Data From File       Insert Data From File is  used to insert data from file
how to read data from excel file through browse and insert into oracle database using jsp or oracle???
how to read data from excel file through browse and insert into oracle database... a browse button which can upload a excelfile and after uploading the data should..., Please go through the following links may this will be helpful for you. However
HTML file upload example
HTML file upload example Here is an example of File Upload in html... that adds the data and display it on the screen. When we save the data and the file...;<center><font size=4><b>File Upload in HTML</b><
Insert Data From File
to insert data from file. To understand this example, we create a table 'Stu_Table... Insert Data From File       Insert Data From File is  used to insert data from file
to upload a table from DB to the same html page
to upload a table from DB to the same html page  how to make this booklist table to be opened in the same html page itself using a third frame
file upload and insert name into database - JSP-Servlet
file upload and insert name into database  Hi, I just joined as a fresher and was given task to upload a file and insert its name into database...  HIread for more information,http://www.roseindia.net/jsp/file_upload
reading the records from a .xlsx file and storing those records in database table
reading the records from a .xlsx file and storing those records in database... file and store that records in database table. I tried like this public class...); String query="insert into student values("+id.trim()+",'"+name.trim
Write Text File to Table
the records of a simple text file and write (insert) into a simple table in MySQL database. All records are written in the simple text file and again, if you need... Write Text File to Table     
upload file and insert other details to database using a single form
upload file and insert other details to database using a single form   hi.. I have problem with uploading a file and insert other user datas together which I retrieved from another jsp/html page. Here i was able to upload file
How to read every cell of an excel sheet using Apache POI and insert those data into a DB?
How to read every cell of an excel sheet using Apache POI and insert those data into a DB?   i have an excel sheet, whose data in each cell has to be read and those data has to be inserted in the DB. This has to happen for all
Upload and insert the file contents - Java Server Faces Questions
Upload and insert the file contents  I want to browse and upload 2 files on server and insert the values into table from JSP page. So i have...   Hi here is the sample csv file data. Duct,1,Civils - General
HTML File Upload
HTML File Upload       HTML File Upload is used to show the list of all file, when a user click... an example from HTML File Upload. In this code, we create a html page which show you
Ant Script to Update Mysql Table
;    This example illustrates how to insert and update data in table through the build.xml file by simply running the ant command..._name text not null ); project.sql create table project
how to insert data in database using html+jsp
how to insert data in database using html+jsp  anyone know what... = null; // declare a resultset that uses as a table for output data from... table. String QueryString = "insert into xxqc_announcement(PID,msg
Upload CSV File into Columns of sql table using servlets and jsp
Upload CSV File into Columns of sql table using servlets and jsp  Hello sir, plz give me the code to upload csv file data into respective columns of table
Insert file data into database
Insert file data into database In this section, you will learn how to insert the file data into database. Description of code: Here we have used FileReader... connection. Then using the insert query, we have inserted whole file data
Copy text file in a HTML form.
Copy Text File in a HTML form For copying a text file from one source to other destination in HTML form, you will have to create a HTML form and call the form...="Content-Type" content="text/html; charset=iso-8859-1">
Insert Records in Table
Insert Records in Table       The Insert data in Table is used to insert  records or rows... The Tutorial illustrates an example from Insert data in Table. In this Tutorial we
Convert the excel sheet data into oracle table through java or jsp
Convert the excel sheet data into oracle table through java or jsp  Hi Friends, Let me help this issue i am phasing Convert the excel sheet data into oracle table through java or jsp
data insertion from xml file to database table
data insertion from xml file to database table  Hi all, I have data in the XML file. I need to insert it into table in the database using servlet. so please reply me . ThankYou
take data from one table and insert same in another
take data from one table and insert same in another  I want to know when a new field update in table and the same data has to update in another table
read text file and store the data in mysql - JDBC
read text file and store the data in mysql  when we store the data in mysql table from text file its store the data from new line to new column. how to store the data in different column from a single line of text file
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
Java create table in html file
Java create table in html file In this section, you will learn how to create table in html file. In the previous section, you have seen different operations... as an argument. Now we have used the html table tag to create a table in html file
 

 

 

DMCA.com