
How to connect several database from a single application in java depending upon the user input it will connect to that database

Connect to database using user input
import java.sql.*;
import java.util.*;
class ConnectToDatabase
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.print("Enter database: ");
String db=input.next();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+db, "root", "root");
Statement st=con.createStatement();
System.out.print("Enter table name: ");
String table=input.next();
ResultSet rs=st.executeQuery("select * from "+table);
while(rs.next()){
System.out.println(rs.getString(1)+" "+rs.getString(2));
}
}
catch(Exception e){}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.