I'm trying to connect a linux machine which has oracle database 11gr2 setup inside. There is no problem with connecting PL/SQL developer with any user. Unfortunately, with my simple java application, it's impossible to connect to database.
Here is my java code:
JAVA CODE
package oraConn;
import java.sql.*;
public class OraConn {
public static void main(String[] args) {
Connection connection=null;
try {
String driverName= "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
String serverName="192.168.2.122";
String portNumber="1521";
String sid="sas";
String url="jdbc:oracle:thin:#" + serverName + ":" + portNumber +":"+sid;
String userName = "system";
String password = "welcome";
connection = DriverManager.getConnection(url,userName,password);
} catch (Exception e) {
e.printStackTrace();
}
}
}
When i run this codeblock, i get an exception like this:
java.sql.SQLException: IO Error: Connection refused
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:489)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:553)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:254)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:528)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at oraConn.OraConn.main(OraConn.java:16)
Caused by: oracle.net.ns.NetException: Connection refused
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:399)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1140)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:340)
... 7 more
I am sure that port 1521 is open because i can use PL/SQL developer. What should i do?
Start your program in a debugger and copy the value of url. If you cannot connect with that URL using PL/SQL developer, something with your setting up the url is wrong. Change your program to get the same URL as in your working PL/SQL developer connection, and you should be done.
(This might sound simple, but it worked for me last time I had this problem).
Related
I am trying to connect to SQL Server using my credentials.
The data I am provided is to connect is the following:
Server: Ccddb294\oss_prod
Database: OSS_DW
Code:
public static void main(String arg[]) throws ClassNotFoundException, SQLException {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String dbURL = "jdbc:sqlserver://ccddb294.corp.corpcom.com:1433;databaseName=OSS_DW;integratedSecurity=true";
Connection conn = DriverManager.getConnection(dbURL,"corp\\e21290","Anjali#1234");
if (conn != null) {
System.out.println("Connected");
}
}
I am not sure where to give oss_prod in server name. When I try to connect, I get this error:
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: Integrated authentication failed. ClientConnectionId:26ddec01-2e7e-46c3-8165-4f3646da5e7c
Can someone validate if the dbURL which I created is correct as per specification or do I need to add odd_prod - but if so, where? (Note: The dll file is correctly placed in bin and i am able to connect to server at least but not able to authenticate only)
After lots of hit and trials.
Following is correct db URL:
"jdbc:sqlserver://ccddb294.corp.corpcom.com:1433;
instanceName=oss_prod;
databaseName=OSS_DW;
integratedSecurity=true;
domain=corp;
authenticationscheme=NTLM;
user=e21290;
password=Anjali#1234";
I'm trying to connect to a local sql server using Java and Microsoft SQL Server.
I'm using sql server auth.
Here's the code:
import java.sql.*;
public class Main {
public static void main(String[] args) {
String userName ="testlogin2";
String password ="pass";
String url ="jdbc:sqlserver://localhost/LabNoteMap;integratedSecurity=true;"; //LabNoteMap beeing target db
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url, userName, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("end");
}
}
and the stacktrace:
"C:\Program Files\Java\jdk-9\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\lib\idea_rt.jar=63510:C:\Program Files\JetBrains\IntelliJ IDEA 2017.2.5\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Deus\IdeaProjects\testMYSQL\out\production\testMYSQL;C:\Users\Deus\Desktop\jdbc\sqljdbc_6.0\enu\jre8\sqljdbc42.jar Main
end
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost/LabNoteMap, port 1433 has failed. Error: "localhost/LabNoteMap. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:242)
at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2369)
at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:551)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1963)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:1628)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1459)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:773)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1168)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:678)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
at Main.main(Main.java:13)
Process finished with exit code 0
also some ss:
Also note that in Sql server configuration, the tcp/ip is enabled and the port is set to 1433.
I guess could be a firewall? but not sure how to check for it, or maybe something else?
Your connection string is wrong. You should change it
String url ="jdbc:sqlserver://localhost/LabNoteMap;integratedSecurity=true;";
to
String url ="jdbc:sqlserver://localhost;databaseName=LabNoteMap;integratedSecurity=true;";
You are not targeting the db which you want to establish connection.
private static String url="jdbc:sqlserver://localhost:1433;DatabaseName = students";
I'm using msbase.jar and mssqlserver.jar and msutil.jar, maybe you are using the wrong jar or your url is wrong (DatabaseName = ?)
I have a really simple piece of JAVA code, where I try to connect from JAVA to my Oracle DB.
Everything works under Windows, but when I try to run it on Ubuntu, I have got an error.
I read a lot and I have tried lot of solutions. Here's my code:
package utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class OracleJDBC {
private static String driver = "oracle.jdbc.driver.OracleDriver";
private static String password = "*****";
private static String dbname = "XE";
private static String username = "userir";
public Connection getConnection() {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521:" + dbname, username,
password);
} catch (SQLException e) {
System.out.println("Connection Failed");
e.printStackTrace();
return null;
}
return connection;
}
public void closeConnection(Connection connection) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
when I run it, I receive an error:
Connection Failed java.sql.SQLRecoverableException: IO Error: The
Network Adapter could not establish the connection at
oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:458) at
oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:546)
at oracle.jdbc.driver.T4CConnection.(T4CConnection.java:236)
at
oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521) at
java.sql.DriverManager.getConnection(DriverManager.java:571) at
java.sql.DriverManager.getConnection(DriverManager.java:215) at
utils.OracleJDBC.getConnection(OracleJDBC.java:26) at
utils.TestMain.main(TestMain.java:6) Caused by:
oracle.net.ns.NetException: The Network Adapter could not establish
the connection at
oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:392) at
oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:434)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:687)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:247) at
oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1102) at
oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:320) ... 8
more Caused by: java.net.ConnectException: Connection refused at
java.net.PlainSocketImpl.socketConnect(Native Method) at
java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at
java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at
java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at
java.net.Socket.connect(Socket.java:579) at
oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:150) at
oracle.net.nt.ConnOption.connect(ConnOption.java:133) at
oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:370) ... 13 more
I logged in: sqlplus sys as sysdba
I started db: startup
I logged into Oracle as userir. XE exists.
I have no idea, what I did wrong...
Thank you in advance for your hints!
Verify your connection string.
I've experienced similar issues with various databases because I forgot to update localhost with the proper hostname.
connection = DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521:" + dbname, username,
password);
Ensure that the connection string matches what you're specifying in sqlplus and you should be good.
Finding the connection string
Determining the connection string can be difficult, but being that you can connect via sqlplus it should be a bit easier:
My SqlPlus connection string looks like: sqlplus user/pass#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(Host=hostname.network)(Port=1521))(CONNECT_DATA=(SID=remote_SID)))
If I bring this into Java it now looks like:
"jdbc:oracle:thin:user/pass#//hostname.network:1521/remote_SID";
Alternatively, if you connect via SQLDeveloper the string is made up on the connection properties:
now the hard part is if you truly are localhost and need to figure out the connection string from there.
First, try your IP address instead of localhost (In my screenshot, the 192.168.0.106) to make a string like:
"jdbc:oracle:thin:user/pass#//192.168.0.106:1521/remote_SID";
If that doesn't work, check your firewall, ensure that port 1521 is open (assuming you did not change the port setting, alter text accordingly if you did)
If still no resolution you will need to check out your listeners file. I'm going to link to some documentation if that's the case and kindly defer to some of the more senior Oracle/Linux guru's
http://docs.oracle.com/html/B10812_06/chapter5.htm - For 10g listener.ora information
http://docs.oracle.com/cd/E11882_01/network.112/e10835/listener.htm#NETRF008 - For 11g listener.ora information
http://docs.oracle.com/cd/E16655_01/network.121/e17610/listenercfg.htm#NETAG010 - For 12c listener.ora information
I'm guessing is that you've got some firewall or some network thingy blocking you from getting from your Linux box to your oracle server. Note the "connection refused" portion of the stacktrace..
...
java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) at
...
Oracle listens on port 1521, no? Maybe shell on the linux box and see if you can just telnet to the oracle host of 1521
telnet localhost 1521
Did you get connection refused there as well? I'm guessing that the Linux box doesn't have port 1521 open for whatever reason (firewall or the oracle listener isn't running).
Also, are you sure its localhost that you want to connect to when you deploy to Linux or is the expectation that the Oracle server is on a different machine? If the Oracle server is on a different machine, that machine's name needs to be in the connection string and not "localhost".
Welcome to the worst part of JDBC; figuring out the freaking connection string.
The issue is with secure random gathering.
As per this SO Discussion, Oracle JDBC Driver tries to get a secure random from the OS that it uses as part of connection establishment with Oracle. The issue is that your OS (Ubuntu) doesn't have enough randoms to give back and blocks you till it secure your request (the request of Oracle JDBC Driver).
A solution would be to ask Oracle JDBC Driver to avoid blocking until getting the complete requested random and use whatever it found. To do so, you have to use -Djava.security.egd=file:///dev/urandom.
More details on the SO Discussion mentioned above.
I am trying to connect to msq sql server 2005 using jdbc.Sql server 2005 is installed in local system and I am trying in the following way.But it fails if i write the ip address of the local system instead of localhost.What would be reason.
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class mssql {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection connection=DriverManager.getConnection("jdbc:sqlserver://192.168.1.207:1433;databaseName=WindProfiles;integratedSecurity=true;");// If i write localhost instead of 192.168.1.207 then works else shows error
if(!(connection==null))
{
System.out.println("connected");
}
//
} catch (Exception e) {
e.printStackTrace();
}
}
}
error
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. The user is not associated with a trusted SQL Server connection. ClientConnectionId:932514a0-5e3d-4d9c-8080-1e83ec703f9f
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:2234)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:2220)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1326)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at mssql.main(mssql.java:14)
I know instead of local system i can write 127.0.0.1 but my idea is to connect to remote system also.
When i write in command prompt telnet 192.168.1.207 1433 then it connects.Even it connects from remote system also.
You are missing username and password of your database so you are getting this error. use following to solve your problem:
Connection connection = DriverManager.getConnection("jdbc:sqlserver://192.168.1.207:1433;databaseName=WindProfiles","username","password");
Updated
If you wish to connect using windows authentication see following link: ( But I do not have experience using windows authentication mode from java)
Connecting to SQL Server from Java
There are a couple of ways I know of to do integrated security to MSSQL, the connection string option you are using is unfamiliar to me (may be valid for java, i don't know). Here are the options I know of to use trusted connection:
Trusted_Connection=True
Integrated Security=SSPI
Try replacing the integratedSecurity=true portion of your connection string with one of those and see if it works.
Edit
Can you try adding another catch with this code:
catch (SQLException se) {
do {
System.out.println("SQL STATE: " + se.getSQLState());
System.out.println("ERROR CODE: " + se.getErrorCode());
System.out.println("MESSAGE: " + se.getMessage());
System.out.println();
se = se.getNextException();
} while (se != null);
}
You can try following the suggestions here also, which is what I'm looking at:
http://msdn.microsoft.com/en-us/library/ms378522.aspx
I am trying to connect to SQL Server 2008 via Java.
I've added sqljdbc4.jar to my project's library.
No username and password is set for database accessing the database (Windows Authentication).
The 1433 port is Listening, but I still receive this exception:
SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user ''. ClientConnectionId:085d5df3-ad69-49e1-ba32-b2b990c16a69
Relevant code:
public class DataBases
{
private Connection link;
private java.sql.Statement stmt;
public ResultSet rs;
public DataBases()
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=DB;";
Connection con = DriverManager.getConnection(connectionUrl);
}
catch (SQLException e)
{
System.out.println("SQL Exception: "+ e.toString());
}
catch (ClassNotFoundException cE)
{
System.out.println("Class Not Found Exception: "+ cE.toString());
}
}
}
If you want windows authentication you need to add the option integratedSecurity=true to your JDBC URL:
jdbc:sqlserver://localhost:1433;databaseName=DB;integratedSecurity=true
You also need sqljdbc_auth.dll (beware of 32/64 bit) in your Windows system path or in a directory defined through java.library.path
For details see the driver's manual: http://msdn.microsoft.com/en-us/library/ms378428.aspx#Connectingintegrated
I was having same issue when I tried to connect to Microsoft SQL server from Java. I used jTDS driver instead of regular SQLJdbdc Driver.
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String connectionUrl = "jdbc:jtds:sqlserver://localhost:1433;databaseName=DB;integratedSecurity=true";
Connection con = DriverManager.getConnection(connectionUrl);
I had the same issue. Its because of the wrong format of the ConnectionUrl. You are missing username and password in the ConnectionUrl.
String ConnectionUrl = "jdbc:sqlserver://localhost:1433;databaseName=DB","username","password");"
I hope it works well!!!
This post my help:
jdbc.SQLServerException: Login failed for user for any user
You need the integratedSecurity=true flag and make sure the server properties are indeed set to 'Windows Authentication Mode' under Security.