To access the database through Java code and manipulate the database, you need the following steps:
First, we need a. jar package. Access to different databases, the required jar packages are not the same
MySQL Database jar package: http://wangpan.baidu.com/disk/home?errno=0&errmsg=Auth%20Login%20Sucess&stoken= 62559320e9b55b03d45ec682d851d5598036a2dc1f8d5d1cd0d330d955fa84deff136d062a78d7530dbe9a48aa7b596731977770eeab3a95a5187543a 562730ecb004cf734ed&bduss= 4e309e0e14127b1f753a6866fe23eae22a67ae3c938ded62a1069606c7d366ed3e60dad34f8c4ecad4b4ac400807677ca77c23c99605da54004c54a22 e7185cc5bb6d77f4261d74d026e83417d686789e962064eb1b94cc4368d7fcc919911d3c7428774027d2388a943364ab66c4d0c9ab3f229d65d3b9389 8c8604a7a118296f615171bf8a57eca49948ae150eceff66a908a68f9a063b7f4be689810a4e8d924b2528046eef8d908c64c15b5c65583d6310788d1 3fa300e9c7fefb9aaab8c540453689d4f&ssnerror=0#list/vmode=list&path=%2f%e6%95%b0%e6%8d%ae%e5%ba%93%20. Jar%20%20%e6%96%87%e4%bb%b6
Orecle Database jar Package: http://pan.baidu.com/disk/home#list/vmode=list&path=%2F%E6%95%B0%E6%8D%AE%E5%BA%93%20.jar%20 %20%e6%96%87%e4%bb%b6%2forecle%e6%95%b0%e6%8d%ae%e5%ba%93
What is a jar package?
A jar package is a class that someone else has already written, and then they are packaged, and you can bring the jar packages into your project, and then you can use the classes and properties and methods in those jar packages directly.
Second, the configuration program, let the Eclipse program to find the database drive jar Package
1. Copy the jar file into the project. (Easy copy)
2. Right-click Project--Build path--Configuration build path--Library--Add external jar--find jar package and click OK, then we will find a reference library, this is the jar package we quoted
III. Create a new database in the database, create a new table
Four, write code to call the class of the driver package, as well as the data additions and deletions
It takes three steps to change the data in the database by adding or deleting it:
1. Load Data Access Driver
Class.forName ("Com.mysql.jdbc.Driver"); // the contents of the quotation marks inside the parentheses are in the jar file, where the driver drive
2. Link to build Data
Connection conn=drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/mydb?characterencoding=gbk", "Root", "" ") ; // The contents of the quotation marks in parentheses: "JDBC (Master Protocol): MySQL (sub-protocol):////127.0.0.1 (IP address this address for itself): 3306 (port number)/mydb (database name)", " Database account "," Password "
3. Build SQL statements
Statement state=conn.createstatement (); // Build statement Container String sql= "INSERT into XS values ('" "+xh+" ', ' "+xm+" ', ' "+xx+" ') "; // write an SQL statement and assign it to a variable State.executeupdate (SQL); // uploading SQL statements to the database for Operation Conn.close (); // Finally, close the link
Example: Modifying a single piece of data in a database
ImportJava.sql.*; Public classTest1 { Public Static voidmain111 (string[] args)throwsException {//1. Load DriverClass.forName ("Com.mysql.jdbc.Driver"); //2. Building a database linkConnection conn=drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/mydb?characterencoding=gbk", "Root", "" "); //3. Building execution StatementsStatement State =conn.createstatement (); String s= "Update xs set xingming= ' Zhang San ' where xuehao=1101"; State.execute (s); Conn.close (); }}
Jdbc--java code-to-Database link bridge