JFinal框架操作oracle数据库
来源:互联网 发布:清华大学软件专业课程 编辑:程序博客网 时间:2024/06/05 09:39
JFinal框架操作oracle数据库,需要在configPlugin()方法中配置链接oracle数据库的相关配置
配置JFinal数据库操作插件,configPlugin方法
这里我加载jdbc.properties配置文件实在configConstant加载的
@Overridepublic 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.OracleDriveroracle.url=jdbc:oracle:thin:@127.0.0.1:1521:orcloracle.username=scottoracle.password=xiaohuconfig.devModel=true
@Overridepublic 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()});elsepst = 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));elseset(pKey, rs.getObject(1));// It returns Long object for int colTypers.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");
0 0
- JFinal框架操作oracle数据库
- JFinal对oracle数据库的操作配置
- JFinal连接Oracle数据库实战
- JFinal学习--06操作数据库
- Jfinal数据库操作API总结
- jfinal main方法里操作数据库+redis
- Jfinal框架
- [JFinal 2] JFinal 开发框架
- java利用poi和jfinal框架将excel导入数据库
- 使用JFinal框架连接数据库,实现注册、登录功能
- Jfinal 连接oracle数据关于数据库大小的问题
- Jfinal 连接Oracle数据库 出现FROM不到关键字的解决方法
- 上班之--JFinal框架
- JFinal框架学习debug
- jfinal框架初识
- JFinal框架简单学习
- JFinal框架小结
- JFinal框架入门
- 中国移动互联网用户行为统计报告2014H1(完整版)
- [除錯]引動過程的目標傳回例外狀況
- 杭电3732 Ahui Writes Word (多重背包问题)
- (原创)底层最基本的也是用的最多的条件查询方法实现,有点多!(已经更新)
- jquery easyui datagrid基本功能
- JFinal框架操作oracle数据库
- Oracle中几个关于日期方面的SQL实例
- 清华大学留学班2014年招生--美加英澳名校直通车
- service使用以及startService,stopService,bindService,unbindService方法比较
- PACKAGE “VISUAL C++ PACKAGE” FAILED TO LOAD
- ArcGIS API for javascript 空间查询 没有结果 解决(二)
- 泛型迭代器-摘自《C++沉思录》Andrew Koenig
- 最短路径 -Java 迪杰斯特拉算法
- 清华大学留学班2014年招生--美加英澳名校直通车