RE: JDBC and insert in VJ#
From: Mads Pedersen (Pedersen_at_discussions.microsoft.com)
Date: 08/06/04
- Previous message: Sunder Balachander via .NET 247: "JDBC and insert in VJ#"
- In reply to: Sunder Balachander via .NET 247: "JDBC and insert in VJ#"
- Next in thread: Lars-Inge Tønnessen [VJ# MVP]: "Re: JDBC and insert in VJ#"
- Messages sorted by: [ date ] [ thread ]
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();
}
- Previous message: Sunder Balachander via .NET 247: "JDBC and insert in VJ#"
- In reply to: Sunder Balachander via .NET 247: "JDBC and insert in VJ#"
- Next in thread: Lars-Inge Tønnessen [VJ# MVP]: "Re: JDBC and insert in VJ#"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|