Java/Database SQL JDBC/Insert

Материал из Java эксперт
Перейти к: навигация, поиск

Insert a record into database table

  
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Main {
  public static void main(String[] args) throws Exception {
    Connection connection = null;
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://localhost/testdb";
    String user = "root";
    String password = "";
    connection = DriverManager.getConnection(url, user, password);
    Statement stmt = connection.createStatement();
    String sql = "INSERT INTO users (name, address) VALUES ("Foo", "Street")";
    stmt.execute(sql);
    connection.close();
  }
}