JFinal框架操作oracle数据库,需要在configPlugin()方法中配置链接oracle数据库的相关配置 配置JFinal数据库操作插件,configPlugin方法 这里我加载jdbc.properties配置文件实在configConstant加载的 @Overridepublic void configConstant(Constants me) {lo
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></object></object>
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 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");</users>

如何有效監控MySQL性能?使用mysqladmin、SHOWGLOBALSTATUS、PerconaMonitoringandManagement(PMM)和MySQLEnterpriseMonitor等工具。 1.使用mysqladmin查看連接數。 2.用SHOWGLOBALSTATUS查看查詢數。 3.PMM提供詳細性能數據和圖形化界面。 4.MySQLEnterpriseMonitor提供豐富的監控功能和報警機制。

MySQL和SQLServer的区别在于:1)MySQL是开源的,适用于Web和嵌入式系统,2)SQLServer是微软的商业产品,适用于企业级应用。两者在存储引擎、性能优化和应用场景上有显著差异,选择时需考虑项目规模和未来扩展性。

在需要高可用性、高級安全性和良好集成性的企業級應用場景下,應選擇SQLServer而不是MySQL。 1)SQLServer提供企業級功能,如高可用性和高級安全性。 2)它與微軟生態系統如VisualStudio和PowerBI緊密集成。 3)SQLServer在性能優化方面表現出色,支持內存優化表和列存儲索引。

mySqlManagesCharacterSetsetSandCollationsyutusututf-8asthEdeFault,允許ConfigurationAtdataBase,table和columnlevels,AndrequiringCarefullageLignmentToavoidMismatches.1)setDefeaultCharactersetTercharactersetEtCollacterSeteTandColletationForAdataBase.2)conformentcollecharactersettersetertersetcollatertersetcollationcollation

MySQL觸發器是與表相關聯的自動執行的存儲過程,用於在特定數據操作時執行一系列操作。 1)觸發器定義與作用:用於數據校驗、日誌記錄等。 2)工作原理:分為BEFORE和AFTER,支持行級觸發。 3)使用示例:可用於記錄薪資變更或更新庫存。 4)調試技巧:使用SHOWTRIGGERS和SHOWCREATETRIGGER命令。 5)性能優化:避免複雜操作,使用索引,管理事務。

在MySQL中創建和管理用戶賬戶的步驟如下:1.創建用戶:使用CREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';2.分配權限:使用GRANTSELECT,INSERT,UPDATEONmydatabase.TO'newuser'@'localhost';3.修正權限錯誤:使用REVOKEALLPRIVILEGESONmydatabase.FROM'newuser'@'localhost';然後重新分配權限;4.優化權限:使用SHOWGRA

MySQL適合快速開發和中小型應用,Oracle適合大型企業和高可用性需求。 1)MySQL開源、易用,適用於Web應用和中小型企業。 2)Oracle功能強大,適合大型企業和政府機構。 3)MySQL支持多種存儲引擎,Oracle提供豐富的企業級功能。

MySQL相比其他關係型數據庫的劣勢包括:1.性能問題:在處理大規模數據時可能遇到瓶頸,PostgreSQL在復雜查詢和大數據處理上表現更優。 2.擴展性:水平擴展能力不如GoogleSpanner和AmazonAurora。 3.功能限制:在高級功能上不如PostgreSQL和Oracle,某些功能需要更多自定義代碼和維護。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!