Database Connection selenium / java

Following classes are used for DB connection

DriverManager --get the db connection
Connection ---store the connection reference
Statement ----specify sql query and execute
ResultSet ---store the results retrived from DB


The below example connect to SQL database using the ODBC-DSN created in Control panel (ds1)


import java.sql.DriverManager;

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

public class dbprog
{
    public static void main(String[] args) throws Exception
    {
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     Connection conn = DriverManager.getConnection("jdbc:odbc:ds1", "", "");
     Statement st=conn.createStatement();
     ResultSet rs1 = st.executeQuery("select title,price,type from titles");
     while (rs1.next())
     {
         System.out.println(rs1.getString(1)+"---- "+rs1.getInt(2) + "---- " + rs1.getString(3));
        
     }
     conn.close();
    }
}

Comments