Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

java - Why my data cannot be insert into database?

I cannot save the input into my database. Can anybody help me? There's SSL problem but it is able to solve by setting SSL to false, but the data seems like only able to read but not save into my database.

package first;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Connection;
import javax.swing.JOptionPane;

public class CustomersForm extends javax.swing.JFrame {
    public CustomersForm() {
        initComponents();
    }

    private void jButton_addcusActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String connectionURL = "jdbc:mysql://localhost:3308/java_his_db";
            Connection con = DriverManager.getConnection(connectionURL, "root", "");
            String sql = "Insert into customers ('fname') values (?)";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(2, jTextField_fname.getText());
            ps.executeUpdate();
            System.out.println("YES");
            con.close();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(rootPane, "Blank or Wrong User Profile", "Insert Error", 2);
        }  
    }                                                         

    private void jTextField_fnameActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        // TODO add your handling code here:
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have only one placeholder in your query, therefore in this statement the index should be one: ps.setString(1, jTextField_fname.getText());


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...