创建数据库的SQL查询是什么?

时间:2014-04-07 11:28:40

标签: mysql database javafx javafx-2

正在开发javafx中的应用程序。我使用MySQL作为后端,我使用NetBeans IDE。我试图为用户创建一个帐户。这是收集数据以从用户创建用户帐户的表单代码:

 private VBox tutorSignUp() {

    VBox vb1 = new VBox();
    vb1.setPadding(new Insets(15, 150, 10, 150));
    vb1.setSpacing(5);

    Text txt1 = new Text("Sign Up");
    txt1.setFont(Font.font("Tahoma", FontWeight.BOLD, 20));

    Label lb1 = new Label("Full Name:");
    lb1.setFont(Font.font("Calibri", FontWeight.BOLD, 17));

    t11 = new TextField();
    t11.setPrefSize(200, 25);

    Label lb2 = new Label("User Name:");
    lb2.setFont(Font.font("Calibri", FontWeight.BOLD, 17));

    t12 = new TextField();
    t12.setPrefSize(200, 25);

    Label lb3 = new Label("Password:");
    lb3.setFont(Font.font("Calibri", FontWeight.BOLD, 17));

    p11 = new PasswordField();
    p11.setPrefSize(200, 25);

    Label lb4 = new Label("Department:");
    lb4.setFont(Font.font("Calibri", FontWeight.BOLD, 17));

    t13 = new TextField();
    t13.setPrefSize(200, 25);

    Label lb5 = new Label("Semester:");
    lb5.setFont(Font.font("Calibri", FontWeight.BOLD, 17));

    ObservableList<String> options2
            = FXCollections.observableArrayList(
                    "1", "2", "3", "4", "5", "6");
    comboBox = new ComboBox(options2);
    comboBox.setPrefSize(410, 30);

    Button signup = new Button("Sign Up");
    signup.setFont(Font.font("Calibri", FontWeight.NORMAL, 17));
    signup.setStyle(" -fx-base: #333333;");
    signup.setTextFill(Color.WHITE);

    signup.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            try {                     
                createAccount();
            } 
            catch (ClassNotFoundException | SQLException ex) {
                Logger.getLogger(FrontPage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });

    vb1.getChildren().addAll(txt1, lb1, t11, lb2, t12, lb3, p11, lb4, t13,
            lb5, comboBox, signup);
    return vb1;
}

我为我的应用程序使用名为'project'的数据库。使用“用户名”创建表并向其插入值的代码如下:

 public void createAccount() throws ClassNotFoundException, SQLException {
    try {
        Class.forName("com.mysql.jdbc.Driver");

        connect = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/project?"
                        + "user=root&password=virus");
        statement = connect.createStatement();

        preparedStatement = connect
                .prepareStatement("create table " + t12.getText() + " (fullname varchar(30),"
                        + "username varchar(20) primary key, password varchar(20),"
                        + "department varchar(30), semester int(2));");
        preparedStatement.executeUpdate();

        preparedStatement = connect
                .prepareStatement("insert into " + t12.getText() + " values(?,?,?,?,?);");

        preparedStatement.setString(1, t11.getText());
        preparedStatement.setString(2, t12.getText());
        preparedStatement.setString(3, p11.getText());
        preparedStatement.setString(4, t13.getText());
        preparedStatement.setString(5, (String)comboBox.getValue());

        preparedStatement.executeUpdate();

    } catch (ClassNotFoundException | SQLException e) {
        throw e;
    } finally {
        close2();
    }

}

private void close2() {
    try {

        if (statement != null) {
            statement.close();
        }

        if (connect != null) {
            connect.close();
        }
    } catch (SQLException e) {

    }
}

但我的想法现在不同了。首先,我需要使用“用户名”创建数据库,即使用TextField“t12”的值。然后我将在此数据库中创建表。我知道如何在数据库中创建表。

如何使用TextField't12'的值创建数据库?

1 个答案:

答案 0 :(得分:3)

尝试这样的事情,

    try{
        String dbName = t12.getText();//assuming dbName is t12.getText();
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/","root","");//use your username and password for connection
        Statement statement = con.createStatement();
        int resultset = statement.executeUpdate("create database " + dbName );
    }catch(SQLException e){
        e.printStackTrace();
    }catch(ClassNotFoundException e){
        e.printStackTrace();
    }