Friday 8 February 2019

java - Connect to a user input database

The stack trace tells you that you have a SQLSyntax error. Using this you can trace back the error. In this case, the last X2 lines in the StackTrace:



"at medapp.DBConnect.Connect(DBConnect.java:19)
at medapp.MedApp.main(MedApp.java:29)
"



Are telling you that there is an error with the "medapp" class with the methods of "main" and "Connect".



Might I suggest that you contain the connection to a database to a different class from each-other? The alternative would be to close all connections with the .close() method/.




Differing classes






Scanner decision = new Scanner(System.in);
String choice = decision.next();

if(choice.trim().equals("test");
{
//enter class to connect to "test" table

}
if(choice.trim().equals("test2")
{
//enter class to connect to "test2" table
}


If you're planning on going back to main or switching tables during runtime, I'd recommend adding a finally block to close connections before making the transition.







The alternative to the above would be to have a method in your main class dedicated to each connection, with a method to close the connection for a re-connect.






EDIT



Based on the response, I would highly suggest checking the status of both tables. Install the MySQL Workbench (found here) and connect to your databases with it. This should aid you in debugging your connection issues.



Might I also suggest, go-over your code and ensure there are no spelling mistakes within your SQL Statement.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...