Back up Oracle databases in Java

Source: Internet
Author: User
Back up Oracle databases in Java

Java thread, process, processbuilder

June 20, 2010

This article describes an example of using Java to execute the external Oracle Export command to back up the database function. This example is used to modify and create bugs, then, we will have a clearer understanding of the Java External commands (personal opinions are for reference only, and you are not allowed to make a brick)

Small details
· Try to comment out the innerth. Start () Section, and then write an error in the username or password.
· Some programs are output without command line, such as the most direct plsqldev.exe?mindmanager.exe
· Try to replace the command to be executed with the corresponding. exe program, which can be used as an example or as a dry tm.exe.

Back to Top

Sample Code:

Package Org. ybygjy. basic. oracle; </P> <p> Import Java. io. file; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import Java. NIO. bytebuffer; <br/> Import Java. NIO. channels. channels; <br/> Import Java. NIO. channels. filechannel; <br/> Import Java. NIO. channels. readablebytechannel; <br/> Import Java. util. arraylist; <br/> Import Java. util. list; <br/> impo RT Java. util. map; </P> <p>/** <br/> * back up the database in Java external system command mode <br/> * @ author wangyancheng <br/> * @ version 2010-6-19 <br /> */<br/> public class backuporacledatabase {<br/>/** <br/> * encapsulation execution entry <br/> */<br/> Public void dowork () {<br/> innerclass innerinst = <br/> New innerclass ("db_user", "db_pass", "127.0.0.1: 1521/orcl", "E: /dbback/back201006.dmp "); <br/> thread thinst = new thread (innerinst); <br/> th Inst. start (); <br/>}</P> <p>/** <br/> * used to share execution commands <br/> * @ author wangyancheng <br/> * @ version 2010-6-19 <br/> */<br/> class innerclass implements runnable {<br/>/** username */<br/> private string username; <br/>/** password */<br/> private string userpass; <br/>/** service address */<br/> private string serveraddr; <br/>/** output file path */<br/> private string outfilepath; </P> <p>/** <br/> * constructor <br/> *@ Param username <br/> * @ Param userpass <br/> * @ Param serveraddr the service address must contain the <strong> port </strong>/<strong> Sid </strong> <br/> * @ Param outfilepath outpath <br/> */<br/> Public innerclass (string username, string userpass, string serveraddr, string outfilepath) {<br/> This. username = username; <br/> This. userpass = userpass; <br/> This. serveraddr = serveraddr; <br/> This. outfilepath = o Utfilepath; <br/>}</P> <p>/** <br/> * provides the command string for execution <br/> * @ return commstr <br/> * /<br/> private list dobuildcommand () {<br/> List rtnlist = new arraylist (); <br/> rtnlist. add ("exp"); <br/> rtnlist. add ("@ user @/@ password @ server @". replaceall ("@ user @", this. username) <br/>. replaceall ("@ password @", this. userpass ). replaceall ("@ server @", this. serveraddr); <br/> rtnlist. add ("file =/" @ file @/"". repla Ceall ("@ file @", this. outfilepath); <br/> rtnlist. add ("tables = (sy_table_def)"); <br/> return rtnlist; <br/>}< br/>/** <br/> * construct a processbuilder instance <br/> * @ Param workdir current process working directory <br/> * @ return pbinst processbuilder instance <br/> * @ see processbuilder <br/> */<br/> private processbuilder buildprocessbuilder (File workdir) {<br/> New processbuilder (); <br/> List commandarray = (dobuildcommand (); <br/> Processbuilder pbinst = new processbuilder (commandarray); <br/> pbinst. command (commandarray); <br/> map envmap = pbinst. environment (); <br/> envmap. clear (); <br/> envmap. putall (system. getenv (); <br/> pbinst. directory (workdir); <br/> pbinst. redirecterrorstream (true); <br/> return pbinst; <br/>}< br/>/** <br/> * thread execution entry <br/> */<br/> Public void run () {<br/> file tmpoutfile = new file (this. outf Ilepath); <br/> file outdir = tmpoutfile. getparentfile (); <br/> processbuilder pbinst = buildprocessbuilder (outdir); <br/> try {<br/> process proinst = pbinst. start (); <br/> final inputstream ins = proinst. getinputstream (); <br/> file OUTFILE = new file (outdir, "exp. log "); <br/> OUTFILE. createnewfile (); <br/> final filechannel focinst = new fileoutputstream (OUTFILE ). getchannel (); <br/> thread Inn Erth = new thread () {<br/> bytebuffer = bytebuffer. allocate (1024); <br/> Public void run () {<br/> readablebytechannel rbcobj = channels. newchannel (INS); <br/> try {<br/> while (rbcobj. read (bytebuffer )! =-1) {<br/> bytebuffer. flip (); <br/> focinst. write (bytebuffer); <br/> bytebuffer. clear (); <br/>}< br/>} catch (ioexception IOE) {<br/> IOE. printstacktrace (); <br/>}< br/>}; <br/> // innerth. setdaemon (true); <br/> innerth. start (); <br/>}catch (ioexception IOE) {<br/> IOE. printstacktrace (); <br/>}</P> <p>/** <br/> * test entry <br/> * @ Param ARGs parameter List <br/> */<br/> Public static void main (string [] ARGs) {<br/> New backuporacledatabase (). dowork (); <br/>}< br/>


Home Page

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.