Connect mySQL database using Selenium Webdriver

package testng;

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

import org.testng.annotations.Test;

public class ConnectmySQL {
	
	@Test
	public void testDB() throws ClassNotFoundException, SQLException
	{
		Class.forName("com.mysql.jdbc.Driver");
						
		Connection cnt = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentInfo","root","Akshaya@19");
					
		Statement smt = cnt.createStatement();
		
		ResultSet rs= smt.executeQuery("select * from studentName");
		
		while(rs.next())
		{
			String firstName = rs.getString("firstName");
			
			System.out.println("Database record is: --- "+firstName);
			
			
		}
		
		
	}

}

 

mysql> select * from studentName;
+———–+———-+
| firstName | lastName |
+———–+———-+
| Ram | Rao |
| Krishna | Kamath |
+———–+———-+

Leave a Reply

Your email address will not be published. Required fields are marked *