Saturday, June 21, 2008

Using Microsoft Access or Excel as the datapool

Normally we use to import a .csv file to use as datapool in the RFT.But as this tool uses all the features available in java so we can use also use Microsoft Access or Microsoft EXcel as the database.For doing this we just need to make a JDBC ODBC connection.
We can do all this in following way.
1)First go to control panel->Administrative Tools->Data Sources(ODBC)
2)Then in the User Dsn tab click add button and add a driver for access or excel and name it MyDsn.
3)select the path where you have put your .mdb or .xls file
After that we just need to use this program to connect to datasource .
Java Program for connection to the Microsoft Access database.
import java.sql.*;
public class TestConnection {
public static void main(String[] args) {
Connection con=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:MyDSN");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT * FROM EMPLOYEE");
// for inserting rows
// int count =stmt.executeUpdate("insert into EMPLOYEE(FIRST_NAME,LAST_NAME) values('Rahul','PERERA')");
while(rs.next())
{
int EMP_ID=rs.getInt(1);
String FIRST_NAME=rs.getString(2);
String LAST_NAME= rs.getString(3);
System.out.print(EMP_ID+"\t"+FIRST_NAME+"\t"+LAST_NAME);
}
catch (Exception e){}
finally{
try{
con.close();
}
catch(SQLException e){}
}
}
}

1 comment:

Anonymous said...

Very helpfull page.....u seem to be fandoo techy banda....keep posting .... ;)