The Artima Developer Community
Sponsored Link

Java Answers Forum
Query just written MS ACCESS

1 reply on 1 page. Most recent reply: Jul 6, 2004 3:25 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Nick

Posts: 9
Nickname: nicolashad
Registered: Jul, 2004

Query just written MS ACCESS Posted: Jul 6, 2004 1:48 AM
Reply to this message Reply
Advertisement
Hi,someone know if exist a command usable to launch from JSP a query just written in ACCESS.
Thanks


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Query just written MS ACCESS Posted: Jul 6, 2004 3:25 PM
Reply to this message Reply
Here is the code that updates a table in MS Access using Java program. You can use similar piece of code in your JSP page. The ODBC data Source name I am using is "test_access" and it is in dbURL value. Please replace this by your ODBC Datasource name.

In the following code table name is person. It has two columns named state and dob. It has some other columns, which we are not using in the code. The update statement will set the state column to 'N' for all the record for which dob is future date.

In MS Access, now() is the function that returns current date/time. You can see the use of now() in update statement in code. Let us know if you have any problem in executing update statement in your Access database.


package test;
 
import java.sql.*;
 
public class TestAccess {
  public static void main(String[] args) {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String dbURL = "jdbc:odbc:test_access";
    Connection con = null;
    Statement stmt = null;
    try {
      // Register the driver
      Class.forName(driver).newInstance();
 
      // Get the connection
      con = DriverManager.getConnection(dbURL, "", "");
 
      // Prepare the update statement
      String sql = "update person set state = 'N' where dob > now()";
 
      stmt = con.createStatement();
      stmt.execute(sql);
 
      int updatedCount = stmt.getUpdateCount();
      
      System.out.println("Total Records Updated:" + updatedCount);
 
      if ( updatedCount == -1 ) {
        con.rollback();
      }
      else {
        con.commit();
      }
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    finally {
      try {
        if (stmt != null) {
          stmt.close();
        }
        if (con != null) {
          con.close();
        }
      }
      catch (Exception e1) {
        e1.printStackTrace();
      }
 
    }
 
  }
}

Flat View: This topic has 1 reply on 1 page
Topic: Undo woes... Previous Topic   Next Topic Topic: Communication with modem

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use