title: Create derby database, tables using java code
body: <p>I downloaded derby in C:\ directly and set the DERBY_INSTALL and CLASSPATH variables:</p>
  
  <pre><code>C:\&gt;set DERBY_INSTALL=C:\Apache\db-derby-10.9.1.0-bin
  C:\&gt;set CLASSPATH=%DERBY_INSTALL%\lib\derby.jar;%DERBY_INSTALL%\lib\derbytools.jar;.
  C:\&gt; cd %DERBY_INSTALL%\bin
  C:\db-derby-10.9.1.0-bin\bin&gt;java org.apache.derby.tools.ij
  ij version 10.9
  ij&gt;
  </code></pre>
  
  <p>So it shows ij> and I am able to create a database using command line SQL statements. But I have to create the database and its tables using a Java code. So I the source code below but I dont know where to paste it and how to run it. When I pasted it in the db-derby-10.9.1.0-bin>bin folder and tried to run it within that folder using 'java MainClass komaldb C:\' it shows could not find or load main class MainClass. I am not sure where do I have to run the java program from.</p>
  
  <p>Java Source Code:</p>
  
  <pre><code>import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.Statement;
  
  public class MainDB {
  static Connection conn;
  
  public static void main(String[] args) throws Exception {
  if (args.length != 2) {
    System.out.println("Usage: java JavaDBDemo &lt;Name&gt; &lt;Address&gt;");
    System.exit(1);
  }
  String driver = "org.apache.derby.jdbc.EmbeddedDriver";
  String dbName = "AddressBookDB";
  String connectionURL = "jdbc:derby:" + dbName + ";create=true";
  String createString = "CREATE TABLE ADDRESSBOOKTbl (NAME VARCHAR 32) NOT NULL, 
  ADDRESS VARCHAR(50) NOT NULL)";
  Class.forName(driver);
  
  conn = DriverManager.getConnection(connectionURL);
  
  Statement stmt = conn.createStatement();
  stmt.executeUpdate(createString);
  
  PreparedStatement psInsert = conn
      .prepareStatement("insert into ADDRESSBOOKTbl values (?,?)");
  
  psInsert.setString(1, args[0]);
  psInsert.setString(2, args[1]);
  
  psInsert.executeUpdate();
  
  Statement stmt2 = conn.createStatement();
  ResultSet rs = stmt2.executeQuery("select * from ADDRESSBOOKTbl");
  System.out.println("Addressed present in your Address Book\n\n");
  int num = 0;
  
  while (rs.next()) {
    System.out.println(++num + ": Name: " + rs.getString(1) + "\n Address"
        + rs.getString(2));
  }
  rs.close();
  }
  }
  </code></pre>