I was trying to connect to Salesforce and migrate data from mySQL database to Salesforce using Java. During the connection process I found that we have to give the Username, Password and oAuth token as parameters to the connection URL: https://login.salesforce.com but it seems there was no security in doing this way. Do we have any predefined security mechanism to connect to salesforce from external application say from a JAVA application?
You can use JWT Bearer Token profile - see https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com#Obtaining_an_Access_Token_using_a_JWT_Bearer_Token
You can register your app's signing certificate in your Salesforce org; your app then signs a JWT referencing a given user and POSTs it to
https://login.salesforce.com/services/oauth2/token. If that user is authorized for the app, then the Salesforce authorization server will issue an access token for your app to access APIs on behalf of that user. The app never needs to know the user's password, and the org admin can revoke the app's access at any time by removing its signing cert from the org config.