Click here to Skip to main content
16,022,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
it is about Java database connection and i got error and it dose not connect to Mysql Database.
Here is the code.
Note: Database name is students .. username is root and i have no password.

What I have tried:

package databaseproject;

import java.sql.*;
import java.util.*;

public class DatabaseProject {

    public static void main(String[] args) {
        DatabaseProject pro = new DatabaseProject();
        pro.createConnection();
    }
    
    void createConnection()
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/students","root", "");
            System.out.println("Database Connection success");
        } catch (SQLException ex) {
            System.out.println("Database Connection faild");
            Logger.getLogger(DatabaseProject.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

and Here is the output

Database Connection faild
نوف 16, 2019 10:42:08 ص databaseproject.DatabaseProject createConnection
SEVERE: null
java.sql.SQLSyntaxErrorException: Unknown database 'students'
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
	at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at databaseproject.DatabaseProject.createConnection(DatabaseProject.java:33)
	at databaseproject.DatabaseProject.main(DatabaseProject.java:25)

BUILD SUCCESSFUL (total time: 1 second)
Posted
Updated 15-Nov-19 22:45pm

1 solution

Read the error message, it's very explicit:
java.sql.SQLSyntaxErrorException: Unknown database 'students'
Your SQL Server does not have a database loaded called "students"

Check your server, see what the DB is called, check you have the right instance of the server, that you have actually created the DB and given it the right name.

Sorry, but we can't do any of that for you!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900