Retrieval of Information from Excel Sheet using JDBC-ODBC

Hello,

Currently I am writing a program that extracts information fromexcel sheet. This excel sheet have some values in Chinese. I am able to extract English character from the different column in excel, but if some column contain chineese valur then it prints ?. After that I looked on net and found some details in to convert Big5 (Chinese) character into unicode, but this is not working and in that case nothing is displayed. I am attaching the my code. In my code the 'customer' field contain chinese character.

import java.io.ByteArrayInputStream;

import java.io.InputStreamReader;

import java.io.BufferedReader;

import java.io.Reader;

import java.io.IOException;

import java.sql.*;

public class ExcelJDBC

{

Connection con;

Statement stmt;

public ExcelJDBC(String url)

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}

catch(java.lang.ClassNotFoundException e)

{

System.err.print("ClassNotFoundException: ");

System.err.println(e.getMessage());

}

try

{

con = DriverManager.getConnection(url, "", "");

stmt = con.createStatement();

}

catch(SQLException ex)

{

System.out.println("SQL EXCEPTION OCCURED");

System.err.println("SQLException: " + ex.getMessage());

ex.printStackTrace();

}

}

public void sql(String sql)

{

try

{

stmt.execute(sql);

}

catch(SQLException ex)

{

System.out.println("SQL EXCEPTION OCCURED");

System.err.println("SQLException: " + ex.getMessage());

ex.printStackTrace();

}

}

public void closeAll()

{

try

{

stmt.close();

con.close();

}

catch(SQLException ex)

{

System.out.println("SQL EXCEPTION OCCURED");

System.err.println("SQLException: " + ex.getMessage());

ex.printStackTrace();

}

}

//big5 to unicode conversion

private String b2u(String str2convert) throws IOException

{

System.out.println("The input string is :" + str2convert);

StringBuffer buffer = new StringBuffer();

byte[] targetBytes = str2convert.getBytes();

ByteArrayInputStream stream = new ByteArrayInputStream(targetBytes);

InputStreamReader isr = new InputStreamReader(stream,"Big5");

Reader in = new BufferedReader(isr);

int chInt;

while((chInt = in.read()) < -1)

{

buffer.append((char)chInt);

}

in.close();

System.out.println("The output string is :" + buffer.toString());

return buffer.toString();

}

public void sqlQuery(String sql)

{

try

{

ResultSet rc = stmt.executeQuery(sql);

if(rc != null)

{

int count = 0;

while (rc.next())

{

String value = "";

value = b2u(rc.getString("workorder"));

System.out.println("The workorder is : "+value);

String customer = rc.getString("Customer");

value = b2u(customer);

System.out.println("The Customer is : "+value);

count++;

}

System.out.println("Entering in loop " +count+ " time.\n");

}

}

catch(SQLException ex)

{

System.out.println("SQL EXCEPTION OCCURED");

System.err.println("SQLException: " + ex.getMessage());

ex.printStackTrace();

}

catch(IOException ex)

{

System.out.println("IO EXCEPTION OCCURED");

System.err.println("IOException: " + ex.getMessage());

ex.printStackTrace();

}

}

public static void main(String[] args)

{

String url = "jdbc:odbc:detailsdsn";

try

{

ExcelJDBC ej = new ExcelJDBC(url);

ej.sqlQuery("SELECT \"workorder\", \"Customer\" FROM \"Sheet1$\"");

ej.closeAll();

}

catch(Exception ex)

{

System.out.println("SQL EXCEPTION OCCURED");

System.err.println("SQLException: " + ex.getMessage());

ex.printStackTrace();

}

}

}

I will highly appriciate any answer of my problem.

Regards,

Ruchi

[4234 byte] By [ruchi_rcs] at [2007-9-26 2:26:55]
# 1

Hi,

can we make 2 parts of your problem ?

If in any record column customer contains Chinese characters:

1) What is in String customer after

String customer = rc.getString("Customer");

This is the complete db stuff.

2) What does

value = b2u(customer);

do with it?

This conversion has nothing at all to with db, right?

If you answer these 2 questions, maybe we can help.

Hartmut at 2007-6-29 9:40:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Yes you are right. In 1st question the character in customer column contains chinese character in excel sheet. In that particular case it returns ?..., corresponding to number of chinese character in customer column. e.g if customer column contains 3 chinese character, then after retrieval the customer details from excel sheet, it returns three question marks.

2nd Question: Yes b2u function has nothing to do with db stuff. It is a simple method to convert big5 string into chinese character. I was just trying to convert the returned string from database.

ruchi_rcs at 2007-6-29 9:40:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
I have to use JDL 1.1.7B for development. It is the requirement of the product. My component has a lot of dependancy to other component and therefore it is not possible to use the latest version.
ruchi_rcs at 2007-6-29 9:40:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

Try to find out, what characters really are in your string customer.

I assume you used System.out.print..(), when you got them as '?'.

Find out which unicodes they are!

Afterwards I see two possibilites:

Either

1) you see real unicodes for Chinese characters, and it was only the unability of System.out to print them others than as '?', then you can convert these characters however you want. Maybe your further processing can handle them directly as unicode.

or

2) you get really '?' in your string. Then Excel (or JDBC or ODBC or ?) hasn't been able to transmit these Chinese characters to your program others than converting them all into '?'. Then you are lost, because you can't know which Chinese character each '?' originally could have been.

Hope this helps.

Please tell us, which you experienced, 1) or 2).

Hartmut at 2007-6-29 9:40:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

ruchi

as far as i know System.out.println prints '?' for each char which system doesn't support

r u usingwindows 2000 if yes go to

control panel -->> regional options(settings in win 98 ) and change language to simplified chinese

u may need to insert ur windows installer disc

may be this help but i am not sure

parul_patidar at 2007-6-29 9:40:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...