|
Re: Query just written MS ACCESS
|
Posted: Jul 6, 2004 3:25 PM
|
|
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();
}
}
}
}
|
|