02-access JDBC (add, delete, modify, and query)

Source: Internet
Author: User
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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.