Someone can explain, I will be very grateful.

Interested in the question, as in the "GUI Form", count the values ​​from the textField and put in the database.

Here is my code:

package diplom.First; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; ; /** * Created by Nika on 26.01.2017. */ public class BD extends JFrame { private JPanel panel; private JButton button1; private JButton Button2; private JButton Button3; private JTextField textField1; private JTextField textField2; private JTextField textField3; private JButton Button4; public static Connection conn; public static Statement statmt; public static ResultSet resSet; public BD() { setContentPane(panel); panel.setPreferredSize(new Dimension(1300,1300)); setVisible(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { Conn(); } catch (Exception u) {} } }); Button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { CreateDB(); } catch (Exception u) { System.out.println (u); } } }); Button3.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { ReadDB(); } catch (Exception u) {} } }); Button4.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String Name = textField1.getText(); int phone = Integer.parseInt(textField2.getText()); int age = Integer.parseInt(textField3.getText()); System.out.println(phone+""+age+""+Name); } }); } // --------ПОДКЛЮЧЕНИЕ К БАЗЕ ДАННЫХ-------- public static void Conn() throws ClassNotFoundException, SQLException { conn = null; Class.forName("org.sqlite.JDBC"); conn = DriverManager.getConnection("jdbc:sqlite:TEST1.s3db"); System.out.println("База Подключена!"); } // --------Создание таблицы-------- public void CreateDB() throws ClassNotFoundException, SQLException { statmt = conn.createStatement(); statmt.execute("CREATE TABLE if not exists 'users' ('id' INTEGER PRIMARY KEY AUTOINCREMENT, 'name' text, 'phone' INT, 'age' INT);"); String name = textField1.getText(); int phone = Integer.parseInt(textField2.getText()); int age = Integer.parseInt(textField3.getText()); //statmt.execute("INSERT INTO 'users' ('name', 'phone','age') VALUES (textField1.getText(), Integer.parseInt(textField2.getText()),Integer.parseInt(textField3.getText())) "); //statmt.execute("INSERT INTO 'users' ('name', 'phone','age') VALUES (name, phone,age) "); statmt.execute("INSERT INTO 'users' ('name', 'phone','age') VALUES ('Vasya', 121,2313) "); System.out.println("Таблица создана или уже существует."); } // -------- Вывод таблицы-------- public static void ReadDB() throws ClassNotFoundException, SQLException { resSet = statmt.executeQuery("SELECT * FROM users"); while(resSet.next()) { int id = resSet.getInt("id"); String name = resSet.getString("name"); String phone = resSet.getString("phone"); String age = resSet.getString("age"); System.out.println( "ID = " + id ); System.out.println( "name = " + name ); System.out.println( "phone = " + phone ); System.out.println( "age = " + age ); System.out.println(); } System.out.println("Таблица выведена"); resSet.close(); conn.close(); statmt.close(); } public static void main(String[] args) { new BD(); } } 

I tried it this way and that, but in the first case, it doesn’t do that.

 statmt.execute("INSERT INTO 'users' ('name', 'phone','age') VALUES (textField1.getText(), Integer.parseInt(textField2.getText()),Integer.parseInt(textField3.getText())) 

"java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (near" (": syntax error)"

It simply does not see these variables.

 statmt.execute("INSERT INTO 'users' ('name', 'phone','age') VALUES (name, phone,age) 

The topic is completely new, I will be glad if in detail, and clearly explain. Thank.

    1 answer 1

    If you add it this way, it is not correct, try this:

     statmt.execute("INSERT INTO `users` (`name`, `phone`, `age`) VALUES ('" + textField1.getText() + "', '" + Integer.parseInt(textField2.getText()) + "', '" + Integer.parseInt(textField3.getText() + "');"); 
    • I 'll try, ask - mrchebik
    • Yes, thanks so it works, there are questions, you can explain. '"+ text +"' - How it works, for it has recently begun. As I understood single quotes display our fields or text value. But why double? And why should we put "+" on the sides of the textUdite, because we use (',') to divide the fields where our data are entered. I hope you understand the idea that I want to ask you, for it is even hard to describe)) - Baffik
    • PS: I want to go to the next line using "Enter", but it sends a message) - Baffik
    • I can assume they serve as a kind of non-screened characters. - Baffik
    • You studied the lines? In the inside execute, you pass the string. To insert any data from a variable into a string, you must close the string with double quotes, put a '+', then the variable name, then again '+' and open double quotes if you want to add a string. - mrchebik