Welcome to OGeek Q&A 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...