为什么显示“未选择数据库”SQLException

时间:2014-02-11 16:38:42

标签: java mysql jdbc

我有一个代码可以在'mysql'数据库中的'createaccount'表中输入一些数据。我使用NetBeans IDE创建了它。表和数据库都已创建。每当我运行代码时,都会显示一个SQLException,显示“No Database Selected”。我将在下面提供我的代码。为什么会出现此错误?我该怎么做才能解决它?

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);
}

}

3 个答案:

答案 0 :(得分:2)

而不是这个

insert into createaccount values (?, ?, ?, ?)

insert into YorDbName.createaccount values (?, ?, ?, ?)

YorDbName替换为您的数据库名称。

答案 1 :(得分:1)

“com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:关键字'PRIMARY'的重复条目'Tom33'”

这意味着您的Tom33条目已插入数据库或已存在,您尝试再次插入相同的值。主键约束限制我们重复值。

答案 2 :(得分:0)

更改,

try {

} catch (ClassNotFoundException | SQLException e) {


}

作为

try {

} catch (SQLException e) { 


} catch (ClassNotFoundException e) {

}

并改变

statement = connect.createStatement();

作为

preparedStatement = connect.createStatement();