Drop Table JDBC

Drop Table JDBC

Drop Table JDBC

Following Drop Table JDBC code deletes the table Employee. All the records also deleted with the table. drop table SQL command is used.
Example on Drop Table JDBC

Let us explain on 3 JDBC statements of Drop Table JDBC.
  1. Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

    forName(String) is a static method of class Class whose job is to load the driver into the RAM from hard disk. Here, the driver is a Java class by name JdbcOdbcDriver which is available in the package, odbc, a subpackage of jdbc, and again a subpackge of sun.

  2. DriverManager.getConnection(“jdbc:odbc:snrao”, “scott”, “tiger”);

    getConnection(String, String, String) is a static method of DriverManager class which establishes a connection with the database provided the 3 string parameters passed are correct.

    1. jdbc:odbc:snrao

      odbc is the subprotocol of protocol jdbc. snrao is the DSN(Data Source Name). DSN is to be created by the programmer.

    2. scott
      It is the user name to open the database
    3. tiger
      It is the password to open the database
  3. Statement stmt = con.createStatement( );

    The createStatement() method of Connection interface returns an object of Statement interface. This object is used by the programmer to send a sql command to the database and retrieve the data from the database. This object job is just to move between Java and Oracle like a courier service.

  4. stmt.executeUpdate(“create table Employee(empid number, empname varchar2(15), empsal number(6,2))”);

    executeUpdate(String) method of Statement interface takes a string as parameter and delivers to the database. The string is nothing but a SQL command which actually we type at SQL prompt.

  5. stmt.close();

    When the job is over, close the database connections, first stmt and then con.

  6. The exceptions thrown by different methods in the above program:
    1. forName( ) : throws ClassNotFoundException. This is thrown when the JdbcOdbcDriver class is not found.
    2. getConnection( ) : throws SQLException. This is thrown when unable to establish a connection, if we pass wrong parameters.
    3. createStatement( ) : throws SQLException. This is thrown when the Connection interface finds a problem to return the Statement object.
    4. executeUpdate( ) : throws SQLException. This is thrown when unable to deliver the sql command.
    5. close( ) : throws SQLException. This is thrown when there is a problem to close the database.

Instead of throwing Exception, realtime applications use try-catch-finally and a good example is shown in JDBC Example – Create Table.

Leave a Reply

Your email address will not be published. Required fields are marked *