Add
/*** Add */try {class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver "); connection con = drivermanager. getconnection ("JDBC: ODBC: mydatasource"); string SQL = "insert into student (ID, names) values (?,?) "; Preparedstatement prep = con. preparestatement (SQL); Prep. setobject (1, 2); Prep. setobject (2, "He xx"); int COUNT = prep.exe cuteupdate (); If (count> 0) {system. out. println ("added successfully ~ ");} Prep. Close (); con. Close ();} catch (exception e) {e. printstacktrace ();}
Delete
/*** Delete */try {class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver "); // obtain the connection con = drivermanager of the database. getconnection ("JDBC: ODBC: mydatasource"); // defines the variable int COUNT = 0; string SQL = "delete from student where id =? "; // Create the execution object preparedstatement prep = con. preparestatement (SQL); // sets the parameter value of the placeholder prep. setobject (1, 2); // execute the command to obtain the affected row value COUNT = prep.exe cuteupdate (); // close the resource prep. close (); con. close (); If (count> 0) {system. out. println ("deleted successfully! ") ;}} Catch (exception ex) {ex. printstacktrace ();}
Modify
/*** Modify */try {class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver "); connection con = drivermanager. getconnection ("JDBC: ODBC: mydatasource"); string SQL = "Update student set names =? Where id =? "; Preparedstatement prep = con. preparestatement (SQL); Prep. setobject (1, "Liu Dan"); Prep. setobject (2, 2); int COUNT = prep.exe cuteupdate (); If (count> 0) {system. out. println ("Update successful! ");} Prep. Close (); con. Close ();} catch (exception e) {e. printstacktrace ();}
Query all
/*** Query all */try {class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver "); connection con = drivermanager. getconnection ("JDBC: ODBC: mydatasource"); string SQL = "select * from student"; preparedstatement prep = con. preparestatement (SQL); resultset rs = prep.exe cutequery (); While (RS. next () {system. out. print (RS. getint (1) + ""); system. out. println (RS. getstring (2) ;}} catch (exception e) {e. printstacktrace ();}
Query by ID
/*** Query by ID */try {class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver "); connection con = drivermanager. getconnection ("JDBC: ODBC: mydatasource"); string SQL = "select * from student where id =? "; Preparedstatement prep = con. preparestatement (SQL); Prep. setint (1, 2); resultset rs = prep.exe cutequery (); While (RS. next () {system. out. println (RS. getstring (2) ;}} catch (exception e) {e. printstacktrace ();}
Code usage:
1. The above code can be directly run in the main method of the Java project.
2. You do not need to add a jar package. You need to create a data source in access (the method for creating a data source is found in the previous blog)
3. Create a DSN (formerly: Data Source Name, Chinese name: data source name)
4. The data source name in this article is (mydatasource)
5. For more information, see Delete.
Create a table in access ):
Differences between addition, deletion, modification, and query of access databases and Oracle and MySQL
1. Different drivers
2. Different connection URLs