JDBC
1.JDBC
JDBC的基本概念
java程序调用数据库的api
JDBC为java开发者使用数据库提供了统一的编程接口,它由一组java类和接口组成。是java程序与数据库系统通信的标准API。JDBC API使得开发人员可以使用纯java的方式来链接数据库,并执行操作。
JDBC的执行过程
1.加载驱动
2.建立连接
3.发送sql语句
4.返回数据库的响应结果
JDBC的任务
处理JAVA程序和数据库之间的交互
1.建立链接
2.发送sql
3.响应结果
JDBC常用接口
1.Driver接口
Driver接口由数据库厂家提供,对于Java开发者而言,只需要使用Driver接口就可以了。
在编程中要连接数据库,不许现状在特定厂商的数据库驱动程序。不同的数据库有不同的装在方法。
装载Mysql驱动
Class.forName(“com.mysql.jdbc.Driver”)
装载Oracle驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
2.Driver接口注释
• /** • * Attempts to make a database connection to the given URL. • * The driver should return "null" if it realizes it is the wrong kind • * of driver to connect to the given URL. This will be common, as when • * the JDBC driver manager is asked to connect to a given URL it passes • * the URL to each loaded driver in turn. • * • * <P>The driver should throw an <code>SQLException</code> if it is the right • * driver to connect to the given URL but has trouble connecting to • * the database. • * • * <P>The <code>java.util.Properties</code> argument can be used to pass • * arbitrary string tag/value pairs as connection arguments. • * Normally at least "user" and "password" properties should be • * included in the <code>Properties</code> object. • * • * @param url the URL of the database to which to connect • * @param info a list of arbitrary string tag/value pairs as • * connection arguments. Normally at least a "user" and • * "password" property should be included. • * @return a <code>Connection</code> object that represents a • * connection to the URL • * @exception SQLException if a database access error occurs • */
3.DriverManager接口
DriverManager接口是处理驱动和程序之间的桥梁
DriverManager是JDBC的管理层,作用于用户和驱动程序之间。
DriverManager跟踪可用的驱动程序,并在数据库和相应的驱动程序之间建立连接。
4.DriverManager工具类源码
• /** • * Load the initial JDBC drivers by checking the System property • * jdbc.properties and then use the {@code ServiceLoader} mechanism • */ • static { • loadInitialDrivers(); • println("JDBC DriverManager initialized"); • } • /** • * Attempts to establish a connection to the given database URL. • * The <code>DriverManager</code> attempts to select an appropriate driver from • * the set of registered JDBC drivers. • * @param url a database url of the form • * <code>jdbc:<em>subprotocol</em>:<em>subname</em></code> • * @param user the database user on whose behalf the connection is being • * made • * @param password the user's password • * @return a connection to the URL • * @exception SQLException if a database access error occurs • */ • @CallerSensitive • public static Connection getConnection(String url, • String user, String password) throws SQLException { • java.util.Properties info = new java.util.Properties(); • if (user != null) { • info.put("user", user); • } • if (password != null) { • info.put("password", password); • } • return (getConnection(url, info, Reflection.getCallerClass())); • }
5.Connection接口
Connection与特定数据库的连接(会话),在连接上下文中执行SQL语句并返回结果。
DriverManager的getConnection()方法建立在JDBC URL中定义的数据库连接上
连接MYSQL数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://host:port/database","user","password")
连接ORACLE数据库:
DriverManager conn = DriverManager.getConnection("jdbc:oracle:thin:@host.port:Database","user","password");
6.Connection源码:
PreparedStatement prepareStatement(String sql) throws SQLException;
Statement createStatement() throws SQLException;
在日常生活中,用的较多的是PreparedStatement预处理对象,相比statement对象功能更强大。PreparedStatement可以延迟加载,批处理
7.Statement接口
用于执行静态SQL语句并返回它所生成的结果。
三种Statement类:
Statement:
由createStatement创建,用于发送简单的SQL语句(不带参数的)
PreparedStatement:
继承自Statement类,由prepareStatement创建,用于发送含有一个或多个输入参数的sql语句。PreparedStatement对象比Statement对象的效率不高。我们一般都用PreparedStatement
CallableStatement:
继承自PreparedStatement.由方法prePareCall创建,用于调用存储过程。
常用的Statement方法:
excute():运行语句,返回是否有结果集
excuteQuery():运行select语句,返回ResultSet结果集
excuteUpdate():运行insert/update/delete操作,返回更新的行数。
8.Statement源码
• ResultSet executeQuery(String sql) throws SQLException;
• int executeUpdate(String sql) throws SQLException;
• void close() throws SQLException;
boolean execute(String sql) throws SQLException;
9.PreparedStatement接口
PreparedStatement:灵活指定SQL语句中的变量。PreparedStatement是一种预编译、SQL表达式形式,效率教高。一般推荐使用,并且应用非常灵活。
源码: • public interface PreparedStatement extends Statement 并且定义了一些自己特有的方法
10. ResultSet接口
Statement执行查询SQL语句时返回ResultSet结果集。
ResultSet提供的检索不同类型字段的方法,常用的有:
getString():获得在数据库里是varchar、char等类型数据的对象
getFloat() :获得在数据库里是Float类型的对象
getDate():获得在数据库里面是Date类型的数据。
getBoolean():获得在数据库里面是Boolean类型的数据
11.ResultSet源码:
• boolean next() throws SQLException;
• void close() throws SQLException;
• String getString(int columnIndex) throws SQLException;
• String getString(String columnLabel) throws SQLException;
boolean getBoolean(int columnIndex) throws SQLException;
Next方法是判断是否有下一个数据,返回类型是boolean,通常与while或for循环并用
Close方法释放resultSet资源
getString(int columnIndex)是通过字段的位置来获取字符串的属性。
String getString(String columnLable)是通过字段名来获取字段大的属性,返回结果是string
boolean getBoolean(int columnIndex)是通过字段位置来获取boolean类型的结果。
12JDBC的编程步骤
1.注册并载入特定的Database Driver
Class.forName("com,mysql.jdbc.Driver");
2.利用Driver Manager建立DB Connection
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db214","user","password");
3.利用Connection对象执行SQL语句并返回它所生成结果的对象
Statement stmt = conn.createStatement();
4.利用Statement执行SQL Statement
必要时取得ResultSet reference
ResultSets rs = stmt.excuteQuery("Select * from Book");
5.利用ResultSet读取结果集:
rs.getxxx("fieldName")
6.依序关闭使用之对象及连接;
ResultSet Statement Connection
Statement执行的sql语句主要分为两种,1.查找(executequery),2.更新(executeUpdate)包括增删改,insert,update,
delete.
ResultSet是查询的结果,所以只能有executry获取,当获取到多条的时候,要用到循环来获取,判断是否存在下一条用next方法.
释放资源:原则是先开后关,开启的顺序是:connection > statement > resultset
关闭的顺序: result > statement > connection
JDBC
最新推荐文章于 2022-04-15 09:59:15 发布