Example of Using OCI:
-
// This example comes from Oracle official Docs.
-
import java.sql.*;
-
import oracle.jdbc.*;
-
import oracle.jdbc.pool.OracleDataSource;
-
-
// We import java.io to be able to read from the command line import java.io.*;
-
class JdbcCheckup {
-
public static void main(String args[]) throws SQLException, IOException {
-
// Prompt the user for connect information
-
System.out.println("Please enter information to test connection to the database");
-
String user;
-
String password;
-
String database;
-
-
user = readEntry("user: ");
-
int slash_index = user.indexOf('/');
-
if (slash_index != -1)
-
{
-
password = user.substring(slash_index + 1);
-
user = user.substring(0, slash_index);
-
}
-
else
-
password = readEntry("password: ");
-
-
database = readEntry("database(a TNSNAME entry): ");
-
System.out.print("Connecting to the database...");
-
System.out.flush();
-
System.out.println("Connecting...");
-
// Open an OracleDataSource and get a connection
-
OracleDataSource ods = new OracleDataSource();
-
ods.setURL("jdbc:oracle:oci:@" + database);
-
ods.setUser(user);
-
ods.setPassword(password);
-
Connection conn = ods.getConnection();
-
System.out.println("connected.");
-
// Create a statement
-
Statement stmt = conn.createStatement();
-
// Do the SQL "Hello World" thing
-
ResultSet rset = stmt.executeQuery("select 'Hello World' from dual");
-
while (rset.next())
-
System.out.println(rset.getString(1));
-
// close the result set, the statement and the connection
-
rset.close();
-
stmt.close();
-
conn.close();
-
System.out.println("Your JDBC installation is correct.");
-
}
-
// Utility function to read a line from standard input
-
static String readEntry(String prompt){
-
try {
-
StringBuffer buffer = new StringBuffer();
-
System.out.print(prompt);
-
System.out.flush();
-
int c = System.in.read();
-
while(c!='\n'&&c!=-1) {
-
buffer.append((char)c); c = System.in.read();
-
}
-
return buffer.toString().trim();
-
}
-
catch(IOException e) {
-
return "";
-
}
-
}
-
}
-
Example of Using Thin:
-
jdbc:oracle:thin:[scott/tiger]@HostnameOrIP:PortNum:SID
-
-- using [scott/tiger] means u can use it or not
-
jdbc:oracle:thin:[scott/tiger]@HostnameOrIP:PortNum/service_name -- using service_name
-
jdbc:oracle:thin:[scott/tiger]@(DESCRIPTION = -- using tns string
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.11)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.12)(PORT = 1521))
(LOAD_BALANCE = no)
(FAILOVER = ON)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = oradb)
(FAILOVER_MODE =
(TYPE = SESSION)
(METHOD = BASIC)
)
)
)
-
jdbc:oracle:thin:[scott/tiger]@ldap://HostnameOrIP:PortNum/service_name,cn=OracleContext,dc=com
-
--using ldap
-
jdbc:oracle:thin:[scott/tiger]@ldaps://HostnameOrIP:PortNum/service_name,cn=OracleContext,dc=com
-
--using ldap+ssl
-
Example of Using tnsnames.ora:
-
-- jdbc using oci --
-
String url = "jdbc:oracle:oci:[scott/tiger]@mydb");
-
ods.setURL(url);
-
Connection conn = ods.getConnection();
-
-- jdbc using thin --
-
jdbc:oracle:thin:[scott/tiger]@mydb;
-
-
Jackson$ java -D oracle.net.tns_admin=$ORACLE_HOME/network/admin
-
Jackson$ cat $ORACLE_HOME/network/admin/tnsnames.ora
-
mydb =
-
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.11)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.10.10.12)(PORT = 1521))
(LOAD_BALANCE = no)
(FAILOVER = ON)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = oradb)
(FAILOVER_MODE =
(TYPE = SESSION)
(METHOD = BASIC)
)
)
)
-
Maybe You Will Need to Read This When You're Using Oracle 12c.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25521690/viewspace-2123201/
,如需转载,请注明出处,否则将追究法律责任。