Java DataBase Connectivity JDBC

java database connectivity jdbc

Test data from database/validate database

Mysql.-Installation:


Database connectivity with java:-
**driver(jars) to connect different DB
---Install the MYSQL-->start the services of Mysql--->
cmd -->path of mysql BIN
-->mysql -u root(to connect using user id 'root')
create a database:

create database sudheer;
show databases;
create table;
inserted data into table;


-----------------------------------------------
add mysql jar to ecllipse:


Java code:

Connetion (interface)->it helps to connect to the DB
   
Connection conn = null;
       
        String url="jdbc:mysql://localhost:3306/";(path of mysql)
        String driver="com.mysql.jdbc.driver";
        String dbName="sudheer";[connecting to DB]
        String Username="root";
        String password="";










import java.sql.DriverManager;
import java.sql.*;

import com.mysql.jdbc.Connection;


public class DatabaseConnection {

    public static void main(String[] args) {
        Connection conn = null;//it helps to connect to the DB
       
        String url="jdbc:mysql://localhost:3306/";
        String driver="com.mysql.jdbc.Driver";
        String dbName="sudheer";
        String Username="root";
        String password="";

   
        try{
            Class.forName(driver).newInstance();//create the object of driver class;
            conn=(Connection) DriverManager.getConnection(url+dbName, Username, password);
            System.out.println(conn.isClosed());
           
           
            Statement stm= conn.createStatement();//executing a static SQL statement and returning the results it produces.
            ResultSet  rs=stm.executeQuery("Select * from employee");
            stm.executeUpdate("update table employee");

       
           
            while(rs.next()){
                System.out.println(rs.getString(1)+"<-->"+rs.getString(2)+"<-->"+rs.getInt(3));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if((conn!=null)&&(!conn.isClosed())){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
       
    }

}

Comments

Popular posts from this blog

Implicit and Explicit Waits,FluentWait,PageLoadTimeOut

A Interview Questions- selenium