Enter your email address:

Delivered by FeedBurner


Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to import a csv file values in to database using jsp
12-14-2011, 12:21 PM (This post was last modified: 02-20-2012 12:33 PM by mjm.)
Post: #1
Sad how to import a csv file values in to database using jsp
Today we are going to learn how to import a csv file values in to database.
Sometimes there are lot of data need to be stored in to database so we want to type each value and submit it.Instead we create a excel file have header names for each row with their corresponding value and import once using the import option and all values are stored in database.
See the code below

<%@ page import="java.io.*,java.sql.*"%>

Quote:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>code from In order to view links, you must have to reply to this thread.</title>
<script type="text/javascript">

function importok()
{
var a=document.im.filename.value;
if(a==""||a==null)
{
alert("Select any file");
return false;
}
else
{
im.method="post";
im.action="#";
im.submit();
return true;
}}


Quote:</script>
</head>
<body>
<table>
<td width="20" valign="top"></td>
<td valign="top">
<table><tr><td>
<form method="get" ACTION="#" name="im" ENCTYPE='multipart/form-data'>
<center>
<input type="hidden" name="ctime"></input>
<input type="file" name="filename" ></input><br><br>
<input type="image" src="Ok.png" title="Ok" onclick="importok()" ></input></center>
<br><br>
<fieldset style ="width:450px">
<legend>Note</legend>
* Address should not have "<font size="5">,</font>" instead you can use "<font size="5">.</font>" <br>
1) 1st line of the imported file should be header..<br>
2) Imported File should contain the data in the following order only..<br>
Student name,regno,department,year<br>

</fieldset>

</form>

<%
Connection con1=DriverManager.getConnection("jdbc:mysql://localhost/s2sgateway.com","root","root");
Statement st=con1.createStatement();
Statement st1=con1.createStatement();
Statement st2=con1.createStatement();

String filename="s2sgateway.csv";

DataInputStream dis = null;
String record = null;
int recCount = 0;
if(filename!=null)
{
try {

File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
String[] contents=new String[1000];
int j=0;
while ( (record=dis.readLine()) != null )
{

contents[j]=record;
j++;

}

for(int i=1;i<j;i++)
{
String[] val2=new String[23];
String[] val1=contents[i].split(",");

String em="";
for(int k=0;k<10;k++)
{
if(val1[k].substring(0,1).equals("\"")||val1[k].substring(0,1).equals("'"))
{
val2[k]=val1[k].substring(1,(val1[k].length())-1);
out.println(val2[k]);
}
else
{
val2[k]=val1[k];
}
}
st.execute("insert into db(name,regno,dept,year) values('"+val2[0]+"','"+val2[1]+"','"+val2[2]+"','"+val2[3]+"')");

%>
<br>
<%
}
}
catch (IOException e) {
out.println("Exception error!" + e.getMessage());

}
}
%>
</body>
</html>


Here the imported csv file values are splited and inserted in to the database using array. To store this file or import from a network drive then store the file in the absolute path first then import the values any doubt comment here.

Happy coding.
Find all posts by this user
Add Thank You Quote this message in a reply
Advertise here or at any positions around our site send a mail to info@walkinsforum.com
02-17-2012, 11:11 AM
Post: #2
RE: how to import a csv file values in to database using jsp
how to write front end code in jsp???
Find all posts by this user
Add Thank You Quote this message in a reply
02-20-2012, 12:17 PM
Post: #3
RE: how to import a csv file values in to database using jsp
if(val1[k].substring(0,1).equals("\"")||val1[k].substring(0,1).equals("'"))
{
val2[k]=val1[k].substring(1,(val1[k].length())-1);
out.println(val2[k]);


can anyone explain above statements???
Find all posts by this user
Add Thank You Quote this message in a reply
Post Reply 


[-]
Share/Bookmark (Show All)
Facebook Linkedin Technorati Twitter Digg MySpace Delicious

Possibly Related Threads...
Thread: Author Replies Views: Last Post
Photo Convert jar to exe file rajasri 3 1,241 02-12-2012 04:55 PM
Last Post: ionutcib
Sad create a file from form input in java,jsp rajasri 0 258 12-19-2011 06:32 PM
Last Post: rajasri
Information create a war file using jsp code rajasri 0 195 12-19-2011 06:18 PM
Last Post: rajasri
Sad how to delete a file using jsp rajasri 0 266 12-19-2011 06:02 PM
Last Post: rajasri
Lightbulb compare current time with database retrieved time in jsp rajasri 0 200 12-19-2011 05:17 PM
Last Post: rajasri
Thumbs Up Display combo box value/Database value on same JSP page rajasri 0 326 12-19-2011 04:57 PM
Last Post: rajasri
Music How to extract war file in java rajasri 0 2,887 12-14-2011 12:07 PM
Last Post: rajasri
Its My Birthday! Display database value in jsp page rajasri 0 609 12-14-2011 11:43 AM
Last Post: rajasri
Its My Birthday! How to pass and get values in href as parameters in Jsp rajasri 0 1,269 12-13-2011 12:04 AM
Last Post: rajasri
Big Grin Retrieve database table values in loop using jsp rajasri 0 694 12-12-2011 11:24 PM
Last Post: rajasri

Forum Jump:


User(s) browsing this thread: 1 Guest(s)