Create a Table

Previous      Next

import java.sql.*;
     
public class CreateCoffees
{
   public static void main(String args[])
   {
      // Example: jdbc:odbc:Coffee
      String url = "jdbc:mySubprotocol:myDataSource";
      Connection con;
      String createString;
      createString = "create table COFFEES " +
                     "(COF_NAME varchar(32), " +
                     "SUP_ID int, " +
                     "PRICE float, " +
                     "SALES int, " +
                     "TOTAL int)";
      // Statements go to the DBMS
      Statement stmt;
   
      try
      {
         // Example: sun.jdbc.odbc.JdbcOdbcDriver
         Class.forName("myDriver.ClassName");
      }
      catch(java.lang.ClassNotFoundException e)
      {
         System.err.print("ClassNotFoundException: "); 
         System.err.println(e.getMessage());
      }

      try
      {
         con = DriverManager.getConnection(url, "myLogin", "myPassword");
   
         stmt = con.createStatement();                     
         stmt.executeUpdate(createString);  // Create/modify table (DDL)
   
         stmt.close();
         con.close();
   
      }
      catch(SQLException ex)
      {
         System.err.println("SQLException: " + ex.getMessage());
      }
   }
}