0

S No. | Name | Phone Number | Address

1 | Ram | 234 | Delhi

2 | Shyam | 235 | Bangalore

3 | Geeta | 236 | Jharkand

4 | Raju | 237 | UP

5 |Chandu | 238 | Mumbai

How can I get data from above mentioned table based on the serial number?

I want to get address of 4th row so what logic should be used?

davejal
  • 6,009
  • 10
  • 39
  • 82

1 Answers1

0

One option is to use JDBC ODBC for Excel.

import java.sql.*;

public class jdbcTest {

    public static void main(String[] args) {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection(
                    "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};" + 
                    "Dbq=C:\\__tmp\\Book1.xlsx;");

            PreparedStatement s = conn.prepareStatement(
                    "SELECT * FROM [Sheet1$] WHERE [S No] = ?");

            // pass the serial number you want to retrieve.
            s.setString(1, "1");
            s.execute();
            ResultSet rs = s.getResultSet();
            if (rs!=null) {
                while (rs.next()) {
                    System.out.println(rs.getString("Name") +"," + rs.getInt("Phone Number"));
                }
            }
            s.close();

            conn.close();
        } catch( Exception e ) {
            e.printStackTrace();
        }

    }

}
parishodak
  • 4,506
  • 4
  • 34
  • 48