开发者

How can I save data from JTextField into the mysql database?

import java.awt.Panel;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import com.mysql.jdbc.Connection;

public class one {

    JFrame frame = new JFrame("ghar hisab");
    JButton b = new JButton("save");
    Panel p = new Panel();
    JTextField f = new JTextField(20);
    JTextField f1 = new JTextField(20);
    JLabel l = new JLabel("Enter the first name");
    JLabel l1 = new JLabel("Enter the first name");
    String s1,s2;
    String ppl;
    int people;

    void display() throws Exception{
        p.add(l);
        p.add(f);
        p.add(l1);
        p.add(f1);
        p.add(b);

        frame.setSize(400,400);

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(p);
        s1=f.getText();
        s2=f1.getText();


        Class.forName("com.mysql.jdbc.Driver");
        Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/people","root","desire");

        Statement stat = con.createStatement();
        //  String s3= "insert into name values s1 +开发者_如何学Go s2";
        //  stat.executeUpdate(s3);

        stat.executeQuery("insert into name (first,last) values('"s1"','"s2"')");


        //  ResultSet rs= stat.executeQuery("insert into name (first,last) values("arun","yadav"));


        //while(rs.next()){
        //      System.out.println(rs.getString(1)+" "+rs.getString(2));
        //  }
    }
}

When I try to update the database using s1 and s2 an error occurs.


Not sure this can be the issue but your stat.executeQuery(...) statement is wrong it should be:

stat.executeQuery("insert into name (first,last) values('"+s1+"','"+s2+"')");

To concat string with variables use + operator.


Copy and paste this code into your project it is 100% work

data insert method:

public void insert()
{   
    try {
    // connection string
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/Project?user=root&password=root");
        Statement st = con.createStatement();

        st.executeUpdate("insert into Register VALUES('"
                + tf1.getText() + "','" + tf2.getText() + "','"
                + tf3.getText() + "','" + cb1.getSelectedItem()
                + "','" + tf4.getText() + "','" + tf5.getText()
                + "','" + tf6.getText() + "','" + tf7.getText()
                + "','" + tf8.getText() + "','" + tf9.getText()
                + "'," + "'" + tf10.getText() + "','"
                + tf11.getText() + "','" + tf12.getText() + "','"
                + tf13.getText() + "','" + tf14.getText() + "',"
                + "'" + tf15.getText() + "','" +tf16.getText()
                + "','" + tf17.getText() + "','" + tf18.getText()
                + "'," + "'" + tf19.getText() + "','"
                + p1.getText() + "','" + p2.getText() + "')");

        JOptionPane.showConfirmDialog(null, "Your Data Has been Inserted",
                "Result", JOptionPane.DEFAULT_OPTION,
                JOptionPane.PLAIN_MESSAGE);

        st.close();
        con.close();

    }

    catch (Exception e1)

    {
        System.out.println("Exception:" + e1);
    }

}

Then call the method using button:

        b1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            insert();
        }

    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜