This article uses the form of loading a configuration file to interact with the database
PS: The database is using PostgreSQL
1. Load configuration file
1 Public void configconstant (Constants me) {23 Propkit.use ("Config.properties"); 4 5 }
2. Config.properties Configuration
1 jdbcurl = jdbc:postgresql://Localhost:5432/liubaohua2 username = Postgres 3 password = postgres4 Driver = Org.postgresql.Driver
3. Connect to the database using C3p0plugin
PS: Here you need to import c3p0-0.9.5.1, mchange-commons-java-0.2.10, Mysql-connector-java-5.1.20-bin
As for Mysql-connector-java-5.1.20-bin what import, it is unclear.
1 Public voidConfigplugin (Plugins me) {2C3p0plugin C3p0plugin =NewC3p0plugin (Propkit.get ("Jdbcurl"), Propkit.get ("username"), Propkit.get ("password"), Propkit.get ("Driver"));3Activerecordplugin ARP =NewActiverecordplugin (C3p0plugin); 4Arp.setshowsql (true); 5Arp.addmapping ("Post_user", "user_id", user.class);6 Me.add (c3p0plugin);7 Me.add (ARP);8Arp.setdialect (Newpostgresqldialect ());9 Ten}
4. Call the database action method in the action method in the controller
1 Public voidSave () {2 //User Delete3User.user.deleteById (4);4 5 //User Add6 NewUser (). Set ("User_name", "Bao Hua" +NewRandom (). Nextint (100))7. Set ("User_password", "123")8. Set ("user_class_id", 1)9 . Save ();Ten OneRenderText ("User added successfully ..."); A //User Query -User user = User.user.findById (2); -RenderText (User.getstr ("user_name")); the //User Modification -User.user.findById (1). Set ("User_name", "Zhang Mowgli"). Update (); - //Query Statements -String likestr = "SELECT * from Post_user where user_name like '% treasure% '"; +List<user> userlist =User.user.find (LIKESTR); -String renderstr = ""; + for(User user2:userlist) { ARenderstr + = User2.getstr ("user_name"); atRenderstr + = "---"; - } - RenderText (RENDERSTR); - //paged Query statement parameter list (Start page, number of bars per page, query fields, tables and conditions "using placeholders", "setting placeholders") -page<user> userpage = User.user.paginate (1, 8, -"SELECT *", "from Post_user where user_name like?", "% hua"); in -Renderstr = ""; to for(User user2:userPage.getList ()) { +RENDERSTR = "--"; -Renderstr + = User2.getstr ("user_name") + "\ n"; the } *RenderText ("Total Pages:" +userpage.gettotalpage () + "\ t total:" +userpage.gettotalrow () + "bar data" $+ "\ t total:" +userpage.gettotalpage () + "page" + "\ T Current page:" +Userpage.getpagenumber ()Panax Notoginseng+ "\ n" +renderstr); - the}
Reprint please mark the source @ China line the world
Jfinal Connection Database Configuration instructions