|
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
|
|||
|
|||
|
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" 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> <% 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.
|
|||
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???
|
|||
|
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??? |
|||
|
« Next Oldest | Next Newest »
|
![]() |
|||||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
User(s) browsing this thread: 1 Guest(s)






![[-]](images/royal_red/collapse.gif)






