简体   繁体   中英

Java JDBC: Cannot connect to SQL due to unknown database

So I've been able to connect to MySQL through Netbeans on my older computer, using the following code:

        Class.forName("com.mysql.jdbc.Driver");
        String connectionUrl = "jdbc:mysql://localhost:3306/tblpizza?user=root"; 
        Connection con = DriverManager.getConnection(connectionUrl,"root","root");

However, since getting a new computer, every time I try to run the code, it comes up with this error:

SQL Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'root'

Being fairly new to coding in general, I tried researching different queries online but couldn't come up with a solution.

Notes:

-I tried changing different sections of my code, making sure I didn't miss any upper case/lower case letters

-I went onto MYSQL and ran a command to check all the database names, and made sure to copy the right one down

-No matter how I change the code in anyway, it always says "unknown database 'root'" even though I haven't put root down as the database name

Any help would be greatly appreciated!

Your database URL should not have the ?user=root suffix.

Make it look like so, as you are specifying the username/password already in the getConnection(url, username, password) call.

String connectionUrl = "jdbc:mysql://localhost:3306/tblpizza"; 

Since it is asking from username and password in next step while making con object you should not specify it in connection url . Try to run code by removing the ?user=root from your connection url .

String connectionUrl = "jdbc:mysql://localhost:3306/tblpizza"; 
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/tblpizza","root","root");

Try this way, you can omit the connectionUrl variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM