Cannot connect java web app to mysql database

Alaa Slaity 1 Reputation point
2020-05-01T05:15:14.87+00:00

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:

Class.forName("com.mysql.jdbc.Driver");
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.
6,853 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 25,486 Reputation points Microsoft Employee
    2020-05-01T21:51:57.23+00:00

    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 (https://yourappservicename.scm.azurewebsites.net/DebugConsole) 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.

    open-ui.png

    Hope this helps. Also, check https://github.com/projectkudu/kudu/wiki/MySQL-in-app for more infomration MySQL In App.

    0 comments No comments