First of all you have to follow the bellow steps to store the Excel sheet data to oracle
====================================================
Write the following code
====================================================
import java.sql.*;
import java.io.*;
import java.util.*;
public class ConvertFromCSF
{
Connection con;
PreparedStatement ps;
public static void main(String args[]) throws Exception
{
String filename = args[0];
ConvertFromCSF obj = new ConvertFromCSF();
obj.processFile(filename);
}
public void processFile(String filename) throws Exception
{
// open file
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
// connect to Oracle
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","sekhar");
ps = con.prepareStatement("insert into data values(?,?)");
String line, columns[];
// headings line. Ignore it
line = br.readLine();
Vector v;
while( (line = br.readLine())!= null)
{
v = getColumns(line);
Enumeration e = v.elements();
int i=1;
while ( e.hasMoreElements())
{
ps.setString(i, e.nextElement().toString());
i ++;
}
// insert into table after values for parameters are set
ps.executeUpdate();
}
fr.close();
ps.close();
con.close();
}
public Vector getColumns(String line)
{
Vector v = new Vector();
StringBuffer bf = new StringBuffer();
boolean instring = false; // indicates whether we are in string
for ( int i = 0 ; i < line.length(); i ++)
{
if ( line.charAt(i) == ',')
{
if (! instring ) // if not already in string.
{
v.add(new String(bf)); // add buffer to vector
bf = new StringBuffer(); // reset buffer
} // end of if
else
bf.append(","); // add comma also to string as we are in string
} // end of if
else
if ( line.charAt(i) == '"') // toggle instring flag when " encountered
{
if ( instring )
instring = false;
else
instring = true;
}
else
bf.append( line.charAt(i));
} // end of for
v.add(new String(bf)); // add value at the end as column
return v;
}
}
compile and run the above java program like the following
>javac ConvertFromCSF.java
>java ConvertFromCSF "data.csv"
Excel sheet information will be inserted into database
- Create an Excel sheet
- Type the file name: data(as you like)
- Click on save-as
- Select save-as-type: CSV(DOS)
- Click on save
- Excel file will be created like data.csv
- create a table with columns depends on your requirement create a columns in this example i was created a two columns like name,address
====================================================
Write the following code
====================================================
import java.sql.*;
import java.io.*;
import java.util.*;
public class ConvertFromCSF
{
Connection con;
PreparedStatement ps;
public static void main(String args[]) throws Exception
{
String filename = args[0];
ConvertFromCSF obj = new ConvertFromCSF();
obj.processFile(filename);
}
public void processFile(String filename) throws Exception
{
// open file
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
// connect to Oracle
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","sekhar");
ps = con.prepareStatement("insert into data values(?,?)");
String line, columns[];
// headings line. Ignore it
line = br.readLine();
Vector v;
while( (line = br.readLine())!= null)
{
v = getColumns(line);
Enumeration e = v.elements();
int i=1;
while ( e.hasMoreElements())
{
ps.setString(i, e.nextElement().toString());
i ++;
}
// insert into table after values for parameters are set
ps.executeUpdate();
}
fr.close();
ps.close();
con.close();
}
public Vector getColumns(String line)
{
Vector v = new Vector();
StringBuffer bf = new StringBuffer();
boolean instring = false; // indicates whether we are in string
for ( int i = 0 ; i < line.length(); i ++)
{
if ( line.charAt(i) == ',')
{
if (! instring ) // if not already in string.
{
v.add(new String(bf)); // add buffer to vector
bf = new StringBuffer(); // reset buffer
} // end of if
else
bf.append(","); // add comma also to string as we are in string
} // end of if
else
if ( line.charAt(i) == '"') // toggle instring flag when " encountered
{
if ( instring )
instring = false;
else
instring = true;
}
else
bf.append( line.charAt(i));
} // end of for
v.add(new String(bf)); // add value at the end as column
return v;
}
}
compile and run the above java program like the following
>javac ConvertFromCSF.java
>java ConvertFromCSF "data.csv"
Excel sheet information will be inserted into database

No comments:
Post a Comment