JDBC-connected Oracle optimized version

Source: Internet
Author: User

Start by first preparing to test if the Oracle database starts normally "is the primary Oracle-related service open"

Then create a class that connects to the database with the first thing to remember is what steps are required to connect to the database, and what parameters are required for these steps 1. Load class-driven parameters: the corresponding driver name"2. Get Connection" parameter: URL , user name, password "You can then get a connection to the connection database. Configure the parameters first.        Private   Static  String   classname =  " Oracle.jdbc.driver.OracleDriver "  ;       private static String url="Jdbc:oracle:thin : @192.168.2.2:1521:java46 " ;        private static String username= "Scott" ;       private static String password= "NIIT" ;1. Load DriverClass. forname( classname); 2. Get the connection Connection =drivermanager. Getconnection( url, username, password); Package Com.niit.common;   Import java.sql.Connection; Import Java.sql.DriverManager; Import java.sql.SQLException;   Public class jdbcconnectfactory { Class-Driven name       private static String classname= " Oracle.jdbc.driver.OracleDriver " ; Url       private static String url="Jdbc:oracle:thin : @192.168.2.2:1521:java46 " ; Connect to database user name       private static String username= "Scott" ;Password to connect to the database       private static String password= "NIIT" ;Declares aConnection       private static Connection Connection = Null; The method is statically decorated to form a static code block, and the value is loaded once      Public static Connection getconnection () {              Try {                    Try { Singleton design Mode "There is only one connect for the entire project, if it is empty"                            If  < Span style= "color: #0000c0; Font-family:courier New; Font-size:small; " > connection  ==   null  ||   Connection .isclosed ()) {Load Class-drivenClass. forname( ClassName);Get database connection                               Connection =drivermanager. getconnection (   URL   Username     Password                         }                  } catch (SQLException e) { e.printstacktrace ();                  }            } catch (classnotfoundexception e) { e.printstacktrace ();            }Back to Connection             return connection;       } }

JDBC-connected Oracle optimized version

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.