Reputation: 1813
I have a code to enter some data to 'createaccount' table in 'mysql' database. I created it using NetBeans IDE. Both the table and the database are created. Whenever I run the code a SQLException saying "No Database Selected" is showing. I will provide my code below. Why is this error showing ? What should I do to solve it ?
package login;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.geometry.Insets;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class Login extends Application {
TextField t1,t2;
PasswordField t3;
ComboBox comboBox2;
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
@Override
public void start(Stage stage) {
BorderPane border = new BorderPane();
border.setTop(loginHBox1());
border.setLeft(loginVBox1());
border.setRight(loginVBox2());
Scene scene = new Scene(border,700,450);
stage.setScene(scene);
stage.setResizable(false);
scene.getStylesheets().add
(Login.class.getResource("Login.css").toExternalForm());
stage.show();
}
private HBox loginHBox1() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 10, 180));
hbox.setSpacing(10); // Gap between nodes
Label lb1=new Label("LOG IN OR CREATE NEW ACCOUNT");
lb1.setAlignment(Pos.CENTER);
lb1.setFont(Font.font("Calibri",FontWeight.BOLD,26));
lb1.setTextFill(Color.BLACK);
hbox.getChildren().addAll(lb1);
return hbox;
}
private VBox loginVBox1() {
VBox hbox = new VBox();
hbox.setPadding(new Insets(20,30,15,50)); // Set all sides to 10
hbox.setSpacing(10); // Gap between nodes
Label lb3=new Label("LOG IN");
lb3.setAlignment(Pos.CENTER);
lb3.setFont(Font.font("Calibri",FontWeight.BOLD,24));
lb3.setTextFill(Color.BLACK);
Label lb1=new Label("Username");
lb1.setAlignment(Pos.CENTER);
lb1.setFont(Font.font("Calibri",FontWeight.BOLD,16));
lb1.setTextFill(Color.BLACK);
TextField t11=new TextField();
t11.setPrefSize(150,30);
Label lb2=new Label("Password");
lb2.setAlignment(Pos.CENTER);
lb2.setFont(Font.font("Calibri",FontWeight.BOLD,16));
lb2.setTextFill(Color.BLACK);
PasswordField pw11=new PasswordField();
pw11.setPrefSize(150,30);
Button b1=new Button("LOG IN");
b1.setFont(Font.font("Calibri",FontWeight.BOLD,16));
b1.setPrefSize(80,5);
hbox.getChildren().addAll(lb3,lb1,t11,lb2,pw11,b1);
return hbox;
}
private VBox loginVBox2()
{
VBox hbox1 = new VBox();
hbox1.setPadding(new Insets(15, 50, 15, 10));
hbox1.setSpacing(10);
Label lb4=new Label("CREATE NEW ACCOUNT");
lb4.setFont(Font.font("Calibri",FontWeight.BOLD,24));
lb4.setPrefSize(250,30);
lb4.setTextFill(Color.BLACK);
Label lb1=new Label("Full Name ");
lb1.setFont(Font.font("Calibri",FontWeight.BOLD,18));
lb1.setPrefSize(100, 30);
lb1.setTextFill(Color.BLACK);
t1=new TextField();
t1.setPrefSize(50,30);
Label lb2=new Label("User Name ");
lb2.setFont(Font.font("Calibri",FontWeight.BOLD,18));
lb2.setPrefSize(150, 30);
lb2.setTextFill(Color.BLACK);
t2=new TextField();
t2.setPrefSize(100,30);
Label lb3=new Label("Password ");
lb3.setFont(Font.font("Calibri",FontWeight.BOLD,18));
lb3.setPrefSize(150, 30);
lb3.setTextFill(Color.BLACK);
t3=new PasswordField();
t3.setPrefSize(100,30);
Label lb5=new Label("Gender ");
lb5.setFont(Font.font("Calibri",FontWeight.BOLD,18));
lb5.setPrefSize(150, 30);
lb5.setTextFill(Color.BLACK);
ObservableList<String> options2 =
FXCollections.observableArrayList(
"Male","Female");
comboBox2 = new ComboBox(options2);
comboBox2.setPrefSize(250,30);
Button btn1=new Button("CREATE");
btn1.setFont(Font.font("Calibri",FontWeight.BOLD,18));
btn1.setPrefSize(100,30);
btn1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
try {
createAccount();
} catch ( ClassNotFoundException | SQLException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
hbox1.getChildren().addAll(lb4,lb1,t1,lb2,t2,lb3,t3,lb5,comboBox2,btn1);
return hbox1;
}
public void createAccount() throws ClassNotFoundException, SQLException
{
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://localhost:3306/mysql?"
+ "user=root&password=virus");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// PreparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement("insert into createaccount values (?, ?, ?, ?)");
// "myuser, webpage, datum, summary, COMMENTS from FEEDBACK.COMMENTS");
// Parameters start with 1
preparedStatement.setString(1, "Tomin Jacob");
preparedStatement.setString(2, "Tom333");
preparedStatement.setString(3, "pass");
preparedStatement.setString(4, "male");
preparedStatement.executeUpdate();
}
catch (ClassNotFoundException | SQLException e) {
throw e;
} finally {
close();
}
}
private void close() {
try {
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
} catch (SQLException e) {
}
}
public static void main(String[] args)
{
launch(args);
}
}
Upvotes: 0
Views: 305
Reputation: 191
"com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'Tom33' for key 'PRIMARY'"
This means that your entry for Tom33 has been inserted into database or is already existing and you are trying to insert the same value again. Primary key constraint restrains us from having duplicate values.
Upvotes: 1
Reputation: 278
Instead of this
insert into createaccount values (?, ?, ?, ?)
try
insert into YorDbName.createaccount values (?, ?, ?, ?)
Replace YorDbName
with your database name.
Upvotes: 2
Reputation: 6527
Change,
try {
} catch (ClassNotFoundException | SQLException e) {
}
as
try {
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
}
And change
statement = connect.createStatement();
as
preparedStatement = connect.createStatement();
Upvotes: 0