I am currently working on setting up a Servlet that utilizes JDBC for database interactions.
My goal is to allow users to log in through a login form and then view the list of available databases in MySQL. I am implementing this functionality dynamically within the servlet by generating a dropdown list of the databases visible to the user.
Once a user selects a database from the dropdown menu, JavaScript is used to identify the selected database. I am now looking for a way to pass this value from JavaScript back to my servlet. The idea is to use the selected database information to display a list of tables within that database to the user.
Is there a way to achieve this? If not, are there alternative methods to accomplish the same goal?
Below is an excerpt of the code responsible for generating the dynamic dropdown menu:
ResultSet r = dbmetadata.getCatalogs();
ResultSetMetaData metadata = r.getMetaData();
int colCount = metadata.getColumnCount();
int i;
out.println("<select id='db' name='db' onChange= 'myFunction();'>");
out.println("<option value = -1>--Select Database--</option>");
int value = 1;
while(r.next())
{
for(i=1;i<=colCount;i++)
{
out.println("<option value = " + value + ">" + r.getString(i) + "</option>");
}
k++;
}
out.println("</select>");
The accompanying JavaScript code is structured as follows:
function myFunction()
{
var select = document.getElementById("db");
var dbSelected = select.options[select.selectedIndex].text;
var x = document.getElementById("dbs");
x.innerHTML = "You have selected " + dbSelected;
};
Thank you for your assistance :)