Deleting a Row from a Database Table : Statement « Database « Java Tutorial






import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class Main {
  public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    Statement stmt = connection.createStatement();
    String sql = "DELETE FROM my_table WHERE col_string='a string'";
    int deleteCount = stmt.executeUpdate(sql);
    sql = "DELETE FROM my_table WHERE col_string=?";
    PreparedStatement pstmt = connection.prepareStatement(sql);
    pstmt.setString(1, "a string");
    deleteCount = pstmt.executeUpdate();
  }
}








20.5.Statement
20.5.1.Change the fetch size on the result set
20.5.2.Get the fetch size of a statement
20.5.3.Set the fetch size on the statement
20.5.4.Retrieving All Rows from a Database Table
20.5.5.Getting Column Names from a database table in Java
20.5.6.Retrieve a rowcount from a ResultSet
20.5.7.Setting the Number of Rows to Prefetch When Executing a SQL Query
20.5.8.Create a result set
20.5.9.Creating a Database Table called my_table with one column, col_string, which holds strings.
20.5.10.Deleting a Database Table called my_table from a database.
20.5.11.Inserting a Row into a Database Table
20.5.12.Inserting a Row into a Database Table Using a Prepared Statement
20.5.13.Deleting a Row from a Database Table
20.5.14.Statement Batch Update