Cannot connect java web app to mysql database

Alaa Slaity 1 Reputation point

I have a Java web app, which I have developed and deployed locally on my PC. The application uses jsp files, as well as java servlets. The database is created using phpMyAdmin. Now I want to make this app available online so as other people can use it. To do so, I've created an Azure web app and I deployed my JAVA app to it using a war file. the problem is that I can't connect to the database.

My original code connects to my database using the following code:

Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");

The question is if I want to use "MySQL In App" how can I connect my application to it. Also, If I want to use "Azure for MySQL" how can I connect to it? Do I only need to replace the above two lines (change the connection string)?

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
4,940 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 18,556 Reputation points Microsoft Employee

    Hi @Alaa Slaity ,

    I'm not well versed in java but using something like string mySqlConnection = System.getenv("MYSQLCONNSTR_localdb"); should retrieve the environment variable from the app service. If you don't want to use the environment variable, you can access the kudu console ( and open D:\home\data\mysql\MYSQLCONNSTR_localdb.ini and set your DriverManager.getConnection("jdbc:mysql//<connection string from *.ini>","root","");. If you want to an Azure hosted MySQL instance, you can add your connection string as a connection string setting under appliation settings (see screen shot below). If call your setting "HostedInstance", then you can use the envrionment variable MYSQLCONNSTR_HostedInstance.


    Hope this helps. Also, check for more infomration MySQL In App.

    0 comments No comments