简体   繁体   English

Java插入MySQL工作台

[英]Java insert to mySQL workbench

I have a mySQL database named Customer that I am trying to insert data from a textfield in a GUI. 我有一个名为Customer的mySQL数据库,我试图从GUI中的文本字段插入数据。 I ran the code below and in the console it said "Connected Successfully" with no errors. 我在下面运行了代码,并在控制台中显示了“成功连接”,没有错误。 I checked to see if the information had been inserted into the Customer table of my mySQL database and nothing was inserted. 我检查了一下信息是否已插入到mySQL数据库的Customer表中,而未插入任何内容。 Does anyone know why? 有人知道为什么吗?

         button.setOnAction(e -> {


         Connection dbConnection = null;
            PreparedStatement preparedStatement = null;

         try {


           Customer cust = new Customer();
          dbConnection = Connect();
            String sql="Insert into CIS3270.Customer(firstName,lastName, email,userNAME,Address,Zip,State,SecurityQ,  Password, ConfirmPassword,SSN)VALUES (?,?,?,?,?,?,?,?,?,?,?)"; 
            preparedStatement =  dbConnection.prepareStatement(sql);


            preparedStatement.setString(1,cust.getFirstName()); 
            preparedStatement.setString(2,cust.getLastName()); 
            preparedStatement.setString(3,cust.getEmail()); 
            preparedStatement.setString(4,cust.getUserNAME()); 
            preparedStatement.setString(5,cust.getAddress());
            preparedStatement.setString(6,cust.getZip()); 
            preparedStatement.setString(7,cust.getState());
            preparedStatement.setString(8,cust.getSecurityQuestion());
            preparedStatement.setString(9,cust.getPassWORD()); 
            preparedStatement.setString(10,cust.getConfirmPassword());
            preparedStatement.setString(11,cust.getSSN()); 

            preparedStatement.executeBatch(); 
            preparedStatement.executeUpdate();

            dbConnection.close(); 
            preparedStatement.close(); 



        LoginScreen loginPage = new LoginScreen();

        loginPage.start(primaryStage);

         }
         catch(Exception e1) {
            e1.printStackTrace();
         }
    });

       public static Connection Connect() {
       Connection con = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = (Connection) DriverManager.getConnection("jdbc:mysql://(ip adress):3306/CIS3270", "root", "password");
    } catch (Exception e) {
        System.out.println("Can not connect");
    }
    if (con != null) {
        System.out.println("Connected Successfully");
    }
    return con;
}

You have no need for a batch insert since you're inserting a single row. 由于您要插入单行,因此无需批量插入。

The reason your code doesn't do anything is because you're executing an empty batch. 您的代码不执行任何操作的原因是因为您正在执行一个空批处理。 You need to call preparedStatement.addBatch() too. 您还需要调用preparedStatement.addBatch() Or just remove that executeBatch() call, as you seem to call executeUpdate() too. 或只需删除该executeBatch()调用,就好像您也调用了executeUpdate()

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

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