Simple Excel Sheet To Mysql Conversion Using Java
Simple Excel Sheet To Mysql Conversion Using Java
-------------
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
----------------------------------------------------
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
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
7. if everything is done correctly then window loaded with all the tables
in the DB is displayed
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.*;
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);
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*/
}
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();
}
}
catch(Exception exe){System.out.println(""+exe);}
}
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");}
}
}
}
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){
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);}
}
}
----------------------------------------------------------------------------------
--------------------