jfinal连接oracle_JFinal框架操作oracle数据库
JFinal框架操作oracle數(shù)據(jù)庫(kù),需要在configPlugin()方法中配置鏈接oracle數(shù)據(jù)庫(kù)的相關(guān)配置 配置JFinal數(shù)據(jù)庫(kù)操作插件,configPlugin方法 這里我加載jdbc.properties配置文件實(shí)在configConstant加載的 @Overridepublic void configConstant(Constants me) {lo
JFinal框架操作oracle數(shù)據(jù)庫(kù),需要在configPlugin()方法中配置鏈接oracle數(shù)據(jù)庫(kù)的相關(guān)配置
配置JFinal數(shù)據(jù)庫(kù)操作插件,configPlugin方法
這里我加載jdbc.properties配置文件實(shí)在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);//設(shè)置數(shù)據(jù)庫(kù)方言
arp.setDialect(new OracleDialect());
arp.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大小寫
me.add(new EhCachePlugin());
arp.addMapping("users", "id",Users.class);
me.add(arp);
}
需要注意一點(diǎn)的是,由于oracle數(shù)據(jù)庫(kù)中在創(chuàng)建表時(shí),會(huì)自動(dòng)的將所有的字段自動(dòng)轉(zhuǎn)為大寫,因此在避免后面操作的時(shí)候出現(xiàn)大小寫錯(cuò)誤的相關(guān)異常,這里需要配置忽略大小寫的功能
arp.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大小寫
如果不需要對(duì)數(shù)據(jù)庫(kù)進(jìn)行增加操作,則必須配置忽略大小寫,如果不配置忽略大小寫,在保存源代碼的該段代碼中會(huì)出現(xiàn)屬性id找不到的異常
/**
* Save model.
*/
public boolean save() {
Config config = getConfig();
Table table = getTable();
StringBuilder sql = new StringBuilder();
Listparas = new ArrayList();
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();
getGeneratedKey(pst, table);//如果不配置忽略大小寫,執(zhí)行到這里會(huì)出現(xiàn)異常,雖然可以添加到數(shù)據(jù)庫(kù),但是這里報(bào)錯(cuò),界面還是會(huì)顯示500錯(cuò)誤getModifyFlag().clear();
return result >= 1;
} catch (Exception e) {
throw new ActiveRecordException(e);
} finally {
config.close(pst, conn);
}
}
getGeneratedKey()源代碼部分
/**
* 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) {
if (getTable().hasColumnLabel(attr)) {//執(zhí)行到這里返回false
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);//拋出該異常
}
現(xiàn)在來說說如果不配置,為什么會(huì)出現(xiàn) The attribute name is not exists:這個(gè)異常,這是因?yàn)閛racle中的字段是大寫的,而set方法中傳入的attr屬性的值是小寫,而getTable()中的屬性對(duì)應(yīng)的就是oracle字段,這些屬性則是大寫,因此這里使用getTable().hasColumnLabel(attr)判斷是否存在該字段,就會(huì)找不到,這時(shí)就會(huì)拋出該異常,因此就必須配置忽略大小寫的方法,就不會(huì)出現(xiàn)該異常
實(shí)體類:
package com.tenghu.core.model;
import com.jfinal.plugin.activerecord.Model;
public class Users extends Model{
public static Users dao=new Users();
}
操作數(shù)據(jù):
Users users=new Users();
users.set("id", "users_sequence.nextval");
users.set("username", "張三");
users.set("pwd", "sdfsdfs");
users.save();
ListtestList=Users.dao.find("select * from users");
這里就完成了JFinal框架操作oracle數(shù)據(jù)庫(kù),刪除和修改就自己去測(cè)試了
本文原創(chuàng)發(fā)布php中文網(wǎng),轉(zhuǎn)載請(qǐng)注明出處,感謝您的尊重!
總結(jié)
以上是生活随笔為你收集整理的jfinal连接oracle_JFinal框架操作oracle数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中static{}语句块详解
- 下一篇: identifier of an ins