question

AlaaSlaity-0253 avatar image
0 Votes"
AlaaSlaity-0253 asked ·

Cannot connect java web app to mysql database

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-webapps
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ryanchill avatar image
0 Votes"
ryanchill answered ·

Hi @AlaaSlaity-0253,

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.



·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.