Links to the Java Code Toolbox Oracle

Source: Internet
Author: User

1. ODBC jar package required for Oracle

2. Code

3. Note: PS objects and statement objects are best used for immediate release, especially when the read-write database code appears in the FOR Loop statement.

Otherwise, there will be insufficient cursors, exceeding the cursor error, such as the maximum number of cursors!

4. Case: When I imported Excel into an Oracle database, I was not impressed by the search for online code. When you traverse every row of data in Excel, you execute the

pre = con.preparestatement (sql) . The pre.close () is not executed until the 5000 rows of data in Excel have been traversed, and the result is an error, appearing

Cursor error exceeded! It was later corrected to execute pre.executequery () within the For loop, followed by immediate execution of Pre.close ();

A pre-empty judgment statement). Once corrected, 5,000 data was imported smoothly.

1  Public voidtestoracle ()2 {3Connection con =NULL;//Create a connection to a database4PreparedStatement pre =NULL;//Create precompiled statement objects, usually with this instead of statement5ResultSet result =NULL;//Create a result set object6     Try7     {8Class.forName ("Oracle.jdbc.driver.OracleDriver");//load the Oracle driver9System.out.println ("Start trying to connect to the database!") ");TenString url = "jdbc:oracle:" + "thin:@127.0.0.1:1521:xe";//127.0.0.1 is a native address, XE is the default database name for Lite Oracle OneString user = "System";//user name, system default account name AString Password = "147";//The password you selected when you installed the settings -con = drivermanager.getconnection (url, user, password);//Get Connections -SYSTEM.OUT.PRINTLN ("Connection Successful! "); theString sql = "SELECT * from student where name=?";//precompiled statement, "? The delegate parameter -Pre = con.preparestatement (SQL);//Instantiating precompiled Statements -Pre.setstring (1, "Liu Xianhan");//The parameter is set, and the previous 1 indicates the index of the parameter, not the index of the column name in the table -result = Pre.executequery ();//Execute the query, note that no additional arguments are required in parentheses +          while(Result.next ()) -             //When the result set is not empty +System.out.println ("School Number:" + result.getint ("id") + "name:" A+ result.getstring ("name")); at     } -     Catch(Exception e) -     { - e.printstacktrace (); -     } -     finally in     { -         Try to         { +             //close each of the above objects individually, because if you do not close it, it will affect performance and consume resources -             //Note the order of the closing, the last used first close the             if(Result! =NULL) * result.close (); $             if(Pre! =NULL)Panax Notoginseng pre.close (); -             if(Con! =NULL) the con.close (); +SYSTEM.OUT.PRINTLN ("The database connection is closed! "); A         } the         Catch(Exception e) +         { - e.printstacktrace (); $         } $     } -}

Links to the Java Code Toolbox Oracle

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.