java code to save excel data to mysql
Java
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import ="java.util.Date" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.io.FileNotFoundException" %>
<%@ page import ="java.io.IOException" %>
<%@ page import ="java.util.Iterator" %>
<%@ page import ="java.util.ArrayList" %>
//Apache POI Libraries
<%@ page import ="org.apache.poi.hssf.usermodel.HSSFCell" %>
<%@ page import ="org.apache.poi.hssf.usermodel.HSSFRow" %>
<%@ page import ="org.apache.poi.hssf.usermodel.HSSFSheet" %>
<%@ page import ="org.apache.poi.hssf.usermodel.HSSFWorkbook" %>
<%@ page import ="org.apache.poi.poifs.filesystem.POIFSFileSystem" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="connection" class="DB.DB_Connection" scope="page">
<jsp:setProperty name="connection" property="*"/>
</jsp:useBean>
<%!
Connection con;
PreparedStatement ps=null;
public static ArrayList readExcelFile(String fileName)
{
/** --Define a ArrayList
--Holds ArrayList Of Cells
*/
ArrayList cellArrayLisstHolder = new ArrayList();
try{
/** Creating Input Stream**/
FileInputStream myInput = new FileInputStream(fileName);
/** Create a POIFSFileSystem object**/
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
/** Create a workbook using the File System**/
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
/** Get the first sheet from workbook**/
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
/** We now need something to iterate through the cells.**/
Iterator rowIter = mySheet.rowIterator();
while(rowIter.hasNext()){
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
ArrayList cellStoreArrayList=new ArrayList();
while(cellIter.hasNext()){
HSSFCell myCell = (HSSFCell) cellIter.next();
cellStoreArrayList.add(myCell);
}
cellArrayLisstHolder.add(cellStoreArrayList);
}
}catch (Exception e){e.printStackTrace(); }
return cellArrayLisstHolder;
}%>
<%
String fileName="testExcel.xls"; //testExcel.xls Excel File name
//Read an Excel File and Store in a ArrayList
ArrayList dataHolder=readExcelFile(fileName);
//Print the data read
//printCellDataToConsole(dataHolder);
con=connection.getConn();
String query="insert into Student values(?,?,?)";
ps=con.prepareStatement(query);
int count=0;
ArrayList cellStoreArrayList=null;
//For inserting into database
for (int i=1;i < dataHolder.size(); i++) {
cellStoreArrayList=(ArrayList)dataHolder.get(i);
ps.setString(1,((HSSFCell)cellStoreArrayList.get(0)).toString());
ps.setString(2,((HSSFCell)cellStoreArrayList.get(1)).toString());
ps.setString(3,((HSSFCell)cellStoreArrayList.get(2)).toString());
count= ps.executeUpdate();
System.out.print(((HSSFCell)cellStoreArrayList.get(2)).toString() + "t");
}
//For checking data is inserted or not?
if(count>0)
{ %>
Following deatils from Excel file have been inserted in student table of database
<table>
<tr>
<th>Student's Name</th>
<th>Class</th>
<th>Age</th>
</tr>
<% for (int i=1;i < dataHolder.size(); i++) {
cellStoreArrayList=(ArrayList)dataHolder.get(i);%>
<tr>
<td><%=((HSSFCell)cellStoreArrayList.get(0)).toString() %></td>
<td><%=((HSSFCell)cellStoreArrayList.get(1)).toString() %></td>
<td><%=((HSSFCell)cellStoreArrayList.get(2)).toString() %></td>
</tr>
<%}
}
else
{ %>
<center> Details have not been inserted!!!!!!!!!</center>
<% } %>
</table>
</body>
</html>
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.regex.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class Insert {
public static void main( String [] args ) {
String fileName="C:\\File.xls";
Vector dataHolder=read(fileName);
saveToDatabase(dataHolder);
}
public static Vector read(String fileName) {
Vector cellVectorHolder = new Vector();
try{
FileInputStream myInput = new FileInputStream(fileName);
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
Iterator rowIter = mySheet.rowIterator();
while(rowIter.hasNext()){
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator cellIter = myRow.cellIterator();
Vector cellStoreVector=new Vector();
while(cellIter.hasNext()){
HSSFCell myCell = (HSSFCell) cellIter.next();
cellStoreVector.addElement(myCell);
}
cellVectorHolder.addElement(cellStoreVector);
}
}catch (Exception e){e.printStackTrace(); }
return cellVectorHolder;
}
private static void saveToDatabase(Vector dataHolder) {
String username="";
String password="";
for (int i=0;i<dataHolder.size(); i++){
Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
for (int j=0; j < cellStoreVector.size();j++){
HSSFCell myCell = (HSSFCell)cellStoreVector.elementAt(j);
String st = myCell.toString();
username=st.substring(0,1);
password=st.substring(0);
}
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
Statement stat=con.createStatement();
int k=stat.executeUpdate("insert into login(username,password) value('"+username+"','"+password+"')");
System.out.println("Data is inserted");
stat.close();
con.close();
}
catch(Exception e){}
}
}
}
package DB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DB_Connection {
private Connection con;
public DB_Connection()
{
try
{
String conUrl="jdbc:mysql://localhost:3306/MyTestDb";
String userName="root";
String pass="root";
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(conUrl,userName,pass);
}
catch(SQLException s)
{
System.out.println(s);
}
catch(ClassNotFoundException c)
{
System.out.println(c);
}
}
public Connection getConn() {
return con;
}
public void setConn(Connection con) {
this.con = con;
}
}
Also in Java:
- Title
- how to change the color of a menubar in java
- Category
- Java
- Title
- how to check if the file has remaining without reading from it java
- Category
- Java
- Title
- java taking console input
- Category
- Java
- Title
- spannable string in android java for color
- Category
- Java
- Title
- elasticsearch api code call using highlevelrestclient
- Category
- Java
- Title
- getordefault java
- Category
- Java
- Title
- java create folder
- Category
- Java
- Title
- how to reverse a string in java
- Category
- Java
- Title
- how to get length of integer in java
- Category
- Java
- Title
- compare list from db and list from request
- Category
- Java
- Title
- jar file not running after being compiled
- Category
- Java
- Title
- sort elements with sortedset
- Category
- Java
- Title
- how to open a folder in java swing project
- Category
- Java
- Title
- raise number to power java
- Category
- Java
- Title
- how to make one java class inherit from another
- Category
- Java
- Title
- sdkmanager JAVA_HOME invalid directory
- Category
- Java
- Title
- how to create a button in java
- Category
- Java
- Title
- from file to array java
- Category
- Java
- Title
- java map iteration
- Category
- Java
- Title
- leap year checker java
- Category
- Java
- Title
- add two numbers bitwise
- Category
- Java
- Title
- java http client eviction
- Category
- Java
- Title
- how to add jar in maven java application in netbeans
- Category
- Java
- Title
- java read each lines in file
- Category
- Java
- Title
- Scanner library showing element not found exception
- Category
- Java
- Title
- java valeur absolue
- Category
- Java
- Title
- islowercase java
- Category
- Java
- Title
- immagini java
- Category
- Java
- Title
- date to string java
- Category
- Java
- Title
- set java
- Category
- Java
- Title
- java split string
- Category
- Java
- Title
- Java app development
- Category
- Java
- Title
- java calendar add minutes
- Category
- Java
- Title
- how to remove all components from layeredPane java
- Category
- Java
- Title
- read and write files pattern java
- Category
- Java
- Title
- Java array nested equals
- Category
- Java
- Title
- java boolean zen
- Category
- Java
- Title
- java script to detect the crome browser
- Category
- Java
- Title
- java last element in array
- Category
- Java
- Title
- using class in java
- Category
- Java
- Title
- reading string after double in java
- Category
- Java
- Title
- counting the number of characters in a string java
- Category
- Java
- Title
- processing pi
- Category
- Java
- Title
- convert array to list java
- Category
- Java
- Title
- print in java
- Category
- Java
- Title
- usaco 2018 january contest
- Category
- Java
- Title
- how to define an arraylist in java
- Category
- Java
- Title
- java loop through map
- Category
- Java
- Title
- read integer input java
- Category
- Java
- Title
- array in java
- Category
- Java
- Title
- java d'intervalle de resultat
- Category
- Java
- Title
- query java persistence
- Category
- Java
- Title
- convert hashset to int array java
- Category
- Java
- Title
- java logical operator
- Category
- Java
- Title
- how to get elements of a list in java
- Category
- Java
- Title
- Java array multidimensional hashcode
- Category
- Java
- Title
- 3-way radix quicksort java
- Category
- Java
- Title
- primitive and non primitive data types in java
- Category
- Java
- Title
- change number into array in java
- Category
- Java
- Title
- java variable string
- Category
- Java
- Title
- java cast duration to long
- Category
- Java
- Title
- java output array lists to file
- Category
- Java
- Title
- get spring application context
- Category
- Java
- Title
- java php object
- Category
- Java
- Title
- queue implementation in java using arraylist
- Category
- Java
- Title
- in javascipt how to stop further page processing
- Category
- Java
- Title
- applicationcontext.xml
- Category
- Java
- Title
- java random number in range
- Category
- Java
- Title
- how take inputs in java with message
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- binary to int java
- Category
- Java
- Title
- how to find length of array in java
- Category
- Java
- Title
- binary string to int java
- Category
- Java
- Title
- prime factorization java
- Category
- Java
- Title
- java read file bufferedreader
- Category
- Java
- Title
- how to check if a string contains only alphabets and space in java
- Category
- Java
- Title
- convert string to int java
- Category
- Java
- Title
- how to reverse a list in java
- Category
- Java
- Title
- create jdbc connection in java
- Category
- Java
- Title
- reverse string using recursion java with explanation
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- scanner check if int
- Category
- Java
- Title
- java next permutation
- Category
- Java
- Title
- read a mail and its content in java mail api
- Category
- Java
- Title
- android create notification
- Category
- Java
- Title
- how to make a fixed size array in java
- Category
- Java
- Title
- how to detect if someone clicks on a jpanel in java
- Category
- Java
- Title
- java remove double spaces
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- expression régulière seulement un espace java
- Category
- Java
- Title
- android how to know when snackbar is done
- Category
- Java
- Title
- java making a screen
- Category
- Java
- Title
- encode file to utf-8 in java
- Category
- Java
- Title
- java array copy
- Category
- Java
- Title
- how make a final variable in java
- Category
- Java
- Title
- capacitor-android. Unfortunately you can't have non-Gradle Java modules and Android-Gradle modules in one project
- Category
- Java
- Title
- separateur JMenu swing java
- Category
- Java
- Title
- Java how to copy file
- Category
- Java
- Title
- java remove duplicates
- Category
- Java
- Title
- java random char a-z
- Category
- Java