如何将文本从JTextFields发送到mysql数据库

时间:2015-02-02 21:34:35

标签: mysql

每当我运行此代码时,它会说java.sql.SQLException:Column' s'未找到。 任何人都可以帮我解决这个问题。我已经检查了堆栈溢出中的相同问题,但我找不到解决方案。

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Scanner;
    import javax.swing.*;
    import java.sql.*;
public class project extends JFrame
{
        static JLabel label1;
        static JLabel label2;
        static JLabel label3;
        static JLabel label4;
        static JButton b2;
        static JTextField text1;
        static JTextField text2;
        static JTextField text3;
    public static void main(String [] args)
        {

            JFrame F= new JFrame();
            JPanel P = new JPanel(new GridLayout(0,1));
            F.add(P);
            P.add(new JLabel("NAME"));
            text1=new JTextField();
            P.add(text1);
            P.add(new JLabel("ADDRESS"));
            text2=new JTextField();
            P.add(text2);
            P.add(new JLabel("AGE"));
            text3=new JTextField();
            P.add(text3);
            P.add(new JLabel("GENDER"));
            b2=new JButton("Submit");
            P.add(b2);
            //Code for functioning 'Submit' button
            b2.addActionListener(new ActionListener(){ 

                @Override
                public void actionPerformed(ActionEvent e){
                    try
                    {

                    Connection C= DriverManager.getConnection("jdbc:mysql://localhost:3306/first","root","root");   
                    Statement S=C.createStatement();
                    ResultSet R=S.executeQuery("SELECT * FROM demo");
                    while(R.next())
                    {
                        String s=text1.getText();
                        String s1=text2.getText();
                        String s2=text3.getText();
                        System.out.println(R.getString("s"));
                        System.out.println(R.getString("s1"));
                        System.out.println(R.getString("s2"));
                    }
                    }
                    catch(Exception ea)
                    {
                System.out.println(ea);     
                    }

                }

            });


            F.setLayout(new FlowLayout());
            F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            F.setSize(400,750);
            F.setVisible(true);
        }

    }

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作,而不是打印ResultSet中的值:

text1.setText(R.getString("s"));
text2.setText(R.getString("s1"));
text3.setText(R.getString("s2"));

但如果查询中有值列表,则代码应如下所示:

StringBuffer field1 = new StringBuffer();
StringBuffer field2 = new StringBuffer();
StringBuffer field3 = new StringBuffer();

while(R.next())
{   
    field1.append(R.getString("s") + "\n");
    field2.append(R.getString("s1") + "\n");
    field3.append(R.getString("s2") + "\n");
}

text1.setText(field1);
text2.setText(field2));
text3.setText(field3);