Connect to the database through JDBC

Source: Internet
Author: User

Some time ago, most people used Java to Design Database courses. Many people encountered problems when connecting to the database.

Problem 1: <error> listener refused the connection with the following error:
ORA-12514, TNS: Listener does not currently know of Service requested in connect Descriptor
The connection descriptor used by the client was:
/// Localhost: 1521/ecmis

It turns out that"ORA-12541: TNS: No listener" caused by IP address changesProgram"Error

SolutionMethod:

View the content of listener. ora:
# Listener. ora network configuration file: D: \ oracle \ product \ 10.2.0 \ db_1 \ Network \ admin \ listener. ora
# Generated by Oracle configuration tools.

Sid_list_listener =
(Sid_list =
(Sid_desc =
(Sid_name = plsextproc)
(ORACLE_HOME = D: \ oracle \ product \ 10.2.0 \ db_1)
(Program = EXTPROC)
)
)
Listener =
(Description_list =
(Description =
(Address = (Protocol = IPC) (Key = extproc1 ))
(Address = (Protocol = TCP )(Host= Localhost) (Port = 1521 ))
)
)

 

After the IP address of the local machine changes, the above problem occurs. Change the IP address of the Database Listener:
(Address = (Protocol = TCP )(Host= Localhost) (Port = 1521 ))
Change
(Address = (Protocol = TCP )(Host= Computer name) (Port = 1521 ))

Restart the listener.

Question 2: JDBC connection to Oracle

1. Obtain the Oracle database connection through JDBC locally

You can use JDBC to obtain Oracle database connections in three ways: OCI, thin, and jdbcodbc. The OCI method depends on the local dynamic link library. This method can be used if the Oracle database client is installed locally. The thin method is a Java-only database connection method; the jdbcodbc bridge method depends on the configuration of the local ODBC database source, which is generally not used.

1. OCI Mode

First, install the Oracle client locally. After installation, find... The/jdbc/lib/classes12.zip file sets the path of classes12.zip in the environment variable classpath.

Then, use the following database connection classes to locally obtain the Oracle database connection through OCI.

/**
* obtain a local database connection
*/

package COM. j2EE. DB;

Import Java. util. *;
Import Java. SQL. *;
Import javax. SQL. *;
Import Java. io. *;
Import oracle. JDBC. driver. *;
Import javax. naming. *;

/**
* obtain the Oracle database connection through OCI
*/
public class dbconnection
{< br> final static string sdbdriver = "oracle. JDBC. driver. oracledriver ";
final static string sconnstr =" JDBC: oracle: oci8: Sr/SR @ ora199 ";

/**
*/
Public dbconnection ()
{< BR >}

/**
* obtain the Oracle database connection
*/
Public Java. SQL. connection connectdbbyoci ()
{< br> JAVA. SQL. connection conn = NULL;
try
{< br> class. forname (sdbdriver);
conn = drivermanager. getconnection (sconnstr);
}< br> catch (exception e)
{< br> system. out. println ("error:" + E. getmessage ();
}< br> return conn;
}

In the connection string "JDBC: oracle: oci8: Sr/SR @ ora199", "Sr/SR" is the username and password of the Oracle user, and "ora199" is the database service name.
2. Thin Mode

First to Oracle Technology Network (http://otn.oracle.com/global/cn/software/tech/java
/Sqlj_jdbc/index.html) download oracle JDBC
Drivers also sets the path of the downloaded ZIP file to the environment variable classpath.

Then, use the following database connection classes to locally obtain the Oracle database connection through thin.

/**
* Obtain a local database connection
*/

Package com. J2EE. dB;

Import java. util .*;
Import java. SQL .*;
Import javax. SQL .*;
Import java. Io .*;
Import oracle. JDBC. Driver .*;
Import javax. Naming .*;

/**
* Obtain the Oracle database connection through thin
*/
Public class dbconnection
{
Private string sconnstr = "";

/**
* Default constructor
*/
Public dbconnection ()
{
Sconnstr = "JDBC: oracle: thin: @ 10.1.4.199: 1521: ora199 ";
}

/**
* @ Param IP, servicename
*/
Public dbconnection (string IP, string servicename)
{
Sconnstr = "JDBC: oracle: thin: @" + IP + ": 1521:" + servicename;
}

/**
* Use thin to obtain the connection to the Oracle database.
*/
Public java. SQL. Connection connectdbbythin ()
{
Java. SQL. Connection conn = NULL;
Try
{
Class. forname (sdbdriver );
Conn = drivermanager. getconnection (sconnstr, "Sr", "Sr ");
}
Catch (exception E)
{
System. Out. println ("error:" + E. getmessage ());
}
Return conn;
}

/**
* Use thin to obtain the connection to the Oracle database.
* @ Param userid, password
*/
Public java. SQL. Connection connectbyjdbc (string userid, string password)
{
Java. SQL. Connection conn = NULL;
Try
{
Class. forname (sdbdriver );
Conn = drivermanager. getconnection (sconnstr, userid, password );
}
Catch (exception E)
{
System. Out. println ("error:" + E. getmessage ());
}
Return conn;
}
}



This method is flexible, simple, and has high portability and applicability. Pay attention to the settings of the specific parameters in the connection string "JDBC: oracle: thin: @ 10.1.4.199: 1521: ora199.
3. jdbcodbc Bridge

First, add a local connection to the Oracle database through the data source in the management tool, and then obtain the Oracle database connection locally through the jdbcodbc bridge through the following database connection classes.

/**
* obtain a local database connection
*/

package COM. j2EE. DB;

Import Java. util. *;
Import Java. SQL. *;
Import javax. SQL. *;
Import Java. io. *;
Import oracle. JDBC. driver. *;
Import javax. naming. *;

/**
* obtain the Oracle database connection through the jdbcodbc bridge
*/
public class dbconnection
{
/**
*/
Public dbconnection ()
{< BR >}

/**
* obtain the Oracle database connection
*/
Public Java. SQL. connection connectdbbyjdbcodbcbridge ()
{< br> JAVA. SQL. connection conn = NULL;
try
{< br> class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
con = drivermanager. getconnection ("JDBC: ODBC: ora199", "Sr", "Sr");
}< br> catch (exception e)
{< br> system. out. println ("error:" + E. getmessage ();
}< br> return conn;
}

In the getconnection method, "ora199" in the first parameter "JDBC: ODBC: ora199" is the name of the local ODBC data source, the second and third parameters are the username and password of Oracle respectively.

 

 

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.