Pages

Thursday, 15 December 2011

Database Connection Using Selenium


 MS Access with Java through JDBC-ODBC connection

Microsoft has provided a method to build a quick Jet-Engine database on your computer without the need for any specific database software (it comes standard with Windows). Using this method, we can even create a blank Microsoft Access database without having MS Access installed!
MS Access data bases can be connected to via ODBC. Instead of accessing the database directly, we can access it via a Data Source Name (DSN). Here's how to set up a DSN on your system:
  1. Open Windows' ODBC Data Source Administrator as follows:
  2. In Windows 95, 98, or NT, choose Start > Settings > Control Panel, then double-click the ODBC Data Sources icon. Depending on your system, the icon could also be called ODBC or 32bit ODBC.
  3. In Windows 2000, choose Start > Settings > Control Panel > Administrative Tools > Data Sources.
  4. In the ODBC Data Source Administrator dialog box, click the System DSN tab.
  5. Click Add to add a new DSN to the list.
  6. Scroll down and select the Microsoft Access (.MDB) driver
  7. Type in the name "TestDB" (no quotes, but leave the cases the same) for the Data Source Name
  8. Click CREATE and select a file to save the database to (I chose "d:\java\TestDB.mdb") - this creates a new blank MS Access database!
  9. Click "ok" all the way out Now our data source is done!

Here's a complete program showing how to access your new DSN data source:

package qtt.selenium;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import com.thoughtworks.*;
import com.thoughtworks.selenium.DefaultSelenium;
 import org.junit.*;
import org.openqa.selenium.server.SeleniumServer;

public class DBtest
{
   DefaultSelenium selenium;
   SeleniumServer ss;
   String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
   String url = "jdbc:odbc:TestDB";
   String username = "";
   String password = "";

   @Before
   public void setup()throws Exception
   {
            ss= new SeleniumServer();
            ss.start();
            selenium = new DefaultSelenium("127.0.0.1",4444,"*chrome","http://");
            selenium.start();
   }
   @Test
   public void test()throws Exception
   {

            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, username, password);
            Statement st = conn.createStatement();
            // st.executeUpdate("drop table survey;");
            st.executeUpdate("create table Midhun(id int,name varchar(30),Homesite varchar(30));");
            st.executeUpdate("insert into Midhun (id,name,Homesite) values (1,'MIDHUN','infofortester.blogspot.com')");
            st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Midhun");
            if(rs.next());
            {

                        String homesite = rs.getString(3);
                        selenium.open(homesite);
                        selenium.windowMaximize();
                        Thread.sleep(30000);
            }

   }

   @After
   public void teardown() throws Exception
   {
            selenium.stop();
            ss.stop();
   }
}

1 comment:

  1. thanks for sharing this topic about Database Connection Using Selenium very useful to me thanks a lot

    ReplyDelete