Home Answers Viewqa JDBC JAVA to SQL

 
 


prashant khandekar
JAVA to SQL
1 Answer(s)      2 years ago
Posted in : JDBC

package abhijeet;

import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Locale;

import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.read.biff.BiffException;

public class Abhijeet {

public void init(String filePath) {
    File fileXYZ = new File(filePath);
    FileInputStream fs = null;
    try {
        fs = new FileInputStream(fileXYZ);
        contentReading(fs);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        try {
            fs.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

//Returns the Headings used inside the excel sheet
public void getHeadingFromXlsFile(Sheet sheet) {
    int columnCount = sheet.getColumns();
    for (int i = 0; i < columnCount; i++) {
        System.out.println("Coloumn Count : "+sheet.getCell(i, 0).getContents());
    }
}

public void contentReading(InputStream fileInputStream) {
    WorkbookSettings ws = null;
    Workbook workbook = null;
    Sheet s = null;
    Cell rowData[] = null;
    int rowCount = 0;
    int columnCount = 0;
    int totalSheet = 0;

    try {
        ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        workbook = Workbook.getWorkbook(fileInputStream, ws);

        totalSheet = workbook.getNumberOfSheets();
        if(totalSheet > 0) {
            System.out.println("Total Sheet Found:" + totalSheet);
            for(int j=0;j<totalSheet ;j++) {
                System.out.println("Sheet Name:" + workbook.getSheet(j).getName());
            }
        }


        s = workbook.getSheet(0);


        System.out.println("Total Rows inside Sheet:" + s.getRows());
        rowCount = s.getRows();


        System.out.println("Total Column inside Sheet:" + s.getColumns());
        columnCount = s.getColumns();

        ArrayList arrMain = new ArrayList();
        for (int i = 0; i < rowCount; i++) {

            rowData = s.getRow(i);

            if (rowData[0].getContents().length() != 0) { // the first date column must not null
                for (int j = 0; j < columnCount ;j++) 
                {

                    ArrayList<String> arrData=new ArrayList();

                        switch (j) {

                    case 0:
                    {
                        System.out.println("AP Code: "+rowData[j].getContents());
                        arrData.add(rowData[j].getContents());
                    }

                    break;
                    case 1:
                    {
                        System.out.println("AP Name: "+rowData[j].getContents());
                        arrData.add(rowData[j].getContents());
                    }
                    break;
                    case 2:

                    {
                        System.out.println("City: "+rowData[j].getContents());
                        arrData.add(rowData[j].getContents());
                    }
                    break;
                    case 3:

                    {
                        System.out.println("Country: "+rowData[j].getContents());
                        arrData.add(rowData[j].getContents());
                    }





                            }
                        arrMain.add(arrData);
                        }


                }


            {
                workbook.close();       

        }


    }
    }
    catch (IOException e)
    {

        e.printStackTrace();

    }

    catch (BiffException e)
    {

        e.printStackTrace();
    }
}

    public int arrMain() {
    // TODO Auto-generated method stub
    return 0;
}

    public static void main(String[] args) {
    try {
        Abhijeet xlReader = new Abhijeet();
        String fileN = "D:/ProjectExcel/First_Project.xls"; 
        Connection conn =getSimpleConnection();
        Statement stmt = conn.createStatement();
        String InstQuery="Insert into First_Project (Airport_code,Airport_name,City,Country) values (arrData.add(Airport_code),arrData.add(Airport_name),arrData.add(City),arrData.add(Country))";
        xlReader.init(fileN);
        ArrayList arrMain = null;
        stmt.execute(InstQuery);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static void ArrayList() { // TODO Auto-generated method stub

    }

static Connection getSimpleConnection() { //See your driver documentation for the proper format of this string : String DBCONNSTRING = "jdbc:mysql://localhot:3306/mysql"; //Provided by your driver documentation. In this case, a MySql driver is used : String DRIVERCLASSNAME = "com.mysql.jdbc.Driver"; String USER_NAME = "navin"; String PASSWORD = "n@v!n";

            Connection conn = null;

         /*   try {
               Class.forName(DRIVER_CLASS_NAME).newInstance();
            }
            catch (Exception ex){
               log("Check classpath. Cannot load db driver: " + DRIVER_CLASS_NAME);
            }

            try {
              conn = (Connection) DriverManager.getConnection(DB_CONN_STRING, USER_NAME, PASSWORD);
            }
            catch (SQLException e)
            {
               log( "Driver loaded, but cannot connect to db: " + DB_CONN_STRING);
            }*/
            return conn;
          }

}

plzzz help mw wid dis...

1: i hav read and Excel file in java and printed its contents

2: Now i want to redirect dat oupput to MYSQL manager 2005 so i have connectivity code wid but i m not getting n e error nor i m getting any output so plzzz kindly help me

View Answers

May 24, 2011 at 5:01 PM


import java.io.*;
import java.sql.*;
import java.util.*;
import jxl.Cell; 
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings; 
import jxl.read.biff.BiffException;

public class ReadExcelFile{
ArrayList<String> list1=new ArrayList<String>();
ArrayList<String> list2=new ArrayList<String>();
ArrayList<String> list3=new ArrayList<String>();
ArrayList<String> list4=new ArrayList<String>();

public void getHeadingFromXlsFile(Sheet sheet) {
    int columnCount = sheet.getColumns();
    for (int i = 0; i < columnCount; i++) {
        System.out.println("Coloumn Count : "+sheet.getCell(i, 0).getContents());
    }
}

public void contentReading() {
    WorkbookSettings ws = null;
    Workbook workbook = null;
    Sheet s = null;
    Cell rowData[] = null;
    int rowCount = 0;
    int columnCount = 0;
    int totalSheet = 0;

    try{
        ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        workbook = Workbook.getWorkbook(new File("C:/data.xls"), ws);

        totalSheet = workbook.getNumberOfSheets();
        if(totalSheet > 0) {
            System.out.println("Total Sheet Found:" + totalSheet);
            for(int j=0;j<totalSheet ;j++) {
            System.out.println("Sheet Name:" + workbook.getSheet(j).getName());
            }
        }
        s = workbook.getSheet(0);
        System.out.println("Total Rows inside Sheet:" + s.getRows());
        rowCount = s.getRows();
        System.out.println("Total Column inside Sheet:" + s.getColumns());
        columnCount = s.getColumns();
        for(int i = 1; i < rowCount; i++){
            rowData = s.getRow(i);
            if (rowData[0].getContents().length() != 0) { 
                for(int j = 0; j < columnCount ;j++){
                 switch (j) {
                    case 0:
                       System.out.println("Name: "+rowData[j].getContents());
                       list1.add(rowData[j].getContents());
                       break;
                    case 1:
                      System.out.println("Address: "+rowData[j].getContents());
                      list2.add(rowData[j].getContents());
                      break;
                    case 2:
                      System.out.println("Contact: "+rowData[j].getContents());
                      list3.add(rowData[j].getContents());
                      break;
                    case 3:
                      System.out.println("Email: "+rowData[j].getContents());
                     list4.add(rowData[j].getContents());
                     break;
                     }
                       }
                }
    }
    workbook.close(); 
    try{
   Class.forName("com.mysql.jdbc.Driver");
         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia", "root", "root");
         Statement st=conn.createStatement();
         for(int i=0;i<list1.size();i++){
             String name=list1.get(i).toString();
             String address=list2.get(i).toString();
             String contact=list3.get(i).toString();
             String email=list4.get(i).toString();
         st.executeUpdate("insert into employee(name,address,contactNo,email) values('"+name+"','"+address+"','"+contact+"','"+email+"')");
         }
        System.out.println("Inserted successfully!");
    }
    catch(Exception e){
    System.out.println(e);
    }
    }
    catch (Exception e){
        e.printStackTrace();
    }
}
    public static void main(String[] args) {
    try {
        ReadExcelFile xlReader = new ReadExcelFile();
        xlReader.contentReading();
            } catch (Exception e) {
        e.printStackTrace();
    }
}
}









Related Pages:
Java and SQL - SQL
Java and SQL  i have a problem with regard to connecting my java code in getting a data to SQL. my problem is on how to come up with a pop up alert message in the user interface where in the message will be coming from the SQL
java,sql
java,sql  Hello sir,iam sireesha studying MCA.i want a project on educational details of a student by using jdbc,odbc connection including sql queries.please send me the reply.  Hi, Please visit http://roseindia.net
sql,java
sql,java   i am writing some code in java.that values are stored in database.plz send me one example for this for example taking the fields as s.no,category,name of the candidate,age,title of the project,video uploading,photo
java - SQL
java  hi . i am asifa and i for the last few days i was trying to display the backend fields to the frontend ,am using sql as the backend am using ecclipseeuropa IDE .can u please send the code for displaying the backend fields
SQL injunctions - SQL
SQL injunctions  Could you please tell me how to handle the sql injunctions?  Hi friend, SQL injection is a technique that exploits.... Prevention against SQL injection, User input must not directly be embedded in SQL
java .sql
java .sql  where are the interfaces of java.sql like STATEMENT,CONNECTION are defined
insertion in SQL - SQL
in the database because of single code in the name. dbase is MS-SQL emp.name data type... as I know string parameter in SQL is denoted as 'john' . I think thats why... str = "John's"; str = str.replace("'","\\'"); String sql
java,sql
java,sql  Hello sir,iam an MCA student.I want a project code of jdbc,odbc connection for educational details of a student.please send me the code.  you din't send me the reply,please send it fastly sir
JAVA to SQL
JAVA to SQL  package abhijeet; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import...; } } plzzz help mw wid dis... 1: i hav read and Excel file in java
Java sql Exception
Java sql Exception  difference between sql exception class and sql warning class
java,sql
java,sql  Hello sir,iam sireesha studying MCA.I want a project code on JDBC,ODBC connection to display student administration system (or) student information system (or) college administraion system.Please send me the code sir
SQL or UNICODE - SQL
SQL or UNICODE   Hi again............ I have got something new that........... i was using MS Access as the database with my JAVA Japplet... ...... I had also change my backhand to SQL SERVER but retrieves same for UNICODE
sql connectivity through java
sql connectivity through java  i want an example for java db through sql connection. Thanks in advance
java-sql - JDBC
java-sql  how to convert excel file into sql database usin java programming
createdatabase - SQL
createdatabase  hello please help me... I'm a student and I want to create database using SQL how I can Connect java with SQL. thanks   Hi Friend, Please visit the following link: http://www.roseindia.net/jdbc
NullPointerException - SQL
are not null, I check them with print them out. String sql = "UPDATE preview SET... sql string to make double check that query is correct. and when I try to run it with calling bean function data.Set_Info(sql);--inside function -> st
My SQL
My SQL  Connection class for MySQL in java
Import Data to SQL database in Java
Import Data to SQL database in Java  Hi, I'm a beginner and I'm wondering how to Import Data to SQL database in Java? Thanks (Platform: Eclipse/SQL
SQL Exception
SQL Exception  I want stores data in to the databse table but it can't be stores it shows an error about sql exception. blackberry.html <...;/body> </html> ** bb.jsp ------ ** <%@ page language="java
oracle - SQL
oracle  can you suggest me a book regarding oracle and java even..  Hi friend, For more information on Oracle and Java Books visit to : http://www.roseindia.net/programming/books/Freejavabooks.shtml http
Java to SQL Server Connectivity
Java to SQL Server Connectivity  Hi, heres my code private void bookedButton() { try { Class.forName... with sql server connectivity at the background to save the data. Now, after
Connectivity with sql in detail - JDBC
Connectivity with sql in detail  Sir/Madam, I am unable to connect the sql with Java. Please tell me in detail that how to connect. Thankyou
Connectivity with sql in detail - JDBC
Connectivity with sql in detail  Sir/Madam, I am unable to connect the sql with Java. Please tell me in detail that how to connect. Thankyou
Java Compilation error - SQL
Java Compilation error  ava.lang.NullPointerException at beanUtils.SaleHeaderUtility.getFullRemainPatientList(SaleHeaderUtility.java:754) at forms.ProcessDialog.updatePatientBalances(ProcessDialog.java:119
java,sql - JSP-Servlet
java,sql  Hi Amardeep, Thank you from the bottom of my heart for giving me some excellent well thought out code. Can you please... any appreciable result. Unfortunately, my java coding skills are not as sharp
java sql - JSP-Servlet
java sql  I need to diplay only 10 records per page from my resultset. while(rs.next())loops through entire rs. How can I use a for loop in conjunction with the while loop to display only 10 records per page and then continue
Transfer Data to Sql Database
Transfer Data to Sql Database  Hey guys, how to Transfer data to SQl database using Java? Transfer data already stored in excel to Sql databse using Java? Looking for an easier method so i can comprehend
SQL connection error in android
SQL connection error in android  hi, i am android developer . recently i made one application connect with sql server 2005 using jtds lib.application run android2.2,2.3 but run in android4.0.3 it give exception: java
my sql innodb
my sql innodb  Write a java program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it. If so, your program should print the total number of disk writes by MySQL
sql
difference between sql and oracle  what is the difference between sql and oracle
sql
sql  how to get first row in sql with using where condition in sql? how to get last row in sql with using where condition in sql
Executing Set of SQL statements in Java
Executing Set of SQL statements in Java  Hi, I am trying to execute a procedure which is stored in MS SQL server's database. I have configured the driver with ther server name, database name and uid/pwd using a callable
SQL Date, SQL Date Examples
SQL Date      ... Date Today in My sql. The Query used in the below example the now... to write the Date Format in SQL Query.    Mysql Date
Java servlet with jsp on sql server
Java servlet with jsp on sql server  How to delete a user by an admin with check box in Java Servlet with jsp on Sql Server?   Here is a jsp example that can delete the multiple record from the database by selecting
DataBase connectivity in java with Microsoft sql server2000 - JDBC
DataBase connectivity in java with Microsoft sql server2000  How i can achieve database connectivity with Microsoft sql server2000 in my java source code
sql
sql  I want interview sql questions   Please visit the following link: SQL Tutorials
database connection - SQL
database connection  hi friends, This is poornima i want to connect sql server to my eclipse editer how to connect and where i have to write code... the tables. Thanks in advance.  Hi Friend, Do you want to use java
Connectivity with sql in detail - JDBC
Connectivity with sql in detail  Sir/Madam, I am unable to connect the sql with Java. Please tell me in detail that how to connect. Thankyou.  Hi Friend, Put mysql-connector
Sql
Sql  how to find maximum of a101,a102,a103 from a table in sql
SQL
SQL  hii What is sql?   hello, SQL, which stands for Structured Query Language, is a special-purpose language used to define, access, and manipulate data. SQL is non procedural, meaning that it describes
sql
sql  returning value from a store procedure in sql with example   Please visit the following links: http://www.roseindia.net/mysql/mysql5/stored-procedures-and-functions.shtml http://www.roseindia.net/sql/create
Connections with MicroSoft SQL - JDBC
Connections with MicroSoft SQL  How to Connect Java and MS SQL?? Actually i used below code i got SQLException and class not found... import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet
java oracle connection problem - SQL
java oracle connection problem  I have successfully made connection with the oracle10g database that i am using and also able to put in data using JSP. But the codes that i have tried for fetching the data are not working even
SQL
SQL  In my computer i have microsoft sql 2008.how can i set that in the cmd.i want to use that in cmd.what is the default username and password
netbeans ddl and dml sql statements - Java Beginners
netbeans ddl and dml sql statements  Kindly send me the ddl,dml and tcl commands while in netbeans. wilson
java and sql server NOT Mysql - JSP-Servlet
java and sql server NOT Mysql  Hi guys. Thank you all for the various helps you render to everyone. Please the question I asked the last time... image from MS SQL server NOT MySQL. Or is it not possible to do it in MS SQL server
jdbc odbc sql server exception
jdbc odbc sql server exception  Hi, I am trying to use sql server with java jdbc odbc but I am getting this exception java.sql.SQLException: [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near '@P1

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.