JFinalo操作框架racle数据库
JFinal操作框架oracle数据库。在需求configPlugin()方法来配置链路oracle配置数据库
组态JFinal数据库操作窗口小部件,configPlugin方法
在这里,我打开jdbc.properties它分析configConstant载入的
@Override
public void configConstant(Constants me) {
loadPropertyFile("jdbc.properties");//载入配置文件
me.setDevMode(getPropertyToBoolean("config.devModel", false));
me.setViewType(ViewType.JSP);
me.setEncoding("UTF-8");
}
jdbc.properites配置文件
oracle.driver=oracle.jdbc.driver.OracleDriver
oracle.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
oracle.username=scott
oracle.password=xiaohu config.devModel=true
@Override
public void configPlugin(Plugins me) {
ActiveRecordPlugin arp=null;
String driver=getProperty("oracle.driver");
String url=getProperty("oracle.url");
String username=getProperty("oracle.username");
String password=getProperty("oracle.password");
DruidPlugin dp=new DruidPlugin(url, username, password, driver);
me.add(dp);
arp=new ActiveRecordPlugin(dp);//设置数据库方言
arp.setDialect(new OracleDialect());
arp.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大写和小写
me.add(new EhCachePlugin());
arp.addMapping("users", "id",Users.class);
me.add(arp);
}
须要注意一点的是,因为oracle数据库中在创建表时。会自己主动的将全部的字段自己主动转为大写。因此在避免后面操作的时候出现大写和小写错误的相关异常,这里须要配置忽略大写和小写的功能
arp.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大写和小写
假设不须要对数据库进行添加操作。则必须配置忽略大写和小写。假设不配置忽略大写和小写,在保存源码的该段代码中会出现属性id找不到的异常
/**
* Save model.
*/
public boolean save() {
Config config = getConfig();
Table table = getTable(); StringBuilder sql = new StringBuilder();
List<Object> paras = new ArrayList<Object>();
config.dialect.forModelSave(table, attrs, sql, paras);
// if (paras.size() == 0) return false; // The sql "insert into tableName() values()" works fine, so delete this line // --------
Connection conn = null;
PreparedStatement pst = null;
int result = 0;
try {
conn = config.getConnection();
if (config.dialect.isOracle())
pst = conn.prepareStatement(sql.toString(), new String[]{table.getPrimaryKey()});
else
pst = conn.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS); config.dialect.fillStatement(pst, paras);
result = pst.executeUpdate();
<span style="color:#ff0000;">getGeneratedKey(pst, table);//假设不配置忽略大写和小写。运行到这里会出现异常,尽管能够加入到数据库,可是这里报错。界面还是会显示500错误</span>
getModifyFlag().clear();
return result >= 1;
} catch (Exception e) {
throw new ActiveRecordException(e);
} finally {
config.close(pst, conn);
}
}
<span style="color:#ff0000;">getGeneratedKey()源码部分</span>
/**
* Get id after save method.
*/
private void getGeneratedKey(PreparedStatement pst, Table table) throws SQLException {
String pKey = table.getPrimaryKey();
if (get(pKey) == null || getConfig().dialect.isOracle()) {
ResultSet rs = pst.getGeneratedKeys();
if (rs.next()) {
Class colType = table.getColumnType(pKey);
if (colType == Integer.class || colType == int.class)
set(pKey, rs.getInt(1));
else if (colType == Long.class || colType == long.class)
set(pKey, rs.getLong(1));
else
set(pKey, rs.getObject(1)); // It returns Long object for int colType
rs.close();
}
}
}
set()源码部分
/**
* Set attribute to model.
* @param attr the attribute name of the model
* @param value the value of the attribute
* @return this model
* @throws ActiveRecordException if the attribute is not exists of the model
*/
public M set(String attr, Object value) {
<span style="color:#ff0000;">if (getTable().hasColumnLabel(attr)) {//运行到这里返回false</span>
attrs.put(attr, value);
getModifyFlag().add(attr); // Add modify flag, update() need this flag.
return (M)this;
}
throw new ActiveRecordException("The attribute name is not exists: " + attr);//抛出该异常
}
如今来说说假设不配置,为什么会出现 The attribute name is not exists:这个异常,这是由于oracle中的字段是大写的,而set方法中传入的attr属性的值是小写,而getTable()中的属性相应的就是oracle字段。这些属性则是大写。因此这里使用getTable().hasColumnLabel(attr)推断是否存在该字段,就会找不到,这时就会抛出该异常,因此就必须配置忽略大写和小写的方法,就不会出现该异常
实体类:
package com.tenghu.core.model;
import com.jfinal.plugin.activerecord.Model;
public class Users extends Model<Users>{
public static Users dao=new Users();
}
操作数据:
Users users=new Users();
users.set("id", "users_sequence.nextval");
users.set("username", "张三");
users.set("pwd", "sdfsdfs");
users.save();
List<Users> testList=Users.dao.find("select * from users");
在这里完成JFinal操作框架oracle数据库,删除和更改自己的测试
JFinalo操作框架racle数据库的更多相关文章
- 十三、EnterpriseFrameWork框架核心类库之数据库操作(多数据库事务处理)
本章介绍框架中封装的数据库操作的一些功能,在实现的过程中费了不少心思,针对不同数据库的操作(SQLServer.Oracle.DB2)这方面还是比较简单的,用工厂模式就能很好解决,反而是在多数据库同时 ...
- ThinkPhp框架的数据库操作(查询)
TP框架有一套自己的数据库操作的代码,包括数据库的增.删.改.查.本文主要讲解TP框架的数据库查询操作. 找到入口文件的控制器: 我这里的入口文件是Show文件夹下的控制器. 打开Login控制器. ...
- python操作三大主流数据库(12)python操作redis的api框架redis-py简单使用
python操作三大主流数据库(12)python操作redis的api框架redis-py简单使用 redispy安装安装及简单使用:https://github.com/andymccurdy/r ...
- Litepal 数据库操作框架的使用 (火)
LitePal是GitHub上一款开源的Android数据库框架. 它採用了对象关系映射(ORM)的模式,将平时开发时最经常使用的一些数据库功能进行了封装.使得开发人员不用编写一行SQL语句就能够完毕 ...
- ThinkPhp框架对“数据库”的基本操作
框架有时会用到数据库的内容,在"ThinkPhp框架知识"的那篇随笔中提到过,现在这篇随笔详细的描述下. 数据库的操作,无疑就是连接数据库,然后对数据库中的表进行各种查询,然后就是 ...
- TP框架对数据库的基本操作
数据库的操作,无疑就是连接数据库,然后对数据库中的表进行各种查询,然后就是对数据的增删改的操作,一步步的讲述一下框架对数据库的操作 想要操作数据库,第一步必然是要:链接数据库 一.链接数据库 (1)找 ...
- 基于abp框架的数据库种子数据初始化
目录 基于abp框架的数据库种子数据初始化 1.背景 2.参照 3.解决方案 3.1 初始化数据 3.2 依赖注入方法容器里获取数据库上下文 3.3 封装创建初始化数据列表方法 3.4 数据库中没有的 ...
- 国产化之 .NET Core 操作达梦数据库DM8的两种方式
背景 某个项目需要实现基础软件全部国产化,其中操作系统指定银河麒麟,数据库使用达梦V8,CPU平台的范围包括x64.龙芯.飞腾.鲲鹏等.考虑到这些基础产品对.NET的支持,最终选择了.NET Core ...
- PHP框架_ThinkPHP数据库
目录 1.ThinkPHP数据库配置 2.ThinkPHP数据库实例化模型 3.ThinkPHP数据库CURD操作 4.ThinkPHP数据库连贯操作 1.ThinkPHP数据库配置 App/Conf ...
随机推荐
- 李洪强实现横向滚动的View<一>
今天做一个小的view的效果(纯代码),虽然这个view做起来 并不是很难,但是他是为后面我要实现的功能做一个铺垫. 01 创建CFTyreView,继承自UIView 02 来到.m文件. 2.1 ...
- freshStartTail 第一次启动时 抛弃旧的日志
freshStartTail [on/off] (requires v8.18.0+) Default: off This is used to tell rsyslog to seek to the ...
- oracle删除列
ALTER TABLE 表名 DROP COLUMN 列名;
- 关于“javax.servlet.include.request_uri”属性值
在springMVC的DispatcherServlet类的doService方法中有如下代码: if (WebUtils.isIncludeRequest(request)) { attribute ...
- 调试MSBuild脚本
http://blogs.msdn.com/b/visualstudio/archive/2010/07/06/debugging-msbuild-script-with-visual-studio. ...
- (转载)Excel文档保存的时候,提示“文档未保存”
亲测,成功搞定 Excel文档保存的时候,提示“文档未保存”? 先打开你需要处理的excel,然后打开工具栏--宏--录制新宏--确定--停止录制宏--宏-宏--编辑--复制以下程序Sub 恢复保存( ...
- shell常用命令总结
统计文件行数 wc -l filename grep -c "" filename sed -n '$=' filename awk 'END{print NR}' filenam ...
- UltraISO PE(软碟通) v9.6.2.3059 注册码
注册码: 王涛7C81-1689-4046-626F
- Storm系列(七)架构分析之Scheduler-调度器[DefaultScheduler]
Storm默认的任务调度器.实现如下: 1 (defn –prepare [this conf]) 2 (defn –schedule [this ^Topologies topologies ^ ...
- EM 算法
这个暂时还不太明白,先写一点明白的. EM:最大期望算法,属于基于模型的聚类算法.是对似然函数的进一步应用. 我们知道,当我们想要估计某个分布的未知值,可以使用样本结果来进行似然估计,进而求最大似然估 ...