RE: JDBC and insert in VJ#

From: Mads Pedersen (Pedersen_at_discussions.microsoft.com)
Date: 08/06/04

  • Next message: Lars-Inge Tønnessen [VJ# MVP]: "Re: JDBC and insert in VJ#"
    Date: Fri, 6 Aug 2004 10:37:01 -0700
    
    

    Hello.

    I have just experimented a little accessing an MS-Access database from J#.
    I used the DSN-less method. Here's my test code for inserting and reading
    data that worked fine for me:

    import System.Data.Odbc.*;
    ...
      public void testInsert() throws System.Exception
      {
        // Build a connection and SQL string
        String connectionString = "Driver={Microsoft Access Driver
    (*.mdb)};DBQ=D:/Data/temp.mdb";
        String SQL = "INSERT INTO tblTemperature (timeMs, temp) VALUES (" +
    System.currentTimeMillis() + ", " + Math.random() * 30 + ");";
        System.out.println("SQL: " + SQL);

        // Create connection object
        OdbcConnection conn = new OdbcConnection(connectionString);

        // Create command object and open it
        OdbcCommand cmd = new OdbcCommand(SQL);
        cmd.set_Connection(conn);
        conn.Open();

        // Execute command
        cmd.ExecuteNonQuery();

        // Close connection
        conn.Close();
      }

      public void testRead() throws System.Exception
      {
        // Build a connection and SQL string
        String connectionString = "Driver={Microsoft Access Driver
    (*.mdb)};DBQ=D:/Data/temp.mdb";
        String SQL = "SELECT * FROM tblTemperature";

        // Create connection object
        OdbcConnection conn = new OdbcConnection(connectionString);
     
        // Create command object and open it
        OdbcCommand cmd = new OdbcCommand(SQL);
        cmd.set_Connection(conn);
        conn.Open();

        // Execute command
        OdbcDataReader reader = cmd.ExecuteReader();

        // Read the reader and display results on the console
        while (reader.Read())
        {
          System.out.print("Time: " + reader.GetValue(1) );
          System.out.print(" , ");
          System.out.println("Temp:" + reader.GetDecimal(2).ToString() );
        }

        // Close reader and connection
        reader.Close();
        conn.Close();
      }


  • Next message: Lars-Inge Tønnessen [VJ# MVP]: "Re: JDBC and insert in VJ#"

    Relevant Pages

    • Re: integers and arrays in Java - how?
      ... If i try "int" in that math, the values are then zero for everything - even those where i do no calculation. ... public void mouseDragged{ ... The "do some calculation" bit is what is troubling; "int" does not work and kills reading of mouse XY coordinates. ...
      (comp.lang.java)
    • Re: integers and arrays in Java - how?
      ... If i try "int" in that math, the values are then zero for everything - even those where i do no calculation. ... public void mouseDragged{ ... The "do some calculation" bit is what is troubling; "int" does not work and kills reading of mouse XY coordinates. ...
      (comp.lang.java)
    • Re: integers and arrays in Java - how?
      ... If i try "int" in that math, the values are then zero for everything - even those where i do no calculation. ... public void mouseDragged{ ... The "do some calculation" bit is what is troubling; "int" does not work and kills reading of mouse XY coordinates. ...
      (comp.lang.java)
    • RE: Excel Workbook.SheetDeactivated events disappearing
      ... After the for loop,the event will keep firing,here is my test code for your ... reference. ... private Excel.Application exApp=null; ... public void OnConnection(object application, Extensibility.ext_ConnectMode ...
      (microsoft.public.office.developer.com.add_ins)

    Loading