

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.derby.jdbc.EmbeddedSimpleDataSource;  // from derby.jar


public class DerbyVersion1 {

    public static void main(String[] args) {
        
        DerbyVersion1 demo = new DerbyVersion1();
       
        demo.runDemo();
    }
    
    private void runDemo() {
        
      
        
        EmbeddedSimpleDataSource ds = new EmbeddedSimpleDataSource();
        
        //	the name of the database
        String dbName = "derbyDB9"; 
        ds.setDatabaseName(dbName);
        
        // tell Derby to create the database if it does not already exist
        ds.setCreateDatabase("create"); 
        
        /* We will be using Statement and PreparedStatement objects for 
         * executing SQL. These objects are resources that should be released 
         * explicitly after use, hence the try-catch-finally pattern below.
         */
        Connection conn = null;
        Statement s = null;        
        ResultSet rs = null;    // used for retreiving the results of a query
     
          
            try {
				conn = ds.getConnection();
				System.out.println("Connected to and created database " + dbName);
				
				/* Creating a statement object that we can use for running various
				 * SQL statements commands against the database.*/
				s = conn.createStatement();

         
				
				// 	Create a table
				s.executeUpdate("CREATE TABLE restaurants ( id INTEGER,  aaaa VARCHAR (30), bbbb VARCHAR (30))");
				System.out.println("Created table");
				
				// Insert some rows...
				
				String tableName = "restaurants";
				int id = 10;
				String restName = "Traube Tonbach";
				String cityName = "Tonbach";
				s.execute("insert into " + tableName + " values (" +
				        id + ",'" + restName + "','" + cityName +"')");
				System.out.println("Inserted data");
         
				
        
				
        
				
         //	Abfragen
				rs = s.executeQuery("select * from " + tableName);
				
				 while(rs.next())
		            {
		                int idQ = rs.getInt(1);
		                String restNameQ = rs.getString(2);
		                String cityNameQ = rs.getString(3);
		                System.out.println(idQ + "\t\t" + restNameQ + "\t\t" + cityNameQ);
		            }
				 rs.close();
		         s.close();
				
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

           
   }

}
