Java implementation from one database query data processed into another database

Source: Internet
Author: User
Tags oracleconnection uuid

When there are CLOB fields in a database table or if you want to do more complex processing of the data in the table, the script will import data from one database into another database, then it will be implemented by code, the following example code for the Orale database is as follows:

Import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.timestamp;import Java.text.simpledateformat;import Java.util.UUID;import Oracle.jdbc.driver.oracleconnection;import oracle.sql.clob;/** * Import data from one database to another * * @author * @version */public class Importdatabyjdbc {public static void main (string[] args) throws Exception {Connection con = null; Create a database connection PreparedStatement pre = null;//Create a precompiled statement object, typically with this instead of statementresultset result = null;//Create a result set object try { Class.forName ("Oracle.jdbc.driver.OracleDriver");//Load the Oracle driver System.out.println ("Start trying to connect to the CMS database!") "); String url = "jdbc:oracle:" + "THIN:@192.168.0.XX:1521:ORCL"; String user = "policy";//username, system default account name string password = "xxxx";//The password you set to install is con = drivermanager.getconnection (url, user, p Assword);//Get Connection System.out.println ("CMS connection succeeded! "); String sql = "Select t.title,t.channel_code,t.content from Cms_news t";//Pre-compiled statements, "? "Delegate parameter pre = Con.preparestAtement (SQL);//Instantiate precompiled statement result = Pre.executequery ();//Execute query, note that no additional parameter importdata (result) is required in parentheses;//while (Result.next ())/ /When the result set is not empty//System.out.println ("Username:" + result.getstring ("title"));} catch (Exception e) {e.printstacktrace ()} finally {try {//} closes several of the above objects, because the words that do not close affect performance, and consume resources//pay attention to the order in which they are closed, and the last use of the first close if ( result = null) result.close (); if (pre! = null) pre.close (); if (con! = null) con.close (); SYSTEM.OUT.PRINTLN ("CMS database connection is down! ");} catch (Exception e) {e.printstacktrace ();}}} public static String ImportData (ResultSet result) {Connection con = null;//Create a database connection PreparedStatement pre = null;//Create precompiled Statement objects, which are generally used instead of statementresultset RESULT2 = null;//Create a result set object Timestamp currenttime = new Timestamp ( System.currenttimemillis ()); SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); String datestring = Formatter.format (currenttime); try {class.forname ("oracle.jdbc.driver.OracleDriver");// Load the Oracle driver System.out.println ("Start trying to connect to the OMP database!") "); String url = "jdbc:oracle:" + "thin:@192.168.0.xx:1521: ORCLGBK "; String user = "xjsp";//username, system default account name string password = "xxx";//The password you set to install is con = drivermanager.getconnection (URL, user, pass Word);//Get Connection System.out.println ("OMP connection succeeded! int num = 0;while (Result.next ()) {String id = getuuid (); String entrytype = Getentrytype (result.getstring ("Channel_code")); String sql = "INSERT into Kms_entry (Id,entry_name,entry_type,entry_content,edit_flag,audit_time,if_del,if_audit, Last_edit_time,create_user,create_time,last_edit_user) "+" values (' "+ id+" ', ' "+ result.getstring (" title ") +" ', ' "+ Entrytype+ "',?, ' 1 ', to_date ('" + datestring+ "', ' yyyy-mm-dd hh24:mi:ss '), ' 1 ', ' 3 ', to_date ('" + datestring+ "', ' Yyyy-mm-dd hh24:mi:ss '), ' 00000000000000000000000000000000 ', to_date (' "+ datestring+" ', ' yyyy-mm-dd hh24:mi:ss '), ' 00000000000000000000000000000000 ') ";//Pre-compile statement,"? "represents the parameter pre = con.preparestatement (SQL);//instantiates the precompiled statement Clob CLOB = new Clob ((OracleConnection) con); Create an instantiated object CLOB = Oracle.sql.CLOB.createTemporary ((oracleconnection) con,true, 1); clob.putstring (1, Result.getsTring ("CONTENT")); Assigns an object Pre.setclob (1, CLOB);p re.execute ();p re.close (), num = num + 1; System.out.println ("Insert success First" + (num) + "bar Data");}} catch (Exception e) {e.printstacktrace ()} finally {try {//} closes several of the above objects, because the words that do not close affect performance, and consume resources//pay attention to the order in which they are closed, and the last use of the first close if ( result = null) result.close (); if (pre! = null) pre.close (); if (con! = null) con.close (); System.out.println ("The OMP database connection is closed! ");} catch (Exception e) {e.printstacktrace ();}} return null;} /** * Get UUID * * @return */public static String getuuid () {uuid uuid = Uuid.randomuuid (); String s = uuid.tostring (); String UUIDs = s.substring (0, 8) + s.substring (9, +) + s.substring (+) + s.substring (+) + s.substring (+); return UUIDs;} public static string Getentrytype (String chanelcode) {string entrytypestring = ""; if (chanelcode! = "" && Chanelco De! = NULL) {processing data ...} return entrytypestring;}}
Where the Clob field is placed into the Clob object and then inserted, Getuuid is required to save when the primary key of the table in the database is not self-growing, and if the database table is set to self-growing primary key, this is not required.




Java implementation from one database query data processed into another database

Related Article

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.