(JDBC: JavaDataBaseConnection
Program this season
1. Classification of JDBC
2. The primary operation classes and interfaces of JDBC:
DriverManager, Connection, Statement, PreparedStatement, ResultSet
3. How to use JDBC to connect to MySQL database
There are MYSQL tutorials on mldn.
1. What is JDBC?
JDBC is a service provided by JAVA, which is specially used to access database operations, and note: JDBC defines a specification for database access, so it is basically a series of interfaces. If each database manufacturer wants to support JDBC operation, it must provide a set of related classes, and these classes must all provide interface subclass implementation.
2. Classification of JDBC
JDBC-ODBC bridge connection
JDBC direct connection
JDBC network connection
3. What is ODBC?
ODBC is an open database connection (OpenDataBaseConnection) defined by Microsoft Corporation, and various databases can be connected using ODBC (condition: the driver must be installed in Windows). The driver for this database is provided by default in SUN's JDK, so the JDK itself can be accessed directly using JDBC-ODBC.
Accessed process:
JDBC--gt;ODBC--gt;Database (lower functionality)
If you use JDBC access directly, access process: JDBC--gt; database (higher function)
4. The primary operation interface
Connection: indicates the interface to connect to the database
Statement: indicates the interface for operating the database (executes the SQL sentence)
-PreparedStatement: Operates SQL sentences, but can perform pre-management operations.
ResultSet: Indicates the future return results of the query
5. Use JDBC-ODBC to operate Access database
Create a new access database
redking.blog.51cto/attachment/200903/9/27212_1236606199LCjf.png
A database has been established, the name of the database is: mydb.mdb
redking.blog.51cto/attachment/200903/9/27212_1236606228qVRk.png
Create table in database:
Create a person table with the following fields:
id--gt; auto increment
·name--gt;name
·age--gt;
redking.blog.51cto/attachment/200903/9/27212_1236606243CsS7.png
Since our intention is to use the JDBC-ODBC bridge connection to operate the database, the ODBC data source must be configured first here: Administration Tools --gt; Data Sources (ODBC)
redking.blog.51cto/attachment/200903/9/27212_1236606261A25n.png
Configure an overall data source name
redking.blog.51cto/attachment/200903/9/27212_1236606274kX8E.png
A database alias is set on ODBC, and this alias testDB represents mydb.mdb. In the future, you can use testDB when operating. redking.blog.51cto/attachment/200903/9/27212_1236606288BlfY.png
If you want to set the access data source name, you must first close the open access file and set it later
redking.blog.51cto/attachment/200903/9/27212_1236606300JGuK.png
Keep and close the access database
redking.blog.51cto/attachment/200903/9/27212_12366063186m5j.png
redking.blog.51cto/attachment/200903/9/27212_1236606335ol5K.png
Then you can access the database directly by using testDB in the future.
1. The process of operating the database (JDBC)
·The driver that loads the database: Class.forName(driver);
Get a database connection through the connection manager <imgsrc="static/image/smiley/default/biggrin.gif"smilieid="3"border="0"alt=""/>riverManager.getConnection(connection address)
·The operation target of creating the database through the connection: Statement
·Operate the database --> Fulfill SQL sentences
·Close database operations
We now use Eclipse to create a new JDBCProject project to test it~
redking.blog.51cto/attachment/200903/9/27212_1236606360a9GP.png
All operations in JDBC are the same, but the driver and connection address are different, ODBC:
Driver: sun.jdbc.odbc.JdbcOdbcDriver
·Connection address: jdbc<imgsrc="static/image/smiley/default/shocked.gif"smilieid="6"border="0"alt=""/>dbc: the DSN name of the ODBC configuration (jdbc<imgsrc=" static/image/smiley/default/shocked.gif"smilieid="6"border="0"alt=""/>dbc:testDB)
The packages for all operations in JDBC are in the java.sql package
JDBCDemo01:
packagecom.redking.jdbc.demo;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
importjava.sql.Statement;
publicclassJDBCDemo01{
publicstaticfinalStringDBDRIVER=sun.jdbc.odbc.JdbcOdbcDriver;
publicstaticfinalStringDBURL=jdbc:odbc:testDB;
publicstaticvoidmain(Stringargs){
// database connection target
Connectionconn=null;
// database operation target
Statementstmt=null;
//1, load the driver
try{
Class.forName(DBDRIVER);
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
//2, connect to the database
//connect to the database through the connection manager
try{
conn=DriverManager.getConnection(DBURL);
}catch(SQLExceptione){
e.printStackTrace();
}
//3, insert a piece of data into the database
Stringsql=INSERTINTOperson(name,age)VALUES('Michael',20);
try{
stmt=conn.createStatement();
}catch(SQLExceptione){
e.printStackTrace();
}
//4, fulfill the sentence
try{
stmt.executeUpdate(sql);
}catch(SQLExceptione){
e.printStackTrace();
}
//5, close the operation, the process is the opposite~
try{
stmt.close();
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
Verify that there is no problem, no error, normal
redking.blog.51cto/attachment/200903/9/27212_1236606367eb8n.png
Now let's see if the data in the access is injected in and out~
redking.blog.51cto/attachment/200903/9/27212_1236606386YWmg.png
Now the data inserted are fixed, so can some sensitive data be inserted? For example: some data can be directly input from the keyboard.
JDBCDemo02:
packagecom.redking.jdbc.demo;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
importjava.sql.Statement;
publicclassJDBCDemo02{
publicstaticfinalStringDBDRIVER=sun.jdbc.odbc.JdbcOdbcDriver;
publicstaticfinalStringDBURL=jdbcbc:testDB;
publicstaticvoidmain(Stringargs){
Stringname=null;
[Zero Basic Learning JAVA] JavaSE Use Part-33.Java Database Programming.doc)