You are on page 1of 6

-----------------------

for comments mail me to


-----------------------
ravikumar17jan@gmail.com

ravibecks2300@gmail.com

-------------
Preconditions
-------------

The name of each individual excel sheet should be the same as the name
of the mysql table to which you are converting

Number of columns in the excel sheet should be the same as the number of
columns in the mysql table to which you are converting

----------------------------------------------------
How to Run this application
----------------------------------------------------

1.Download Java Excel API from http://jexcelapi.sourceforge.net/

2.Add jxl.jar file from the downloaded zip or tar.gz to your classpath

3.Save all the three files given below in the same location

4.Replace the following in the db.conf and save it

yourhost with hostip or host name in which your mysql server is running
yourdb with mysql server DB name
yourname with your mysql user account name having previlage to modify above DB
yourpass with the password for above user account

5.Compile javac ExToMy.java


6.run java ExToMy

7. if everything is done correctly then window loaded with all the tables
in the DB is displayed

8. browse and load the xls file

9. select the sheet name and corresponding mysql table name and click convert

10. if all the preconditions are satisfied then conversion is done successfully

----------------------------------------------------
Save this code as DBConnection.java
----------------------------------------------------
import java.sql.*;
import java.io.*;

public class DBConnection {

public Connection con;


FileInputStream fi;
BufferedReader buff;
String line="";
String[] lines=new String[30];
int i=0;
public DBConnection() {

try {

fi=new FileInputStream("db.conf");
buff=new BufferedReader(new InputStreamReader(fi));
i=0;
while((line=buff.readLine())!=null){
lines[i]=line;
i++;
}
String host=lines[1].trim();
String database =lines[3].trim();
String user =lines[5].trim();
String password = lines[7].trim();
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://"+host+"/" + database,
user, password);

} catch (Exception exe) {


System.out.println("fof " + exe);
}
}
}
----------------------------------------------------------------------------------
--------------------
----------------------------------------------------
Save this code as db.conf
----------------------------------------------------
#hostname or IP address
yourhost
#Database name
yourdb
#Database server User name
yourname
#Database server password(leave next line blank if no password)
yourpass
#end
----------------------------------------------------------------------------------
--------------------
----------------------------------------------------
Save this code as ExToMy.java
----------------------------------------------------

import jxl.*;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class ExToMy extends JFrame implements ActionListener{
/*swing components*/
private JLabel l_xlfile,l_xlsheets,l_mytables;
private JTextField xlfile;
private JComboBox xlsheets,mytables;
private JButton browse,convert,reload_file;
private JFileChooser filechoose;
private GridBagLayout gbl;
private GridBagConstraints gbc;
/*swing components*/
java.util.List my_fields_type;
/*DB*/
DBConnection db;
Statement stat;
ResultSet rs;
/*DB*/
/*excel*/
Workbook workbook;
private String[] sheet_names;
/*excel*/
public ExToMy(){
initComponents();
/*DB*/
try{
db=new DBConnection();
stat=db.con.createStatement();
}
catch(Exception exe){}
initDB();
/*DB*/

/*Layout settings*/

gbl=new GridBagLayout();
gbc=new GridBagConstraints();
gbc.weighty=1;
gbc.weightx=1;
gbc.fill=GridBagConstraints.HORIZONTAL;
posComponent(l_xlfile,gbl,gbc,1,1);
posComponent(l_xlsheets,gbl,gbc,1,3);
posComponent(l_mytables,gbl,gbc,1,4);
posComponent(xlfile,gbl,gbc,2,1);
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.NORTHWEST;
posComponent(reload_file,gbl,gbc,1,2);
posComponent(browse,gbl,gbc,2,2);
gbc.anchor=GridBagConstraints.CENTER;
gbc.fill=GridBagConstraints.HORIZONTAL;
posComponent(xlsheets,gbl,gbc,2,3);
posComponent(mytables,gbl,gbc,2,4);
gbc.fill=GridBagConstraints.NONE;
posComponent(convert,gbl,gbc,2,5);
gbc.fill=GridBagConstraints.HORIZONTAL;
setLayout(gbl);
setTitle("Excel to mysql Converter");
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*Layout settings*/
}

public void initComponents(){


l_xlfile=new JLabel("Select a ms excel file to open");
l_xlsheets=new JLabel("Worksheets in workbook");
l_mytables=new JLabel("MySQL tables");

xlfile=new JTextField();

xlsheets=new JComboBox();
mytables=new JComboBox();

browse=new JButton("Browse");
browse.addActionListener(this);
reload_file=new JButton("Reload File");
reload_file.addActionListener(this);
convert=new JButton("Convert");
convert.addActionListener(this);

filechoose=new JFileChooser();
my_fields_type=new ArrayList();
}

public void initDB(){


try{
mytables.removeAllItems();
rs=stat.executeQuery("SHOW TABLES");
while(rs.next()){
mytables.addItem(""+rs.getString(1));
}

}
catch(Exception exe){System.out.println(""+exe);}
}

public void posComponent(Component comp,GridBagLayout gbl,GridBagConstraints


gbc,int posx,int posy){
gbc.gridx=posx;
gbc.gridy=posy;
gbl.setConstraints(comp,gbc);
getContentPane().add(comp);
}

public void openXls(){


int val=filechoose.showOpenDialog(this);
if(val == JFileChooser.APPROVE_OPTION) {
/*excel*/
try{
workbook=
Workbook.getWorkbook(filechoose.getSelectedFile());

xlfile.setText(filechoose.getSelectedFile().getPath());
sheet_names=workbook.getSheetNames(); //A string
array of sheet names is returned
xlsheets.removeAllItems();
for(int i=0;i<sheet_names.length;i++){
xlsheets.addItem(""+sheet_names[i]);
}
}
catch(Exception
exe){JOptionPane.showMessageDialog(this,(String)"Select a valid excel(.xls)
file");}
}
}

public void actionPerformed(ActionEvent ev){


Object source=ev.getSource();
if(source==browse){
openXls();

}
if(source==reload_file){
try{
workbook=
Workbook.getWorkbook(filechoose.getSelectedFile());
sheet_names=workbook.getSheetNames(); //A string array of
sheet names is returned
xlsheets.removeAllItems();
for(int i=0;i<sheet_names.length;i++){
xlsheets.addItem(""+sheet_names[i]);
}
}
catch(Exception exe){}

}
if(source==convert){
if(xlsheets.getItemCount()<=0){
JOptionPane.showMessageDialog(this,(String)"Select a valid
excel(.xls) file to select a worksheet");
}
else{
try{
int xl_fields=0,my_fields=0,xl_row_count=0;
String insert_query="";
String
sel_sheet_name=""+xlsheets.getSelectedItem();
String
sel_table_name=""+mytables.getSelectedItem();
if(sel_sheet_name.equals(sel_table_name)){
Sheet
sheet=workbook.getSheet(xlsheets.getSelectedIndex());
xl_fields=sheet.getColumns();
my_fields_type.clear();
rs=stat.executeQuery("DESCRIBE
"+sel_table_name);
while(rs.next()){
my_fields++;

my_fields_type.add(rs.getString(2));
}
if(xl_fields==my_fields){

//JOptionPane.showMessageDialog(this,(String)"Number of Fields are equal");


xl_row_count=sheet.getRows();
System.out.println(xl_row_count);//
for(int j=0;j<xl_row_count;j++){
insert_query="INSERT INTO
"+sel_table_name+" VALUES(";
for(int i=0;i<xl_fields;i++){
String
field_type=""+my_fields_type.get(i);

if(field_type.indexOf("int")>=0||field_type.indexOf("float")>=0||
field_type.indexOf("double")>=0){

insert_query+=sheet.getCell(i,j).getContents().toString()+",";
}
else{

insert_query+="\'"+sheet.getCell(i,j).getContents()+"\',";
}
}

insert_query=insert_query.substring(0,insert_query.length()-1)+")";

System.out.println(insert_query);
try{

stat.executeUpdate(insert_query);
}
catch(Exception
exe){JOptionPane.showMessageDialog(this,(String)"Row No "+j+"
"+exe);System.out.println(""+exe);}
}

JOptionPane.showMessageDialog(this,(String)"All rows Successfully copied");


}
else

JOptionPane.showMessageDialog(this,(String)"Number of Fields are not


equal");
}
else

JOptionPane.showMessageDialog(this,(String)"Select same worksheet name as


mysql table");
}
catch(Exception exe){System.out.println(""+exe);}
}
}
}

public static void main(String[] args){

ExToMy exm=new ExToMy();

}
----------------------------------------------------------------------------------
--------------------

You might also like