Application First Run database Configuration applet (Java Edition)

Source: Internet
Author: User
Tags generator gettext

Application Initialization Database Configuration applet

Previously wrote a Java version of the information management system, but the deployment of the system also need to manually configure the database and import some initialized data to let the system run, so I was wondering if you can write a small program to automatically deploy the database and import some initialization data when the system is first run. Then we have the following ideas:

At the entrance of the application to determine whether the database has been configured to complete, if the configuration is completed into the normal login system to complete normal operation can, if not configured to enter the database configuration of the small program to complete the database configuration and then enter the system, but how to determine whether it has been configured to complete, here I use the comparison of the original method, When configuring the database, the system automatically generates a database-related configuration file in the relevant directory, which describes the type of database, the user name of the database, the password of the database (which is addressed after the security issue), and the database name that the application uses for the database that is about to be automatically created. Therefore, when the application starts, the system will detect the relevant path under the configuration file, if the file exists, the database configuration is complete, otherwise the system is the first run, you need to start the database configuration applet. After entering the database configuration applet, a pop-up dialog will pop up, we need to select the database to use and enter the database user name, password and the name of the database used by the system, click the Test button to see if you can connect to the database, if you can complete the connection will be a more initialization button, Clicking the Initialize button executes the corresponding operations such as creating a database data table and importing data, which can be done by reading the initialization file (which is the SQL statement that was initialized in the file), completing the initialization of the entire database, and then invoking the normal login program to log into the system.

So the entire process is summed up by running the application--to determine if there is a database configuration file (config. metabase) in the relevant directory (with the normal login program)--and if not, run the database initialization applet--Select the database type, Enter the user name password and the name of the newly created database-click the Test button (the test does not retry by checking the database)-click the Initialize button to complete the initialization when the initialization task is complete and jump to the normal login program.

Procedure entry Judgment procedure

 PackageHxy;ImportJava.io.File;classCreatedbconfig { PublicCreatedbconfig () {Try{File File=NewFile ("config"); File FilePath=NewFile ("Config//config.xml"); if(!file.exists ()) {                if(File.mkdir ()) {if(!filepath.exists ()) {                        NewDbform (); }Else{System.out.print ("Configuration file creation failed!!!" "); }                                            }                Else{System.out.println ("Directory Creation failed!!!" "); }            }            Else{                if(!filepath.exists ()) {                    NewDbform (); }Else{System.out.println ("The database has been configured successfully directly into the application!!! "); }            }        } Catch(Exception e) {e.printstacktrace (); }        }}

Database selection interface and Control monitor program (complete the collection of configuration data and then hand it to the configuration file generator to generate the appropriate profile)

Here the interface is mainly a drop-down list box to provide the type of database selection, currently I only provide MySQL and SQL Server two kinds of database selection, interested can add their own additional database related options, here also I did not consider that is the location of the database, Because I write here mainly to connect to the local database so the address in my database connection string is localhost (127.0.0.1), if we need to connect to the network database, we can add the connection string input box in the dialog box, so you can connect to the network database, so the interface is only the user name and Password and the description of the database name, of course, if you need more detailed configuration, we can also add more detailed sub-tags in the configuration file and set more detailed options in the dialog box.

 PublicDatabase Getdatabase () {Database db=NewDatabase (); String DbType=Databasetype.getselecteditem (). toString (). Trim ();        System.out.println (DbType); String DbUser=username.gettext (). Trim (); @SuppressWarnings ("Deprecation") String Dbpassword=password.gettext (). Trim (); String DbName=databasename.gettext (). Trim (); if(dbtype==NULL|dbuser==NULL|dbpassword==NULL|dbname==NULL){            NewJoptionpane (). Showmessagedialog (OKButton, "please complete database information!!!");; }        Else{            if(Dbtype.equals ("MYSQL") {db.settype (dbType); Db.setdriver ("Com.mysql.jdbc.Driver"); Db.seturi ("LocalHost"); Db.setport ("3306");                Db.setuser (DbUser);                Db.setpassword (Dbpassword);            Db.setname (DbName); }            Else{db.settype (dbType); Db.setdriver ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"); Db.seturi ("LocalHost"); Db.setport ("1433");                Db.setuser (DbUser);                Db.setpassword (Dbpassword);            Db.setname (DbName); }        }                    returnDB; }

In the pop-up dialog we fill in the detailed database configuration information, which is synthesized into an entity class database, so that these configurations are set to private member variables, This makes it necessary to invoke the XML file parser in subsequent calls to obtain the appropriate configuration and inject the configuration information into a database entity so that the entity has all the configuration information. The connector only needs to call this entity to get the information needed to connect with the database to complete the connection and later database interaction.

The configuration file generator is to write the information about the database to the configuration file for the system to connect to the database when read, so there is a good benefit of generating the configuration file here is that my program does not need to write dead is in the program need to completely write the information about the database to enter, Here I just need to read the configuration file and inject it into the relevant method. As for the configuration file generator is to create a file, and then according to the corresponding configuration data written to the file, of course, this is an XML file, you can use dom4j and so on to scientific writing and modification, but this is relatively simple and rude, as for the XML file Parser and generator program in the future blog post corresponding to.

This small program is entirely because of their own small ideas and small guess to practice writing and applied to my previous information management system, the above is a small program all the code part, the program complete code in my GitHub (https://github.com/ Huangxinyuan650/connectdatabase) Welcome everyone to criticize

Application First Run database Configuration applet (Java Edition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.