JavaXT

Database Class

The database class is used to encapsulate all of the information required to connect to a database. This class is often used in conjunction with the Connection Class. Once a connection to the database is opened, you can use this class to retrieve metadata (schema, primary keys, list of other databases, etc).

Instantiating the Class

Here's a simple example of how to instantiate the database object and set connection parameters.

  javaxt.sql.Database db = new javaxt.sql.Database();
  db.setDriver(javaxt.sql.Database.SQLServer);
  db.setHost("localhost");
  db.setUserName("username");
  db.setPassword("password");
  db.setName("Northwind");

Open Database Connection

The primary purpose of the Database class is to help connect to a database.

     javaxt.sql.Connection conn = new javaxt.sql.Connection();     
     conn.open(db);
     conn.close();

Alternatively, you can open a database connection directly from the database class.

     javaxt.sql.Connection conn = db.getConnection();
     conn.close();

Table Metadata

Once a connection to the database is established, you can retrieve schema information (tables, columns, keys, etc). Here's a simple example of how to print all tables and columns in a database.

//Open connection
  javaxt.sql.Connection conn = db.getConnection();

//Print tables and columns
  for (javaxt.sql.Table table : db.getTables()){
      System.out.println("\r\n" + table.getName());
      System.out.println("----------------------");
      for (javaxt.sql.Column column : table.getColumns()){
          System.out.println(column.getName() + " (" + column.getType() + ")");
      }
  }

//Close connection
  conn.close();

Drivers

One of the key connection properties required to connect to a database is the driver information. The database class is prepackaged with the following driver information that you can use in the setDriver() method.

Driver NameClassURL Protocol
SQLServercom.microsoft.sqlserver.jdbc.SQLServerDriverjdbc:sqlserver
DB2com.ibm.db2.jcc.DB2Driverjdbc:db2
Sybasecom.sybase.jdbc3.jdbc.SybDriverjdbc:sybase
PostgreSQLorg.postgresql.Driverjdbc:postgresql
Derbyorg.apache.derby.jdbc.EmbeddedDriverjdbc:derby
SQLiteorg.sqlite.JDBCjdbc:sqlite
Microsoft Accesssun.jdbc.odbc.JdbcOdbcDriverjdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}
FrontBasecom.frontbase.jdbc.FBJDriverjdbc:FrontBase
Informixcom.informix.jdbc.IfxDriverjdbc:informix-sqli
Cachecom.intersys.jdbc.CacheDriverjdbc:Cache
microsoftcom.microsoft.jdbc.sqlserver.SQLServerDriverjdbc:microsoft
Mimercom.mimer.jdbc.Driverjdbc:mimer
MySQLcom.mysql.jdbc.Driverjdbc:mysql
Teradatacom.ncr.teradata.TeraDriverjdbc:teradata
Pervasivecom.pervasive.jdbc.v2.Driverjdbc:pervasive
Pointbasecom.pointbase.jdbc.jdbcUniversalDriverjdbc:pointbase
pointbase microcom.pointbase.me.jdbc.jdbcDriverjdbc:pointbase:micro
Daffodilin.co.daffodil.db.jdbc.DaffodilDBDriverjdbc:daffodil
daffodilDBin.co.daffodil.db.rmi.RmiDaffodilDBDriverjdbc:daffodilDB
JTDSnet.sourceforge.jtds.jdbc.Driverjdbc:jtds
Oracleoracle.jdbc.driver.OracleDriverjdbc:oracle
derby netorg.apache.derby.jdbc.ClientDriverjdbc:derby:net
Firebirdorg.firebirdsql.jdbc.FBDriverjdbc:firebirdsql
H2org.h2.Driverjdbc:h2
HyperSQLorg.hsqldb.jdbcDriverjdbc:hsqldb
odbcsun.jdbc.odbc.JdbcOdbcDriverjdbc:odbc