java - 为什么显示 "No database Selected"SQLException

标签 java mysql jdbc

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

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

}

最佳答案

而不是这个

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

尝试

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

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

关于java - 为什么显示 "No database Selected"SQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21707689/

相关文章:

php - 在 joomla 中上传后将文件路径保存到数据库中

java - 无法使用 Java 更新 mySQL 数据库中的行,没有错误消息

java - 如何更新 p :selectCheckboxMenu without the component being closed after ajax call in primefaces? 的标签

java - Java多线程同步用法

mysql - 寻找存档数据的数据库系统

mysql - 从 Apache Spark 访问带有文本列的 MySql 表

java - ORMLite MySQL 初始化似乎不起作用

java - 如何在虚拟机退出后创建临时文件并删除

java - 如何使用 Gradle 构建 Maven 项目作为根项目的依赖项?

java - 由 : java. sql.SQLException 引起:用户 'root' @'localhost' 的访问被拒绝(使用密码:YES)