Example of JDBC database connection (Oracle)

Source: Internet
Author: User

Import java. SQL .*;

 

Public class ora8iconnect
{
Public ora8iconnect (string dB, string ID, string PWD)
{Dbname = dB;
Userid = ID;
Userpwd = PWD;
Beginconnect (); // connect to the database
}
/*-
* Returns a connection object.
*/
Public connection getconnection () {return conn ;}

/*-
* Connect to the database. If the connection succeeds, 1 is returned. Otherwise, 0 is returned.
*/
Public int beginconnect ()
{Try
{// Load an oracle driver
Drivermanager. registerdriver (New Oracle. JDBC. Driver. oracledriver ());
// Use oci8 to connect to the database
Conn = drivermanager. getconnection ("JDBC: oracle: oci8: @" + dbname, userid, userpwd );
Return 1;
}
Catch (sqlexception e) // capture SQL violations
{System. Out. println ("captured when ora8iconnect connects to the oracle8 Database ");
While (E! = NULL)
{System. Out. println ("sqlstate:" + E. getsqlstate ());
System. Out. println ("message:" + E. getmessage ());
System. Out. println ("vendor:" + E. geterrorcode ());
E = E. getnextexception ();
System. Out. println ("");
}
Conn = NULL;
Return 0;
}
}

Private connection conn; // connection object
Private string dbname; // instance
Private string userid; // User Name
Private string userpwd; // Password

}

 

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.