Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The process of inserting information into MySQL using Eclipse involves the following steps:

  1. Set up a MySQL database with a table to hold the data you want to insert.

  2. Create a Java project in Eclipse and add the MySQL JDBC driver to the project's build path.

  3. Write Java code to establish a connection to the MySQL database using the JDBC driver.

  4. Create a PreparedStatement object with a SQL INSERT statement to insert the data into the table.

  5. Set the values of the PreparedStatement's parameters using Java variables or constants.

  6. Execute the PreparedStatement's executeUpdate() method to commit the data to the database.

  7. Close the PreparedStatement and database connection to free up resources.

Here is an example Java code snippet that inserts a new customer into a "Customers" table in a MySQL database:

import java.sql.*;

public class InsertData {

   public static void main(String[] args) {
      String url = "jdbc:mysql://localhost:3306/mydb";
      String username = "root";
      String password = "password";

      try {
         Connection conn = DriverManager.getConnection(url, username, password);

         String sql = "INSERT INTO Customers (CustomerName, ContactName, Country) VALUES (?, ?, ?)";
         PreparedStatement stmt = conn.prepareStatement(sql);
         stmt.setString(1, "John Smith");
         stmt.setString(2, "Mary Brown");
         stmt.setString(3, "USA");

         int rowsInserted = stmt.executeUpdate();
         if (rowsInserted > 0) {
            System.out.println("A new customer was inserted successfully!");
         }

         stmt.close();
         conn.close();

      } catch (SQLException e) {
         e.printStackTrace();
      }
   }
}