繁体   English   中英

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

[英]What is the SQL query to create a database?

正在用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;
}

我为我的应用程序使用了一个名为“项目”的数据库。 下面给出了使用“用户名”创建表并向其中插入值的代码:

 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'的值创建数据库?

尝试这样的事情,

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM