Task

Create a table to store addresses.

You may assume that all the addresses to be stored will be located in the USA.   As such, you will need (in addition to a field holding a unique identifier) a field holding the street address, a field holding the city, a field holding the state code, and a field holding the zipcode.   Choose appropriate types for each field.

For non-database languages, show how you would open a connection to a database (your choice of which) and create an address table in it. You should follow the existing models here for how you would structure the table.




/* NetRexx */
options replace format comments java crossref symbols binary
 
import java.sql.Connection
import java.sql.Statement
import java.sql.SQLException
import java.sql.DriverManager
 
class RTableCreate01 public
  properties private constant
    addressDDL = String '' -
    ' create table Address' -
    ' (' -
    '   addrID     integer     primary key generated by default as identity,' -
    '   addrStreet varchar(50) not null,' -
    '   addrCity   varchar(50) not null,' -
    '   addrState  char(2)     not null,' -
    '   addrZip    char(10)    not null' -
    ' )'
    driver = String 'org.apache.derby.jdbc.EmbeddedDriver'
    dbName = String 'db/rosetta_code'
 
  method createTable() public static
    connectionURL = String 
    conn = java.sql.Connection
    sqlStatement = java.sql.Statement
    do
      Class.forName(driver)
      connectionURL = 'jdbc:derby:' || dbName || ';' || 'create=true'
      conn = DriverManager.getConnection(connectionURL)
      sqlStatement = conn.createStatement()
      say 'Creating table'
      sqlStatement.execute(addressDDL)
      say 'Table creation complete'
      sqlStatement.close()
      conn.close()
      do
        -- In embedded mode, an application should shut down Derby.
        -- Shutdown throws the XJ015 exception to confirm success.
        connectionURL = 'jdbc:derby:' || ';' || 'shutdown=true'
        DriverManager.getConnection(connectionURL)
      catch sex = SQLException
        if sex.getSQLState().equals("XJ015") then do
          say 'Database shut down normally'
          end
        else do
          say 'Database did not shut down normally'
          signal sex
          end
      end
    catch sex = SQLException
      sex.printStackTrace()
    catch ex = ClassNotFoundException
      ex.printStackTrace()
    end
    return
 
  method main(args = String[]) public static
    createTable()
    return
 