To operate the oracle database using the JFinal framework, you need to configure the relevant configuration of the link oracle database in the configPlugin () method to configure the JFinal database operation plug-in. In the configPlugin method, I load jdbc here. the properties configuration file is actually loaded by configConstant @ OverridepublicvoidconfigConstant (Constantsme) {lo
To operate the oracle database using the JFinal framework, you need to configure the relevant configuration of the link oracle database in the configPlugin () method to configure the JFinal database operation plug-in. In the configPlugin method, I load jdbc here. the properties configuration file is actually loaded by configConstant @ Overridepublic void configConstant (Constants me) {lo
To operate the oracle database using the JFinal framework, you must configure the configuration of the link to the oracle database in the configPlugin () method.
Configure the JFinal database operation plug-in, configPlugin Method
Here I load the jdbc. properties configuration file, which is actually loaded by configConstant.
@ Overridepublic void configConstant (Constants me) {loadPropertyFile ("jdbc. properties "); // load the configuration file me. setDevMode (getPropertyToBoolean ("config. devModel ", false); me. setViewType (ViewType. JSP); me. setEncoding ("UTF-8 ");}
Jdbc. properites configuration file
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); // sets the database dialect arp. setDialect (new OracleDialect (); arp. setContainerFactory (new CaseInsensitiveContainerFactory (); // ignore the case of me. add (new EhCachePlugin (); arp. addMapping ("users", "id", Users. class); me. add (arp );}
Note that when creating a table in the oracle database, all fields are automatically converted to uppercase, so exceptions related to case-insensitive operations are avoided, the case-insensitive function needs to be configured here.
Arp. setContainerFactory (new CaseInsensitiveContainerFactory (); // case insensitive
If you do not need to add an operation to the database, you must configure case-insensitive settings. If you do not configure case-insensitive settings, an error occurs when the property id cannot be found in the code that saves the source code.
/*** 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 ()}); elsepst = conn. prepareStatement (SQL. toString (), Statement. RETURN_GENERATED_KEYS); config. dialect. fillStatement (pst, paras); result = pst.exe cuteUpdate (); getGeneratedKey (pst, table); // If Case Insensitive is not configured, an exception occurs during execution, although it can be added to the database, an error is reported, and the page still displays Error 500.getModifyFlag().clear();return result >= 1;} catch (Exception e) {throw new ActiveRecordException(e);} finally {config.close(pst, conn);}}
GetGeneratedKey () source code
/** * 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 () source code
/*** 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) {// returns falseattrs. 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); // throw this exception}
Now let's talk about why The attribute name is not exists if it is not configured: this exception occurs because The oracle field is capitalized, the value of the attr attribute passed in the set method is in lower case, while the attribute in getTable () corresponds to the oracle field, and these attributes are in upper case. Therefore, getTable () is used here (). hasColumnLabel (attr) determines whether this field exists and cannot be found. This exception is thrown. Therefore, you must configure the case-insensitive method to avoid this exception.
Entity class:
package com.tenghu.core.model;import com.jfinal.plugin.activerecord.Model;public class Users extends Model
{public static Users dao=new Users();}
Operation data:
Users users = new Users (); users. set ("id", "users_sequence.nextval"); users. set ("username", "Michael"); users. set ("pwd", "sdfsdfs"); users. save (); List
TestList = Users. dao. find ("select * from users ");
Here we have completed the JFinal framework to operate the oracle database, and deleted and modified it and tested it on our own.