Saturday 8 September 2012

Tally ERP 9 to Java via ODBC bridge


The following is the working java code to extract consolidated data from Tally Database. 
Note: Type the following code by changing the 'TallyODBC' name to the 'TallyODBC' name in your PC and the compile but before running the code make sure that your 'Tally.exe' is running as an administrator especially if its Windows 7.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

/**
 *
 * @author SUNEET
 */
public class TallyODBC {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO codimporte application logic here
        try {
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

              Connection con = DriverManager.getConnection("jdbc:odbc:TallyODBC_9696", "", "");
              Statement stmt = (Statement) con.createStatement();
              ResultSet rs = stmt.executeQuery("SELECT StockItem.`$Parent`, StockItem.`$Name`, StockItem.`$BaseUnits`, StockItem.`$OpeningBalance`, StockItem.`$_InwardQuantity`, StockItem.`$_OutwardQuantity`, StockItem.`$_ClosingBalance` FROM SuneetLtd.TallyUser.StockItem StockItem ORDER BY StockItem.`$Parent`");
           
              int numberOfColumns = rs.getRow();
              int rowCount = 1;
              while (rs.next()) {
                  //for(int i = 0; i < 7; i++){
                       int op = Integer.parseInt(rs.getString("StockItem.`$OpeningBalance`").substring(0, (rs.getString("StockItem.`$OpeningBalance`").length() - 3)).trim());
                       int in = Integer.parseInt(rs.getString("StockItem.`$_InwardQuantity`").substring(0, (rs.getString("StockItem.`$_InwardQuantity`").length() - 3)).trim());
                       int out = Integer.parseInt(rs.getString("StockItem.`$_OutwardQuantity`").substring(0, (rs.getString("StockItem.`$_OutwardQuantity`").length() - 3)).trim());
                       int cl = Integer.parseInt(rs.getString("StockItem.`$_ClosingBalance`").substring(0, (rs.getString("StockItem.`$_ClosingBalance`").length() - 3)).trim());
                       System.out.println(rs.getString("StockItem.`$Parent`") + " \t " + rs.getString("StockItem.`$Name`") + " \t " + rs.getString("StockItem.`$BaseUnits`") + " \t " + op + " \t " + in + " \t " + out + " \t " + cl);
                  //}            
                rowCount++;
              }
              //System.out.println(rowCount);
              stmt.close();
              con.close();
          } catch (Exception e) {
          System.out.println(e);
        }
    }
}

Output:

Raw Materials R 1 nos 5000 5000 0 10000
Raw Materials R 2 nos 7000 8000 0 15000

More Coming Soon

23 comments:

  1. Thanks to Publish this code.
    I executed this code but it shows Data Source Name Not Found.I tried to create Data Source Name in my PC but Tally related Data Source Name is Not available.Please Guide me to set the Data Source in my windows PC.

    ReplyDelete
  2. Dear Mr. Chittibabu,
    1. Please check whether Tally is running as a administrator or not. I am talking about operating system admin. rights.
    Especially if its win 7 then make sure that you right click on the Tally exe and click on 'Run as administrator'.
    2. Then check the port in in F12 config of Tally. The port should match with the port in Java code.
    Still if you have any problem please send me the snap shots.
    Regards.

    ReplyDelete
    Replies
    1. Hi,

      I need to insert data into tally using above code. is there any possibility to do this?

      Delete
    2. Hi,

      I need to Know More Table list from Tally. How to know that table list. If you know all the table lists please send to me.

      Delete
    3. In MS Excel query wizard you get all the tables listed.
      Visit: http://www.tallysolutions.com/website/CHM/TallyERP9/Data_Management/outward_connectivity_from_tallyerp_9.htm

      Delete
    4. Hi,
      I am trying to connect to Tally ERP9 using HTTP POST with an xml attachment. I am able to establish connection to Tally, but the response is always "Tally ERP is running". The expected output is an xml with the companies created in tally. what could have gone wrong?

      Delete
    5. Send me the xml code which you are inserting

      Delete
    6. hiii

      i kunal i am using above code but it is showing error.

      java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver

      please help

      Delete
  3. hi geek, u mentioned that changet 'TallyODBC' name in your PC can u explain that where i should change or settings..badly need this integration......can u help me in this regards.thanks in advance

    ReplyDelete
  4. Thank you very much bro......i got the expected out put...

    ReplyDelete
    Replies
    1. how you got bro. tel me where to change name ?

      Delete
    2. How you get the connection with TallyODBC_9696 ( i have selected port as 9696 )

      Delete
    3. chek ur odbc connection in admin tools and chek the port which port you tally is running, usually it ll be 9000

      Delete
  5. Hi Sir,
    From the below query, I have understood that Parent,Name,BaseUnits,etc are column fields of table and I want to know that StockItem field belongs to which type of field in the table.
    ("SELECT StockItem.`$Parent`, StockItem.`$Name`, StockItem.`$BaseUnits`, StockItem.`$OpeningBalance`, StockItem.`$_InwardQuantity`, StockItem.`$_OutwardQuantity`, StockItem.`$_ClosingBalance` FROM SuneetLtd.TallyUser.StockItem StockItem ORDER BY StockItem.`$Parent`");


    this query fields are not inserting in tally database pls help me how to insert this fields in tally database.

    ReplyDelete
  6. hello sir i am new in java so can u pls tell to me how can i develop the same step by step pls send me on my id arjundhilod@gmail.com

    ReplyDelete
  7. Hi Suneet kumar It was nice tutorial but my doubt from where to get tallyODBC_9000 driver
    I am not getting the source bro please help its very urgent please bro.
    mohsin.myk@gmail.com

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. I am getting General Error
    Tally port is 9000
    Tally act as Both client/Server
    I am new in Tally
    Please help me

    ReplyDelete
    Replies
    1. I am getting error on DriverManager.getConnection("jdbc:odbc:TallyODBC_9000");

      Delete
  10. i am using above code but it is showing error.

    java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver

    please help
    Ali.fuhad74@gmail.com

    ReplyDelete
  11. Hello,

    My tally is running in admin mode in windows 10. I have used below connection string

    Connection con = DriverManager.getConnection("jdbc:odbc:TallyODBC_9000", "", "");

    But still I am getting an exception.

    java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6964)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3080)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)

    Can you please help

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete
  13. I want to fetch list of Countries and state name from tally odbc table and also i want to fetch list of bank names(which is provided in tally while creating ledger under bank accounts). plz tell me the query to fetch from tally database or some other way......

    ReplyDelete