Database Upgrade Code Learning

Source: Internet
Author: User
Tags stringbuffer

Business Scenarios and requirements: every bank in the country "refers to cooperation with the company" products on-line. It is certainly necessary to upgrade the business to meet the new functional requirements, so the upgrade script is run in the database management tool each time the database is upgraded. It's not convenient. At this point of demand: You can set the switch in the business system code, need to upgrade the switch system to open their own initiative directly completed the upgrade.


Ideas: 1) The database has a corresponding version number, the service has a version number file "To upgrade to the version number named."

2) Upgrade the time to find out the version number of the service and the database inside the version number, the comparison: assuming the same version number is not upgraded. If the version number is different, run the upgrade script inside the service.


The following is a look at the basic idea code as a note, later able to participate.


a) Get all the script version number of the service inside the file name: extended in an array form to facilitate subsequent multiple version numbers.

/** * Get full script version number file name */private arraylist<string> Getversionsforfilepath () {String filePath = "";
		arraylist<string> fileName = new arraylist<string> ();
			try {//FilePath is the folder where the database upgrade script is placed FilePath = PropertiesHander.class.getResource ("/assp/evoucher/sql/"). ToString ();
			FilePath = Filepath.replace ("File:", "");
			Get the full file name under FilePath. Dir = new file (FilePath);       Returns the file under this abstract path file[] files = dir.listfiles (); <strong>/<span style= "color: #cc0000;"
				>/expands the file "to an array because there may be very many folders or files under the file" </span></strong> for (int i = 0; i < files.length; i++) {
				Infer if this file is a file if (Files[i].isdirectory ()) {Filename.add (Files[i].getname ());
			}}//From small to large sort String templte = ""; for (int i = 0; i < filename.size ()-1; i++) {for (int j = 0; J < filename.size ()-i-1; j + +) {if (fil
						Ename.get (j). CompareTo (Filename.get (j + 1)) > 0) {templte = Filename.get (j); Filename.set (J, Filename.get (j + 1));
					Filename.set (j + 1, Templte); }}}} catch (Exception e) {throw new Evoucherexception ("Get own Proactive Upgrade script file" "+filepath+" "failed to check for correct configuration.)
		", e);
	} return fileName; }


II) obtain the corresponding upgrade script based on the current version number and the database type used by the current service

/** * Obtain the corresponding upgrade script based on the current version number and the database type used by the current service * @param version * @param userbbtype */public map<string, String[]> gets Qlscriptsfordbtype (string version, String userbbtype) {map<string, string[]> Map = new hashmap<string, string[]
		> (); try {String FilePath = DataBaseUPdateThread.class.getResource ("/assp/evoucher/sql/" + version + "/" + Userbbtype). toSt
			Ring ();
			FilePath = Filepath.replace ("File:", "");
			Get the full file name under FilePath. Dir = new file (FilePath);
			
			Returns the file under this abstract path file[] files = dir.listfiles (); for (int i = 0; i < files.length; i++) {//infer if this file is a file if (!files[i].isdirectory ()) {Map.put (files[i].g
					Etname (), This.getresourcestring (Files[i].getpath ())); Logger.info ("Get Version" "+ Version +" ", the Database type is" "+ Userbbtype +" "Corresponding upgrade Script" "+ files[i].getname () +" "is complete.

"); }}} catch (Exception e) {throw new Evoucherexception ("Get version" + version + ", database Type" "+ Userbbtype +" "phase The expected upgrade script failed. ", e); } return map; }


III) Get the database script based on the path

/**
	 * Get database script by Path
	 * @param sqlabsolutepath *
	 @return
	 * @throws Exception */
	private string[] Getresourcestring (String sqlabsolutepath) throws Exception {
		//returns the input stream reading the specified resource
		inputstream is = new FileInputStream (Sqlabsolutepath);
		BufferedReader br = new BufferedReader (new InputStreamReader (IS, "utf-8"));
		String s = "";
		StringBuffer sb = new StringBuffer ();
		while ((s = br.readline ()) = null)
			sb.append (s). Append ("\ n");
		if (br! = null) {
			br.close ();
		}
		if (is = null) {
			is.close ();
		}
		Return sb.tostring (). Split (";");
	}

Database Upgrade Code Learning

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.