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
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:
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
Insert text file data into DatabaseInsert 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
Java insert file data to JTable();
frame.setVisible(true);
}
}
Through the above code, you can
insert the
file data...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 Insert Data From
File
Insert Data From
File is used to
insert data from
file... an example to
insert data from
file. To understand
this example, we create
HTML file upload exampleHTML 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...;td colspan=4><center><font size=4><b>
File Upload Insert Data From File Insert Data From
File
Insert Data From
File is used to
insert data from
file... an example to
insert data from
file. To understand
this example, we create
How to insert data into MySQL Table?How to
insert data into MySQL
Table? Hi,
How to
insert the
data...
Hi,
The
insert into query is used to
insert the
data into MySQL...
)
Then we can use the following query to
insert the
data:
insert into email(first_name
Write Text File to Table the records
of a simple
text file and write (
insert) into a simple
table in MySQL... Write
Text File to
Table
...
text file that have to be inserted the records
into a database
table. This program
Ant Script to Update Mysql Table
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
HTML File Upload
HTML File Upload
HTML File Upload is used to show the list of all
file, when a user
click... illustrate an example from
HTML File Upload. In this code, we
create a
html page
how to insert data in database using html+jsphow 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
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...;/title>
<meta http-equiv="Content-Type" content="
text/
html Insert file data into databaseInsert 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 Insert Records in Table;
The
Insert data in
Table is used to insert records or rows... with Example
The Tutorial illustrates an example from
Insert data in
Table... show you the field,
data type ,attribute used in
table country.
The
insert Java create table in html fileJava 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 Servlet to add the data into database the
data in the database
table from a
html form.
This servlet program... successfully in the
table then the output
looks like this
"
Data has been...{
response.setContentType("
text/
html");
PrintWriter pw = response.getWriter();
String
read text file and store the data in mysql - JDBCread
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.
insert datainsert data i've got a problem to
insert my
data to database.i can
upload my multipart
file but not
text data.Please help me to solve this .Attached...="
text/
html; charset=iso-8859-1"><title>Page Insert<
HTML UploadHTML Upload Hi,
I want to
upload a word / excel document using the
html code (web interface)need to get displayed on another webpage. Please let me the coding to display on another webpage using
Insert XML file data to databaseInsert XML
file data to database
In this tutorial, you will learn how to
insert the xml
file data to database
using dom parser.
You all are aware of XML
file, it is a tag based language. You can easily
transfer and store its
data JSP Include File;
JSP Include
File is used to
insert a
html file and the
text file in the jsp
page when the JSP page is compiled. The
insert file can be an
HTML file, a
text
file or code file written in the Java
Extract File data into JTableExtract
File data into JTable
In this section, you will learn how to read the
data from the
text file and
insert it into JTable. For this, we have created... the BufferedReader class, we have read the
data of the
file. This
data is then broken
Insert text into database table using JSP & jQueryInsert text into database
table using JSP & jQuery
In this tutorial , the
text is inserted into database
table using JSP &
jQuery. In the below example, the first JSP page is use to display
text box in
which we type
data filter and display html table data using ajax - Ajaxfilter and display
html table data using ajax Hi
i am stuck up at a situation where i have a jsp page from where the control goes to the .js
file... on DisplayCategory.jsp in the form of the
html table using responseText. how to do
Handle Null Pointer Exception while text file uploadHandle Null Pointer Exception while
text file upload I want the code for handling null pointer exception in jsp page.
That is I need an error page... uploading
text file into database if a different
text file is selectedi.e...,my db