Archive for 4月, 2013

  1. Hello, JDBC Type4(Oxygene) World!

    Posted on 4月 18th, 2013 by cx20

    JDBC Type4(Oxygene)

    JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
    Type4 は DBMS のクライアントライブラリを使用しない Pure Java の DBMS ドライバである。
    以下は Oxygene による JDBC ライブラリの使用例となっている。

    ソースコード(Oxygene + JDBC Type4 + SQL Server)

    namespace hello;
     
    interface
    uses
        java.sql.*;
     
    type
        Hello = class
    public
        class method Main(args: array of String);
    end;
     
    implementation
     
    class method Hello.Main(args: array of String);
    var
        conn: Connection;
        stmt: Statement;
        rs: ResultSet;
    begin
        java.lang.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
     
        conn := DriverManager.getConnection("jdbc:sqlserver://;server=localhost", "sa", "P@ssW0rd");
        stmt := conn.createStatement();
        rs := stmt.executeQuery("SELECT 'Hello, JDBC Type4 World!' AS Message");
        while rs.next() do begin
            System.out.println( rs.getString(1) );
        end;
        rs.close();
        stmt.Close();
        conn.Close();
    end;
     
    end.

    コンパイル&実行方法(Oxygene + JDBC Type4 + SQL Server)

    C:¥> oxygene Hello.pas -mode:Java
    C:¥> java -Xbootclasspath/a:sqljdbc4.jar -jar hello.jar

    ソースコード(Oxygene + JDBC Type4 + Oracle)

    namespace hello;
     
    interface
    uses
        java.sql.*;
     
    type
        Hello = class
    public
        class method Main(args: array of String);
    end;
     
    implementation
     
    class method Hello.Main(args: array of String);
    var
        conn: Connection;
        stmt: Statement;
        rs: ResultSet;
    begin
        java.lang.Class.forName("oracle.jdbc.driver.OracleDriver");
     
        conn := DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
        stmt := conn.createStatement();
        rs := stmt.executeQuery("SELECT 'Hello, JDBC Type4 World!' AS Message FROM DUAL");
        while rs.next() do begin
            System.out.println( rs.getString(1) );
        end;
        rs.close();
        stmt.Close();
        conn.Close();
    end;
     
    end.

    コンパイル&実行方法(Oxygene + JDBC Type4 + Oracle)

    C:¥> oxygene Hello.pas -mode:Java
    C:¥> java -Xbootclasspath/a:ojdbc6.jar -jar hello.jar

    ソースコード(Oxygene + JDBC Type4 + MySQL)

    namespace hello;
     
    interface
    uses
        java.sql.*;
     
    type
        Hello = class
    public
        class method Main(args: array of String);
    end;
     
    implementation
     
    class method Hello.Main(args: array of String);
    var
        conn: Connection;
        stmt: Statement;
        rs: ResultSet;
    begin
        java.lang.Class.forName("com.mysql.jdbc.Driver");
     
        conn := DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "P@ssW0rd");
        stmt := conn.createStatement();
        rs := stmt.executeQuery("SELECT 'Hello, JDBC Type4 World!' AS Message");
        while rs.next() do begin
            System.out.println( rs.getString(1) );
        end;
        rs.close();
        stmt.Close();
        conn.Close();
    end;
     
    end.

    コンパイル&実行方法(Oxygene + JDBC Type4 + MySQL)

    C:¥> oxygene Hello.pas -mode:Java
    C:¥> java -Xbootclasspath/a:mysql-connector-java-5.1.22-bin.jar -jar hello.jar

    実行結果

    Hello, JDBC Type4 World!
  2. Hello, JDBC Type2(Oxygene) World!

    Posted on 4月 17th, 2013 by cx20

    JDBC Type2

    JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
    Type2 は JDBC と DBMS クライアントの API をマップさせたブリッジドライバである。
    例えば、Oracle であればクライアントライブラリとして OCI が使用される。
    以下は Oxygene による JDBC ライブラリの使用例となっている。

    ソースコード(Oxygene + JDBC Type2 + Oracle)

    namespace hello;
     
    interface
    uses
        java.sql.*;
     
    type
        Hello = class
    public
        class method Main(args: array of String);
    end;
     
    implementation
     
    class method Hello.Main(args: array of String);
    var
        conn: Connection;
        stmt: Statement;
        rs: ResultSet;
    begin
        java.lang.Class.forName("oracle.jdbc.driver.OracleDriver");
     
        conn := DriverManager.getConnection("jdbc:oracle:oci:@orcl", "scott", "tiger");
        stmt := conn.createStatement();
        rs := stmt.executeQuery("SELECT 'Hello, JDBC Type2 World!' AS Message FROM DUAL");
        while rs.next() do begin
            System.out.println( rs.getString(1) );
        end;
        rs.close();
        stmt.Close();
        conn.Close();
    end;
     
    end.

    コンパイル&実行方法

    C:¥> oxygene Hello.pas -mode:Java
    C:¥> java -Xbootclasspath/a:ojdbc6.jar -jar hello.jar

    実行結果

    Hello, JDBC Type2 World!
  3. Hello, JDBC Type1(Oxygene) World!

    Posted on 4月 16th, 2013 by cx20

    JDBC Type1

    JDBC(Java Database Connectivity)は、Java 用のデータベース接続 API である。実装方法によりType1~4の4つのタイプが存在する。
    Type1 は JDBC と ODBC の API をマップさせたブリッジドライバである。
    以下は Oxygene による JDBC ライブラリの使用例となっている。

    ソースコード(Oxygene + JDBC Type1 + ODBC + Jet データベース)

    namespace hello;
     
    interface
    uses
        java.sql.*,
        sun.jdbc.odbc.*;
     
    type
        Hello = class
    public
        class method Main(args: array of String);
    end;
     
    implementation
     
    class method Hello.Main(args: array of String);
    var
        conn: Connection;
        stmt: Statement;
        rs: ResultSet;
    begin
        java.lang.Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     
        conn := DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=.\\hello.mdb", "", "");
        stmt := conn.createStatement();
        rs := stmt.executeQuery("SELECT 'Hello, JDBC Type1 World!' AS Message");
        while rs.next() do begin
            System.out.println( rs.getString(1) );
        end;
        rs.close();
        stmt.Close();
        conn.Close();
    end;
     
    end.

    ソースコード(Oxygene + JDBC Type1 + ODBC + ACE データベース)

    namespace hello;
     
    interface
    uses
        java.sql.*,
        sun.jdbc.odbc.*;
     
    type
        Hello = class
    public
        class method Main(args: array of String);
    end;
     
    implementation
     
    class method Hello.Main(args: array of String);
    var
        conn: Connection;
        stmt: Statement;
        rs: ResultSet;
    begin
        java.lang.Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     
        conn := DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=.\\hello.accdb", "", "");
        stmt := conn.createStatement();
        rs := stmt.executeQuery("SELECT 'Hello, JDBC Type1 World!' AS Message");
        while rs.next() do begin
            System.out.println( rs.getString(1) );
        end;
        rs.close();
        stmt.Close();
        conn.Close();
    end;
     
    end.

    ソースコード(Oxygene + JDBC Type1 + ODBC + SQL Server)

    namespace hello;
     
    interface
    uses
        java.sql.*,
        sun.jdbc.odbc.*;
     
    type
        Hello = class
    public
        class method Main(args: array of String);
    end;
     
    implementation
     
    class method Hello.Main(args: array of String);
    var
        conn: Connection;
        stmt: Statement;
        rs: ResultSet;
    begin
        java.lang.Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     
        conn := DriverManager.getConnection("jdbc:odbc:Driver={SQL Server};SERVER=(local)", "sa", "P@ssW0rd");
        stmt := conn.createStatement();
        rs := stmt.executeQuery("SELECT 'Hello, JDBC Type1 World!' AS Message");
        while rs.next() do begin
            System.out.println( rs.getString(1) );
        end;
        rs.close();
        stmt.Close();
        conn.Close();
    end;
     
    end.

    ソースコード(Oxygene + JDBC Type1 + ODBC + Oracle)

    namespace hello;
     
    interface
    uses
        java.sql.*,
        sun.jdbc.odbc.*;
     
    type
        Hello = class
    public
        class method Main(args: array of String);
    end;
     
    implementation
     
    class method Hello.Main(args: array of String);
    var
        conn: Connection;
        stmt: Statement;
        rs: ResultSet;
    begin
        java.lang.Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     
        conn := DriverManager.getConnection("jdbc:odbc:Driver={Oracle in OraDb11g_home1};DBQ=ORCL", "scott", "tiger");
        stmt := conn.createStatement();
        rs := stmt.executeQuery("SELECT 'Hello, JDBC Type1 World!' AS Message FROM DUAL");
        while rs.next() do begin
            System.out.println( rs.getString(1) );
        end;
        rs.close();
        stmt.Close();
        conn.Close();
    end;
     
    end.

    ソースコード(Oxygene + JDBC Type1 + ODBC + MySQL)

    namespace hello;
     
    interface
    uses
        java.sql.*,
        sun.jdbc.odbc.*;
     
    type
        Hello = class
    public
        class method Main(args: array of String);
    end;
     
    implementation
     
    class method Hello.Main(args: array of String);
    var
        conn: Connection;
        stmt: Statement;
        rs: ResultSet;
    begin
        java.lang.Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     
        conn := DriverManager.getConnection("jdbc:odbc:Driver={MySQL ODBC 5.1 Driver};Server=localhost", "root", "P@ssW0rd");
        stmt := conn.createStatement();
        rs := stmt.executeQuery("SELECT 'Hello, JDBC Type1 World!' AS Message");
        while rs.next() do begin
            System.out.println( rs.getString(1) );
        end;
        rs.close();
        stmt.Close();
        conn.Close();
    end;
     
    end.

    実行方法

    C:¥> oxygene Hello.pas -mode:Java
    C:¥> java -jar hello.jar

    実行結果

    Hello, JDBC Type1 World!
  4. Hello, Connector/NET(ClojureCLR) World!

    Posted on 4月 15th, 2013 by cx20

    Connector/NET(ClojureCLR)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は ClojureCLR による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(ClojureCLR + Connector/NET + MySQL)

    (System.Reflection.Assembly/LoadWithPartialName "System.Data")
    (System.Reflection.Assembly/LoadWithPartialName "MySql.Data")
     
    (import '(MySql.Data.MySqlClient MySqlConnection))
    (import '(MySql.Data.MySqlClient MySqlCommand))
    (import '(MySql.Data.MySqlClient MySqlDataReader))
     
    (def conStr "server=localhost;user id=root;password=P@ssW0rd")
    (def sqlStr "SELECT 'Hello, Connector/NET World!' AS Message")
    (def con (MySqlConnection. conStr))
    (def cmd (MySqlCommand. sqlStr con))
    (.Open con)
    (def reader (.ExecuteReader cmd))
    (if (.Read reader)
        (do
            (System.Console/WriteLine (.GetName reader 0))
            (System.Console/WriteLine "---------------------")
            (System.Console/WriteLine (.GetValue reader 0))
        )
    )
    (.Close reader)
    (.Close con)

    コンパイル方法

    C:¥> Clojure.Main hello.clj

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  5. Hello, Connector/NET(IronScheme) World!

    Posted on 4月 14th, 2013 by cx20

    Connector/NET(IronScheme)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は IronScheme による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(IronRuby + Connector/NET + MySQL)

    (import
        (rnrs)
        (ironscheme clr)
    )
     
    (clr-reference System)
    (clr-reference System.Data)
    (clr-reference MySql.Data)
     
    (clr-using System)
    (clr-using MySql.Data.MySqlClient)
     
    (begin
        (define conStr "server=localhost;user id=root;password=P@ssW0rd")
        (define sqlStr "SELECT 'Hello, Connector/NET World!' AS Message")
        (define con (clr-new MySqlConnection conStr))
        (define cmd (clr-new MySqlCommand sqlStr con))
        (clr-call MySqlConnection Open con)
        (define reader (clr-call MySqlCommand ExecuteReader cmd))
        (if (clr-call MySqlDataReader Read reader)
            (begin
                (clr-static-call System.Console WriteLine (clr-call MySqlDataReader GetName reader 0))
                (clr-static-call System.Console WriteLine "---------------------")
                (clr-static-call System.Console WriteLine (clr-call MySqlDataReader GetValue reader 0))
            )
        )
        (clr-call MySqlDataReader Close reader)
        (clr-call MySqlConnection Close con)
    )

    実行方法

    C:¥> isc Hello.ss

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  6. Hello, Connector/NET(Oxygene) World!

    Posted on 4月 13th, 2013 by cx20

    Connector/NET(Oxygene)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は Oxygene による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(Oxygene + Connector/NET + MySQL)

    namespace hello;
     
    interface
    uses
        System,
        MySql.Data.MySqlClient;
     
    type
        Hello = class
    public
        class method Main(args: array of String): Integer;
    end;
     
    implementation
     
    class method Hello.Main(args: array of String): Integer;
    var
        conStr: String;
        sqlStr: String;
        con: MySqlConnection;
        cmd: MySqlCommand;
        reader: MySqlDataReader;
    begin
        conStr := "server=localhost;user id=root;password=P@ssW0rd";
        sqlStr := "SELECT 'Hello, Connector/NET World!' AS Message";
        con := new MySqlConnection(conStr);
        cmd := new MySqlCommand(sqlStr, con);
        con.Open();
        reader := cmd.ExecuteReader();
        while reader.Read() do begin
            System.Console.WriteLine( reader.GetName(0) );
            System.Console.WriteLine( "---------------------" );
            System.Console.WriteLine( reader[0] );
        end;
        reader.Close();
        con.Close();
    end;
     
    end.

    コンパイル方法

    C:¥> oxygene Hello.pas ^
           -type:exe -cputype:x86 ^
           -ref:System.dll;System.Data.dll;MySql.Data.dll

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  7. Hello, Connector/NET(Nemerle) World!

    Posted on 4月 12th, 2013 by cx20

    Connector/NET(Nemerle)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は Nemerle による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(Nemerle + Connector/NET + MySQL)

    using System;
    using MySql.Data.MySqlClient;
     
    class Hello
    {
        public static Main() : void {
            def conStr = "server=localhost;user id=root;password=P@ssW0rd";
            def sqlStr = "SELECT 'Hello, Connector/NET World!' AS Message";
     
            def con = MySqlConnection(conStr);
            def cmd = MySqlCommand(sqlStr, con);
            con.Open();
            def reader = cmd.ExecuteReader();
            while( reader.Read() )
            {
                Console.WriteLine( reader.GetName(0) );
                Console.WriteLine( "---------------------" );
                Console.WriteLine( reader[0] );
            }
            reader.Close();
            con.Close();
        }
    }

    コンパイル方法

    C:¥> ncc -o Hello.exe -r:MySql.Data Hello.n

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  8. Hello, Connector/NET(Boo) World!

    Posted on 4月 11th, 2013 by cx20

    Connector/NET(Boo)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は Boo による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(Boo + Connector/NET + MySQL)

    import System
    import MySql.Data.MySqlClient
     
    [STAThread]
    def Main(argv as (string)):
        conStr = "server=localhost;user id=root;password=P@ssW0rd"
        sqlStr = "SELECT 'Hello, Connector/NET World!' AS Message"
     
        con = MySqlConnection(conStr)
        cmd = MySqlCommand(sqlStr, con)
        con.Open()
     
        reader = cmd.ExecuteReader()
        while reader.Read():
            Console.WriteLine( reader.GetName(0) )
            Console.WriteLine( "---------------------" )
            Console.WriteLine( reader.GetValue(0) )
     
        reader.Close()
        con.Close()

    コンパイル方法

    C:¥> booc Hello.boo

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  9. Hello, Connector/NET(Cobra) World!

    Posted on 4月 10th, 2013 by cx20

    Connector/NET(Cobra)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は Cobra による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(Cobra + Connector/NET + MySQL)

    use System
    use System.Data
    use MySql.Data.MySqlClient
     
    class Program
        def main is shared
            conStr = "server=localhost;user id=root;password=P@ssW0rd"
            sqlStr = "SELECT 'Hello, Connector/NET World!' AS Message"
     
            con = MySqlConnection(conStr)
            cmd = MySqlCommand(sqlStr, con)
            con.open()
     
            reader = cmd.executeReader()
            while reader.read()
                Console.writeLine( reader.getName(0) )
                Console.writeLine( "---------------------" )
                Console.writeLine( reader.getValue(0) )
     
            reader.close()
            con.close()

    コンパイル方法

    C:¥> cobra -compile Hello.cobra

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  10. Hello, Connector/NET(Phalanger) World!

    Posted on 4月 9th, 2013 by cx20

    Connector/NET(Phalanger)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は Phalanger による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(Phalanger + Connector/NET + MySQL)

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <phpNet>
            <classLibrary>
                <add assembly="mscorlib" />
                <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <add assembly="MySql.Data, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
            </classLibrary>
        </phpNet>
    </configuration>
    <?
    use System;
    use MySqlDataMySqlClient;
     
    class Hello
    {
        static function Main()
        {
            $conStr = "server=localhost;user id=root;password=P@ssW0rd";
            $sqlStr = "SELECT 'Hello, Connector/NET World!' AS Message";
     
            $con = new MySqlDataMySqlClientMySqlConnection($conStr);
            $cmd = new MySqlDataMySqlClientMySqlCommand($sqlStr, $con);
            $con->Open();
            $reader = $cmd->ExecuteReader();
            while ($reader->Read())
            {
                SystemConsole::WriteLine( $reader->GetName(0) );
                SystemConsole::WriteLine( "---------------------" );
                SystemConsole::WriteLine( $reader->GetValue(0) );
            }
            $reader->Close();
            $con->Close();
        }
    }
    ?>

    コンパイル方法

    C:¥> phpc /pure Hello.php

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!