SQL Server: connect to database using NTLM authentication using Java 8 - java

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";

Related

Java connect with JDBC to Postgres - SQLException

Iam tryig to connect to a Postgres Database. Iam really new to that and have read a post in the forum. But I didn't manage it.
public void connect() {
//Connection con = null;
try {
Class.forName("org.postgresql.Driver");
Properties props = new Properties();
props.setProperty("user", user);
props.setProperty("password", password);
props.setProperty("ssl","true");
Connection conn = DriverManager.getConnection(url, props);
//String url = "jdbc:postgresql://localhost/test?user=fred&password=secret&ssl=true";
//Connection conn = DriverManager.getConnection(url);
System.out.println("Erfolgreich verbunden!");
}
catch (Exception e){
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
}
EDIT:
I updated my Code.
The database is deployed to heroku.
It throws the error:
java.sql.SQLException: No suitable driver found for jdbc:postgres://vuqmbekwlgohkw:******
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:189)
at com.company.Database.connect(Database.java:20) p
at com.company.Main.start(Main.java:16)
at com.company.Main.main(Main.java:25)
java.sql.SQLException: No suitable driver found for jdbc:postgres://vuqmbekwlgohkw:***************
I believe that your problem is that your connection URL is malformed.
When DriverManager.getConnection throws SQLException, the message includes the exact url value passed to the function. In your case, that looks like jdbc:postgres://vuqmbekwlgohkw:******.
But obviously that is not the URL you are using. You have replaced part of the URL with asterisks. That suggests that you think the format of the URL is:
jdbc:postgres://username:password#host:port/dbname
which seems to be what Heroku provides in the DATABASE_URL environment variable. You are using asterisks to prevent us from seeing the password.
However, it looks like the PostgreSQL JDBC driver does not accept URLs in this format. When I tried, I also got the "No suitable driver" error. According to the documentation, the format of the URL is:
jdbc:postgres://host:port/database
Some parts are optional, but the driver does not appear to support putting the user name or password in the URL.
I was able to connect to an AWS PostgreSQL instance by using the URL format described in the documentation and using the connection properties to set the user name and password.
You said you are new so I'm gonna start with the painfully obvious, do you have the driver on your classpath?
it's a jar you add to your project that you can download from https://jdbc.postgresql.org/
Otherwise I have an example of the url that worked for me, except I was not exactly using jdbc, it was a spring-boot project:
jdbc:postgresql://ec2-174-99-88-88.compute-1.amazonaws.com:5432/asdfasdfsadf?sslmode=require
There is no port number in your commented URL and not all parameters might be supported, like Willis pointed out.

Connect Java remotely to MYSQL DB on another network

I want to write a java application that uses a mysql database to store and retrieve information. I am still just a beginner and I do not have a lot of knowledge on web hosting providers and server architecture. In this application, several clients will have to access this remote database located on a server machine maybe.
Currently, I can connect to the database from the same computer that runs the mysql server (I am using workbench by the way). Any suggestions on what should I do?
public static Connection openDatabase()
{
Connection myConn;
try {
myConn =
DriverManager.getConnection(Configs.getProperty("DatabaseURL"));
return myConn;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
The url is:
jdbc:mysql://Atomic-PC:3306/test?autoReconnect=true&rewriteBatchedStatements=true&useSSL=false&user=root&password=password
First of all you have to find connection URL, port and credential from MySql workbench you are using. If hosting provider provides a public IP for DB server you should be able to access it from anywhere by using following code in Java.
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static void main(String[] argv) throws Exception {
String driver = "com.mysql.jdbc.Driver";
String connection = "jdbc:mysql://localhost:3306/YourDBName";
String user = "root";
String password = "root";
Class.forName(driver);
Connection con = DriverManager.getConnection(connection, user, password);
if (!con.isClosed()) {
con.close();
}
}
}
For this reason you have to import mysql connection driver to your application first.
If you are trying to connect to remote database then yo need to change the database url from localhost to remote server ip address.
jdbc:mysql://Atomic-PC:3306/test
to
jdbc:mysql://<db-server-ip-address>:<db-server-port>/<db-name>
Assuming, remote server ip address is 10.234.05.123 and database port number is 3300 and database name is remoteDB. Then,
jdbc:mysql://10.234.05.123:3300/remoteDB
your web hosting company should be able to provide you with the url:port which you can use in your connection string to connect to MySQL(or any other remote db for that matter)

Can't Connect To Remote Oracle Database (11g)

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).

Receiving SQLException "Login failed for user" connecting to SQL Server 2008

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.

Having trouble connecting to mySQL server on linux server from netbeans/java

I am unable to connect to a MySQL server which is hosted on a linux server through netbeans.
All of these credentials work when connecting through MySQL Workbench "Standard TCP/IP through ssh".
Here is my code:
public class Database {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://john.myschool.edu:3306/cs3610";
private static final String USERNAME = "mbrooke";
private static final String PASSWORD = "mypass";
private Connection connection;
public Database() throws Exception{
try{
connect();
}catch(SQLException e){
if(connection !=null){
connection.close();
}
}
}
//Open connection to database
private void connect() throws Exception{
connection = null;
Class.forName (DRIVER).newInstance ();
connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
}
}
I am getting SQLException with #521 on the line that starts "connection = DriverManager..." and I'm not sure what is causing this problem. The driver seems to be installed correctly as, when stepping through, I make it past the "Class.forName(D..." line with no exceptions thrown.
It sounds like your Database server doesn't have port 3306 open, or your MySQL credentials aren't allowed to use remote connections.
MySQL Workbench's TCP/IP over SSH setting first opens an SSH connection to the SSH server, then connects to the database server (often localhost or 127.0.0.1). So the MySQL connection is actually initiated from the SSH server. So the ability to connect through that channel only demonstrates that your java code would work if it were running on the server into which you're SSHing. But you may still have a firewall or MySQL permissions issue when trying to run the code from another machine.
I would try downloading a MySQL client to your machine and seeing if you can connect using that method: mysql -h myDatabaseServer.school.edu cs3610 -u mbrooke -p'mypass' and see if that works. You'll likely either get a "connection not available" error or a "user mbrooke doesn't have permission to access remotely" which should give you some insight into which problem you're facing.
Try it without ending slash
URL = "jdbc:mysql://john.myschool.edu:3306/cs3610/";
like
URL = "jdbc:mysql://john.myschool.edu:3306/cs3610";
or you have a Database named "cs3610/"

Categories

Resources