2

I am trying to make a new user in a Mysql database using java code but the code is not working. This is the Database class which initializes the database connection and creates a user, sets the password, etc.

package app;

import com.mysql.cj.jdbc.MysqlDataSource;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class Database {

    public static MysqlDataSource dataSource;

    public static void InitializeData() {
        dataSource = new MysqlDataSource();
        dataSource.setUser("Quiz");
        dataSource.setPassword("Give the quiz");
        dataSource.setServerName("localhost");
        dataSource.setPort(3306);
        dataSource.setDatabaseName("QUIZDATA");


        Connection con = Main.getConnection();
        try {
            Statement st2 = con.createStatement();
            st2.execute("CREATE TABLE IF NOT EXISTS Subject1(Question VARCHAR, Option1  VARCHAR, Option2 VARCHAR, Option3 VARCHAR, Option4 VARCHAR, Answer VARCHAR)");
        } 
            catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

And here is my Main class.

package app;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;    
import java.sql.Connection;
import java.sql.SQLException;

public class Main extends Application {

    @Override
            public void start(Stage stage) throws Exception
            {
                Parent scene = FXMLLoader.load(getClass().getResource("quiz.fxml"));
                stage.setTitle("Quiz");
                stage.setScene(new Scene(scene));
                stage.show();
            }

    public static void main(String[] args)
    {
        launch(args);
        Database.InitializeData();
    }


    public static Connection getConnection()
    {
        Connection con = null;
        try
        {
            con = Database.dataSource.getConnection();
        }
        catch (SQLException e)
        {
            Alert alert = new Alert(Alert.AlertType.ERROR, "Database error", ButtonType.OK);
            alert.showAndWait();
        }

        return con;
    }

}

But when I am scanning for any new server in the Mysql workbench, it is not showing any new server.

Add:- When I am right clicking on the blank area(Mysql Workbench), there is a option to "to add connections from clipboard"(Although I don't know what it is doing), it is returning an error :- "Could not parse connection parameters from string "Give the quiz" " (which is the password)"

1 Answer 1

1

But when I am scanning for any new server in the Mysql workbench, it is not showing any new server.

This is not related to your program. First, you need to make sure that your MySQL database is running and then try to execute your program.

Apart from this, although you will be able to run your program once you ensure that your MySQL server is up and running, your program has not been designed well e.g. you should consider the following:

  1. MysqlDataSource dataSource should be marked as private and then you should have public getter for it
  2. Connection getConnection() should be defined in your class Database
  3. You should have constructors and field getters and setters in your class Database

Please note that the above-mentioned points are just a few recommendations and it is not a complete list. I recommend you understand OOPS concept well and apply it in all your programs.

Sign up to request clarification or add additional context in comments.

1 Comment

The server is running but still the same problem. When I am opening the Mysql Workbench and right clicking on the blank area, there is a option to "to add connections from clipboard"(Although I don't know what it is doing), it is returning an error :- "Could not parse connection parameters from string "Give the quiz" (which is the password)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.