Establish jdbc connection with mysql database

 import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


public class sales {


public static void main(String[] args) throws SQLException, ClassNotFoundException {

// TODO Auto-generated method stub

 

 String host="localhost";

 String port="3306";

 Connection con=DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/myoffice","root","password");

Statement s=con.createStatement();

ResultSet rs=s.executeQuery("Select * from employee where emp_id=105");

while(rs.next())

{

System.out.println(rs.getString("first_name"));

System.out.println(rs.getString("last_name"));


}

}


}

Comments