JDBC

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

 

 

 

 

内容概要:文章基于4A架构(业务架构、应用架构、数据架构、技术架构),对SAP的成本中心和利润中心进行了详细对比分析。业务架构上,成本中心是成本控制的责任单元,负责成本归集与控制,而利润中心是利润创造的独立实体,负责收入、成本和利润的核算。应用架构方面,两者都依托于SAP的CO模块,但功能有所区分,如成本中心侧重于成本要素归集和预算管理,利润中心则关注内部交易核算和获利能力分析。数据架构中,成本中心与利润中心存在多对一的关系,交易数据通过成本归集、分摊和利润计算流程联动。技术架构依赖SAP S/4HANA的内存计算和ABAP技术,支持实时核算与跨系统集成。总结来看,成本中心和利润中心在4A架构下相互关联,共同为企业提供精细化管理和决策支持。 适合人群:从事企业财务管理、成本控制或利润核算的专业人员,以及对SAP系统有一定了解的企业信息化管理人员。 使用场景及目标:①帮助企业理解成本中心和利润中心在4A架构下的运作机制;②指导企业在实施SAP系统时合理配置成本中心和利润中心,优化业务流程;③提升企业对成本和利润的精细化管理水平,支持业务决策。 其他说明:文章不仅阐述了理论概念,还提供了具体的应用场景和技术实现方式,有助于读者全面理解并应用于实际工作中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值