问题
I am trying to connect to Oracle database using TNS.
The problem is that TNS alias contains dot, so when I am specifying url like this:
jdbc:oracle:thin:@TNS.ALIAS
I've got...
oracle.net.ns.NetException: Invalid connection string format, a valid format is: "host:port:sid"
...during creation of connection.
I know that dot character is a problem, because after removing it from tnsnames.ora
file connection to database works.
My question is - is it possible to escape dot character somehow? Maybe there is some connection parameter that can be setup to allow dot character in alias? I would like to avoid removing dot from tnsnames.ora
since i am getting the file from outside source.
Here are the options that I've already tried that gave me the same error:
jdbc:oracle:thin:@"TNS.ALIAS"
jdbc:oracle:thin:@\"TNS.ALIAS\"
jdbc:oracle:thin:@`TNS.ALIAS`
jdbc:oracle:thin:@TNS\.ALIAS - this one is not compiling
jdbc:oracle:thin:@TNS\\.ALIAS
jdbc:oracle:thin:@TNS.ALIAS
jdbc:oracle:thin:@TNS\".\"ALIAS
jdbc:oracle:thin:@TNS%2eALIAS
Here are the options that resulted with oracle.net.ns.NetException: could not resolve the connect identifier
:
jdbc:oracle:thin:@TNSALIAS
jdbc:oracle:thin:@TNS-ALIAS
jdbc:oracle:thin:@TNS_ALIAS
Additional context:
- I am trying to create Java's
DataSource
(OracleDataSource
to be strict) in Scala (it is Play Framework - but I am not using Play's way of creation of DB connections - I am doing it manually) - I have SQL Developer that is using exactly the same
tnsnames.ora
file and it is working there - We are having C# applications that are using exactly the same
tnsnames.ora
file and it is working there (data source is defined like this:<add name="connectionName" connectionString="Data Source=TNS.ALIAS;"/>
回答1:
You need set
System.setProperty("oracle.net.tns_admin","C:\\app\\product\\12.2.0\\client_1\\network\\admin");
set to the location of the tnsnames.ora
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnect {
public Connection connection;
public DBConnect() {
}
public void connect() throws Exception {
String connectString;
System.setProperty("oracle.net.tns_admin", "C:\\app\\product\\12.2.0\\client_1\\network\\admin");
Class.forName("oracle.jdbc.driver.OracleDriver");
connectString = "jdbc:oracle:thin:@esmdj.test";
System.out.println("Before DriverManager.getConnection");
try {
connection = DriverManager.getConnection(connectString, "scott", "tiger");
System.out.println("Connection established");
connection.setAutoCommit(false);
} catch (Exception e) {
System.out.println("Exception inside connect(): " + e);
e.printStackTrace();
}
}
public static void main(String[] args) {
DBConnect client = new DBConnect();
System.out.println("beginning");
try {
client.connect();
System.out.println("after Connected");
client.connection.close();
System.out.println("after close");
} catch (Exception e) {
try {
System.out.println("Exception : " + e);
client.connection.close();
e.printStackTrace();
} catch (Exception ex) {
System.out.println("Close Connection Exception : " + ex);
ex.printStackTrace();
}
}
}
}
C:\Program Files\Java\jdk1.8.0_73\bin>java -classpath . DBConnect
beginning
Before DriverManager.getConnection
Connection established
after Connected
after close
tnsnames.ora
esmdj.test =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = xxx.xxx.yyy.zzz)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = ESMD)
)
)
回答2:
I've found the problem - I was using older Oracle driver (ojdbc7.jar
for version 12.1.0.1
) after change to newer one (ojdbc8.jar
for version 12.2.0.1
) lookup by TNS alias started to work - there was no need to escape anything
来源:https://stackoverflow.com/questions/57341480/invalid-connection-string-format-error-when-trying-to-connect-to-oracle-using