I have Connect() method which has a string paramater, this parameter is an input from the user. I have 2 databases created: test and test2, but I can only connect to test db from user input. Should I made changes to the server that host the 2 dbs so to be able to connect to any database that user writes???
public void Connect(String DataBase) {
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + DataBase);
st = con.createStatement();
System.out.println("Connected to " + DataBase + " !!!");
con.close();
}catch(SQLException se){
System.out.println("Cannot connect to database " + DataBase);
}
catch(Exception ex){
System.out.println("Error: " + ex);
}
}
MAIN METHOD
// DB1Connect connect = new DBConnect("test");
//connect.getData();
Scanner scanner = new Scanner(System.in);
System.out.println("Choose from these choices");
System.out.println("-------------------------\n");
System.out.println("1 - Connect to a specific DB schema");
System.out.println("2 - List DB schema structure: tables, fields, types");
System.out.println("3 - List table structure: fields, types");
System.out.println("4 - Quit");
int choice = scanner.nextInt();
switch(choice) {
case 1:
System.out.println("Enter the Db's name:");
String dbName = scanner.next();
DBConnect connect = new DBConnect();
connect.Connect(dbName);
break;
case 2:
break;
default:
System.out.println("Incorrect option");
}
}
STACK TRACE:
Enter the Db's name:
test2
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 'test2'
Cannot connect to database test2
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:910)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3923)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1273)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:718)
at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at medapp.DBConnect.Connect(DBConnect.java:19)
at medapp.MedApp.main(MedApp.java:29)
testdatabase? What happens if the user enterstest2? - RealSkepticse.printStackTrace()to thatcatchand copying the full stack trace to your question? - RealSkepticeditlink under the question. Write something like "This is the stack trace", then paste the stack trace, then add<pre>at its beginning and</pre>at its end to format it. - RealSkeptic