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.