creating database tables

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

HarryDeBoer
Ebase User
Posts: 118
Joined: Tue Oct 23, 2012 7:01 am
Location: The Netherlands

creating database tables

#1

Postby HarryDeBoer » Fri Mar 07, 2014 12:15 pm

Hi

is there a way to create database tables/schema's in the embedded Derby database from within the XiDesigner ? Also for other databases (MS SQL)?
0 x
Kind regards,

Harry

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#2

Postby Jon » Fri Mar 07, 2014 12:54 pm

Harry,

There is a Create Database Table icon on the toolbar within the Database Resource editor. You can use this to create a table from the Database Resource (if the Database Resource represents a single table), or you can use it to just run your own SQL.

If you're doing very much of this, you are probably better of converting to use networked Derby instead of embedded, then you can attach a client to it, set up your own schema, and do anything you want. There should be a new forum post on this in the next couple of days in section Technical How To Documents, but it's a relatively easy thing to do.

Regards
Jon
0 x

Steve
Moderator
Moderator
Posts: 421
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

#3

Postby Steve » Fri Mar 07, 2014 12:56 pm

Hi Harry,

You can create the tables from the Database Resource, create database table.

Create a database resource.
Select the database you wish to create the table on from the list.
Add a field to the resource fields.
Write something in both the select and table boxes.
Press the 'create database table' on the toolbar at the top of the frame.

This will open a box filled in with create table....
You can type your own SQL into here also and click 'Submit SQL' to execute the SQL.

Kind regards

Steve
0 x

HarryDeBoer
Ebase User
Posts: 118
Joined: Tue Oct 23, 2012 7:01 am
Location: The Netherlands

#4

Postby HarryDeBoer » Fri Mar 07, 2014 2:56 pm

Ok,

-I created a database (embedded derby -copied ebase_samples to harry_samples)
-created a resource: first added two fileds (resource fields editor) pressed create table and added some more fileds in the create table sql. Table succesfully created.

Then I noticed that in my form I only saw the two fields (of course correct because in the resource there were only two). I then wanted to recreate the resource (to fetch all fields) with the db schema wizard, but I can't see my table. Probably because of a lack of a schema.

Can I create schema's or am I missing something/did something wrong?
0 x
Kind regards,

Harry

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#5

Postby Jon » Fri Mar 07, 2014 3:39 pm

The schema seems to be taken from the connection user name. For example if I create a table using the schema wizard from the EBASE_SAMPLES database connection, it creates it in the EBASE_SAMPLES schema. If you look at the connection definition in tomcat\conf\Catalina\localhost.ufs.xml, you'll see that the user name is ebase_samples - I think this is being used as the schema name. Same should apply for your database connection.
0 x

HarryDeBoer
Ebase User
Posts: 118
Joined: Tue Oct 23, 2012 7:01 am
Location: The Netherlands

#6

Postby HarryDeBoer » Fri Mar 07, 2014 4:41 pm

Hi Jon,

Alas I'm not seeing a schema with the same name (harry_samples). In what directory could I find the database, tables, etc ?
0 x
Kind regards,

Harry

Jon
Moderator
Moderator
Posts: 1342
Joined: Wed Sep 12, 2007 12:49 pm

#7

Postby Jon » Fri Mar 07, 2014 4:57 pm

The database should be in UfsServer/DB/harry_samples but I don't think that will help. Have you set up a new Database Connection for harry_samples? If yes, what is the user name that you are connecting with - this should be the schema name. Also, when you go into the schema wizard, which schema names do you see displayed?
0 x

HarryDeBoer
Ebase User
Posts: 118
Joined: Tue Oct 23, 2012 7:01 am
Location: The Netherlands

#8

Postby HarryDeBoer » Tue Aug 19, 2014 7:54 pm

Hi,

completely forgot about this post. Sorry for that. However, I cannot (don't know how) create a new database for Derby embedded (or network) on my Windows machine. I cannot find the technical document that should appear in the Technical Howto documents (see Jon's post) too. Could you provide that?
0 x
Kind regards,

Harry

Steve
Moderator
Moderator
Posts: 421
Joined: Fri Sep 07, 2007 3:44 pm
Location: Sandy, UK
Contact:

#9

Postby Steve » Wed Aug 20, 2014 7:14 am

Hi Harry,

You can create a new database just simply by appending create=true to the JDBC url.

You will need to modify the ufs.xml file inside the directory:

<Ebase_Install_Dir>/UfsServer/tomcat/conf/Catalina/localhost

You will need to add a new resource:

e.g
To create a new database called using Derby embedded HARRY_TEST

Code: Select all

<Resource name="jdbc/HARRY_TEST" auth="Container"
              type="javax.sql.DataSource"
              username="harry" password="harry"
              driverClassName="org.apache.derby.jdbc.EmbeddedDriver"
              url="jdbc&#58;derby&#58;HARRY_TEST;create=true"
              max="30" min="5" 
              factory="com.ebase.jndi.DataSourceFactory"
	          validationQuery="SELECT 1 FROM SYSIBM.SYSDUMMY1" checkLevel="1" preparedStatementCache="0"
	      />
For Derby server:

Code: Select all

   <Resource name="jdbc/HARRY_TEST" auth="Container"
              type="javax.sql.DataSource"
              username="harry" password="harry"
              driverClassName="org.apache.derby.jdbc.ClientDriver"
              url="jdbc&#58;derby&#58;//localhost&#58;1527/HARRY_TEST;create=true;"
              max="30" min="0"
              factory="com.ebase.jndi.DataSourceFactory"
	          validationQuery="VALUES 1" checkLevel="2" preparedStatementCache="0"
	      />
Note that the database is created only if it does not exist.

I hope this helps.

Steve
0 x

HarryDeBoer
Ebase User
Posts: 118
Joined: Tue Oct 23, 2012 7:01 am
Location: The Netherlands

#10

Postby HarryDeBoer » Wed Aug 20, 2014 8:29 am

Thanks Steve. Works fine!
0 x
Kind regards,

Harry

Hovik
Moderator
Moderator
Posts: 184
Joined: Tue Sep 11, 2007 8:58 am

#11

Postby Hovik » Wed Aug 20, 2014 10:07 am

Harry,

I have added a new document which you my find useful.

http://forum.ebasetech.com/forum/viewto ... =2345#2345

Regards,
Hovik
0 x

Dave
Ebase Staff
Posts: 89
Joined: Mon Sep 10, 2007 11:48 am

#12

Postby Dave » Thu Aug 21, 2014 8:26 am

...or you could always do this sort of thing:

Code: Select all

var samplesDB = "EBASE_SAMPLES";

// DDL statements to be executed
//
var tableName = "ES120913_COUNTRIES";
if &#40;!dbExists&#40;tableName&#41;&#41; &#123;
	// table doesn't exist so push your DDL statements onto the array and build the     
   // database
   var ddl = &#91;&#93;;
   ddl.push&#40;"create table ES120913_COUNTRIES&#40;id int not null, country_name varchar&#40;100&#41;&#41;"&#41;;
   ddl.push&#40;"insert into ES120913_COUNTRIES &#40;id, country_name&#41; values &#40;1, 'Argentina'&#41;"&#41;;
   ddl.push&#40;"insert into ES120913_COUNTRIES &#40;id, country_name&#41; values &#40;2, 'Japan'&#41;"&#41;;
   ddl.push&#40;"insert into ES120913_COUNTRIES &#40;id, country_name&#41; values &#40;3, 'Iran'&#41;"&#41;;
   
   execute&#40;ddl&#41;;
&#125;

var tableName = "ES120913_CITIES";
if &#40;!dbExists&#40;tableName&#41;&#41; &#123;
	// table doesn't exist so push your DDL statements onto the array and build the     
   // database
   var ddl = &#91;&#93;;
   ddl.push&#40;"create table ES120913_CITIES&#40;id int not null, city_name varchar&#40;100&#41;, country_id int&#41;"&#41;;
   ddl.push&#40;"insert into ES120913_CITIES &#40;id, city_name, country_id&#41; values &#40;1, 'Buenos Aires', 1&#41;"&#41;;
   ddl.push&#40;"insert into ES120913_CITIES &#40;id, city_name, country_id&#41; values &#40;2, 'Mendoza', 1&#41;"&#41;;
   ddl.push&#40;"insert into ES120913_CITIES &#40;id, city_name, country_id&#41; values &#40;3, 'Rosario', 1&#41;"&#41;;

   ddl.push&#40;"insert into ES120913_CITIES &#40;id, city_name, country_id&#41; values &#40;4, 'Osaka', 2&#41;"&#41;;
   ddl.push&#40;"insert into ES120913_CITIES &#40;id, city_name, country_id&#41; values &#40;5, 'Kobe', 2&#41;"&#41;;
   ddl.push&#40;"insert into ES120913_CITIES &#40;id, city_name, country_id&#41; values &#40;6, 'Kyoto', 2&#41;"&#41;;
   ddl.push&#40;"insert into ES120913_CITIES &#40;id, city_name, country_id&#41; values &#40;7, 'Nagoya', 2&#41;"&#41;;

   ddl.push&#40;"insert into ES120913_CITIES &#40;id, city_name, country_id&#41; values &#40;8, 'Tehran', 3&#41;"&#41;;
   ddl.push&#40;"insert into ES120913_CITIES &#40;id, city_name, country_id&#41; values &#40;9, 'Yadz', 3&#41;"&#41;;
   ddl.push&#40;"insert into ES120913_CITIES &#40;id, city_name, country_id&#41; values &#40;10, 'Esfahan', 3&#41;"&#41;;
   ddl.push&#40;"insert into ES120913_CITIES &#40;id, city_name, country_id&#41; values &#40;11, 'Shiraz', 3&#41;"&#41;;

   execute&#40;ddl&#41;;
&#125;

// function to test if a database table exists, returns true if it does
//
function dbExists&#40;tableName&#41; &#123;
   var con, rs, stmt, metaData;
   try &#123;
      con = system.getDatabaseConnection&#40;samplesDB&#41;;
      metaData = con.getMetaData&#40;&#41;;
      rs = metaData.getTables&#40;null, null, tableName.toUpperCase&#40;&#41;, null&#41;;
      if &#40;rs.next&#40;&#41;&#41; &#123;return true;&#125;
      else &#123;return false;&#125;
   &#125;
   catch &#40;error&#41; &#123;
   	  log&#40;error&#41;;
   &#125;
   finally &#123;
	    if &#40;rs&#41; rs.close;
      if &#40;stmt&#41; stmt.close;
		  if &#40;con&#41; con.close;
   &#125;	
&#125;

// function to execute an array of DDL statements
//
function execute&#40;ddl&#41; &#123;
   var con, stmt;
   try &#123;
     con = system.getDatabaseConnection&#40;samplesDB&#41;;
	  stmt = con.createStatement&#40;&#41;;
     ddl.forEach&#40;function&#40;s&#41; &#123; 
         try &#123;
	        stmt.execute&#40;s&#41;;
	        system.transactionManager.commitAndRestartTransaction&#40;&#41;;
         &#125;
         catch &#40;error&#41; &#123;
            log &#40;error&#41;;
         &#125;
      &#125;&#41;;
   &#125;
   catch &#40;error&#41; &#123;
   	  log&#40;error&#41;;
   &#125;
   finally &#123;
	    if &#40;stmt&#41; stmt.close&#40;&#41;;
        if &#40;con&#41; con.close&#40;&#41;;
   &#125;
&#125;
which is how the samples set up local database tables.

Dave
0 x


Who is online

Users browsing this forum: No registered users and 20 guests