N
N
Neonoviiwolf2016-08-12 19:21:29
MySQL
Neonoviiwolf, 2016-08-12 19:21:29

How to enter data into MySQL DB from TextField in java?

Good!
I'm trying to figure out how to work with a MySQL database. So, there is a TextField txtName field

statement.execute("INSERT INTO deftab (name ) VALUE (txtName.getText().toString)");

how to put values ​​in there?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MrBe, 2016-08-13
@Neonoviiwolf

Use PreparedStatement

....
private static final String INSERT = "INSERT INTO deftab (name ) VALUE (?)";
....
public void insert(String name) {
 try (Connection connection = DBConnectionPool.getConnection();
             PreparedStatement ps = connection.prepareStatement(INSERT_NEWS)) {
            ps.setString(1, name);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
}

*DBConnectionPool - the place from where you will take the connection
To study:
JDBC lesson in
PreparedStatement examples (prepared queries)
How to use the MySQL database in Java
The simplest Connection pool without a DataSource in Java

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question