找回密码
 立即注册
JDBCdatabase数据库The | 企业管理 2022-09-23 72 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
JDBC:JavaDataBaseConnection
本季方案
1、JDBC的分类
2、JDBC的首要操作类和接口:
DriverManager、Connection、Statement、PreparedStatement、ResultSet
3、如何使用JDBC连接MySQL数据库
mldn上有MYSQL的教程
1、啥是JDBC?
JDBC是JAVA提供的一个服务,专门用于访问数据库操作,并且注意:JDBC定义的是一个数据库访问的规范,所以里边基本上都是一系列的接口。各个数据库厂商假如要想支持JDBC操作,则必定要提供一组相关的类,这些类必定都是提供接口子类实现。
2、JDBC的分类
·JDBC-ODBC桥连接
·JDBC直接连接
·JDBC网络连接
3、啥是ODBC?
ODBC是微软企业定义的一个开放式数据库连接(OpenDataBaseConnection),使用ODBC可以连接各种数据库(条件:必须在windows中安装好驱动才可以)。在SUN的JDK中默认提供了此数据库的驱动程序,所以JDK自身可以直接使用JDBC-ODBC进行访问。
访问的进程:
JDBC--gt;ODBC--gt;数据库(功能比较低)
假如直接使用JDBC访问,访问进程:JDBC--gt;数据库(功能较高)
4、首要操作接口
·Connection:表明连接数据库的接口
·Statement:表明操作数据库的接口(履行SQL句子)
-PreparedStatement:操作SQL句子,可是可以进行预管理操作。
·ResultSet:表明查询今后的回来成果
5、使用JDBC-ODBC操作Access数据库
新建一个access空数据库
redking.blog.51cto/attachment/200903/9/27212_1236606199LCjf.png
建立好了一个数据库,数据库的名字为:mydb.mdb
redking.blog.51cto/attachment/200903/9/27212_1236606228qVRk.png
在数据库中创建表:
建立一张person表,包括以下字段:
·id--gt;自动增加
·name--gt;名字
·age--gt;年纪
redking.blog.51cto/attachment/200903/9/27212_1236606243CsS7.png
由于我们的意图是使用JDBC-ODBC桥连接操作数据库,所以此处必须先配置ODBC数据源:管理工具--gt;数据源(ODBC)
redking.blog.51cto/attachment/200903/9/27212_1236606261A25n.png
配置一个大局的数据源称号
redking.blog.51cto/attachment/200903/9/27212_1236606274kX8E.png
在ODBC上设置了一个数据库的别号,此别号testDB代表mydb.mdb。今后在操作的时候就是使用testDB即可。redking.blog.51cto/attachment/200903/9/27212_1236606288BlfY.png
假如要设置access数据源称号,则必须先关闭打开的access文件今后再设置
redking.blog.51cto/attachment/200903/9/27212_1236606300JGuK.png
保留并关闭access数据库
redking.blog.51cto/attachment/200903/9/27212_12366063186m5j.png
redking.blog.51cto/attachment/200903/9/27212_1236606335ol5K.png
那么今后直接使用testDB就可以访问数据库了。
1、操作数据库的过程(JDBC)
·加载数据库的驱动程序:Class.forName(驱动程序);
·通过连接管理器,获得一个数据库的连接<imgsrc="static/image/smiley/default/biggrin.gif"smilieid="3"border="0"alt=""/>riverManager.getConnection(连接地址)
·通过连接创建数据库的操作目标:Statement
·操作数据库--gt;履行SQL句子
·关闭数据库操作
我们如今使用Eclipse新建一个JDBCProject项目来测试下哈~
redking.blog.51cto/attachment/200903/9/27212_1236606360a9GP.png
在JDBC中一切的操作过程都是一样,可是驱动程序和连接地址是不一样的,ODBC的:
·驱动程序:sun.jdbc.odbc.JdbcOdbcDriver
·连接地址:jdbc<imgsrc="static/image/smiley/default/shocked.gif"smilieid="6"border="0"alt=""/>dbc:ODBC配置的DSN称号(jdbc<imgsrc="static/image/smiley/default/shocked.gif"smilieid="6"border="0"alt=""/>dbc:testDB)
JDBC中的一切操作的包都是java.sql包中
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){
//数据库连接目标
Connectionconn=null;
//数据库操作目标
Statementstmt=null;
//1、加载驱动程序
try{
Class.forName(DBDRIVER);
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
//2、连接数据库
//通过连接管理器连接数据库
try{
conn=DriverManager.getConnection(DBURL);
}catch(SQLExceptione){
e.printStackTrace();
}
//3、向数据库中刺进一条数据
Stringsql=INSERTINTOperson(name,age)VALUES('Michael',20);
try{
stmt=conn.createStatement();
}catch(SQLExceptione){
e.printStackTrace();
}
//4、履行句子
try{
stmt.executeUpdate(sql);
}catch(SQLExceptione){
e.printStackTrace();
}
//5、关闭操作,过程相反哈~
try{
stmt.close();
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
验证一下,有没问题,没有报错,正常
redking.blog.51cto/attachment/200903/9/27212_1236606367eb8n.png
如今看下access中数据有没注入进出哈~
redking.blog.51cto/attachment/200903/9/27212_1236606386YWmg.png
如今所刺进的数据都是固定死的,那么能不能刺进一些灵敏的数据呢?例如:部分数据可以直接从键盘上输入。
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;
[零基础学JAVA]JavaSE使用部分-33.Java数据库编程.doc

(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)

[下载]10490418152.rar




上一篇:MySql-5.0.22数据库程序
下一篇:Eclipse中文教程(PDF版)