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)"