Connect MSSQL DB as different Windows user in JAVA - java

I need to establish connection to MSSQL database from java using Windows Authentication as a different user
I getting SQL login failiure because windows authentication is using server username and password. I would like to change those.
this.connectionString = "jdbc:sqlserver://SERVER;DatabaseName=DATABASE;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
this.conn = DriverManager.getConnection(this.url);
this.statement = conn.createStatement();
I expect to log into remote database as a different windows user.
Many thanks in Advance

Related

I can connect to AWS MySQL RDS instance from workbench but not in my JDBC application

I am trying to create a Java application that connects to a MySQL database and I am using AWS to host it. So I created the AWS RDS instance and I got it to connect to the MySQL workbench just fine. My problem arises when I am trying to use JDBC to connect to it.
I have security groups that allow traffic from anywhere, but I also tried just allowing my IP.
I had it working when I was using a localhost but now I'm trying to move it to a server so keeping it localhost isn't an option (used localhost to test the application and such)
I made sure that my user was a remote user by doing
SELECT * from mysql.user;
And made sure that the host was a '%' and that it had all the privileges
So my code I'm trying to connect with in Java is
String connectionURL = "jdbc:mysql:/{hostname}:{port}/{database name}/?autoReconnect=true&useSSL=false";
String username = "username";
String password = "password";
con = DriverManager.getConnection(connectionURL, username, password);
Of course I have the actual host, port, and such in there just changed it for posting online.
When I try running it on Eclipse it says this java.lang.Exception: Database not found
I looked up some tutorials on AWS docs to make sure it lined up, which it all did.
Anyone have any idea why it might be connecting to MySQL workbench and not the JDBC?

Access denied when trying to connect to mysql database via eclipse, java

I'm trying to connect to mysql database, but I get one and the same error: javax.servlet.ServletException: java.sql.SQLException: Access denied for user 'user'#'localhost' (using password: YES)
I've already tried the following solutions:
Checked my username and password. MySQL connection is established via username = user1, password = 123. I use the same in my code, when I connect to the database: Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/employee","user1", "123");
Granted privileges to user1 in the following manner:
GRANT ALL PRIVILEGES ON * . * TO 'user1'#'localhost';
FLUSH PRIVILEGES;
By the way I have the same problem when I try to connect with 'root'#'localhost'.
Do you have any ideas why it doesn't work still?
MySQL JDBC is only able to connect via TCP/IP (on Unix, or TCP/IP or named pipe on Windows).
The MySQL JDBC driver cannot establish a connection using the local unix socket.
With MySQL on Unix, localhost has a different meaning that we might expect. It is not a hostname synonym for the 127.0.0.1 TCP/IP loopback address. On Unix, MySQL user
'user1'#'localhost'
specifies a user that can connect only via unix socket file; it's not possible to connect to that user via TCP/IP.
The above explains why MySQL is refusing a connection from JDBC: the connection attempt fails because a matching user does not exist.
To create a MySQL user that connect from TCP/IP loopback address, assuming that MySQL is started with --skip-name-resolve and without --skip-networking, we can specify user as:
'user'#'127.0.0.1'
That user would allow connection from JDBC.
(If MySQL DNS name resolution is enabled, then we would need to use a hostname that resolves to the loopback address; or we can consider using a wildcard '%' for the hostname portion of the user.)
Try this out might help:
1) goto mysql terminal. 2) mysql> use mysql; 3) mysql> select user, host from user; 4) There next to you user set the host as "%" instead of localhost
.
now in the connection instead of localhost tryp specifying the ip address of the server.
Hope this helps you. :)

Connecting to DB using JDBC driver with different user login

I have opened sql with other user (Shift+Right click and run as different user) by providing user name and password, hence when I connect to SQL Server, with windows authentication am able to access the DB's and tables.
Now, can I do this using Java? When I use below code:
conn = DriverManager.getConnection("jdbc:sqlserver://ServerName;Database=Testing;integratedSecurity=true");
Error:
Login failed for user so and so. This is actually correct because it is taking the login information of system I am currently using.
You can add to JDBC connection your new password and username
jdbc:sqlserver://localhost;user=MyUserName;password=*****;
So in your code:
DriverManager.getConnection("jdbc:sqlserver://ServerName;Database=Testing;integratedSecurity=true;user=MyUserName;password=*****");
Note
Although the previous example uses a username and password in the connection string, you should use integrated security as it is more secure. For more information, see the Connecting with Integrated Authentication section later in this topic.

How do I connect to a remote servers MySQL database using Java?

Currently I have written a program to populate a mysql database on my own computer and I connect to it using JDBC using:
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/data?useSSL=false", "root", "your_new_password"); // MySQL
// Step 2: Allocate a 'Statement' object in the Connection
Statement stmt = conn.createStatement();)
Now I need to populate the database of a remote server which I have been given a username and password to access. So I have the IP address of the the remote server, a username and password for the server. Once I have connected to the server, then I need to access the MySQL database on that server a root user and there is no password for root.
How do I connect to a remote MySQL server given the requirements above?
I have tried the following way:
Connection conn = DriverManager.getConnection(
"jdbc:mysql://10.XXX.XX.XX:3306/data?useSSL=false", "serverusername", "serverpassword"); // MySQL
But I get the error:
message from server: "Host 'Connection conn = DriverManager.getConnection(
"jdbc:mysql://10.143.38.63:3306/Adwhere2?useSSL=false", "serverusername", "serverpassword");' is not allowed to connect to this MariaDB server"
I have tried to connect to the server via ssh on the terminal, using the username and password and it successfully connected. So the username and password is not the problem.
I am able to access the server via ssh on the terminal using:
ssh serverusername#10.XXX.XX.XX , and then typing the password.
Once connected to the server I can connect the the mysql database using:
mysql -u root

Connect to SQL Server with JDBC

I have this program
SqlConnection myConnection = new SqlConnection("Data Source=IGOR-PC;Initial Catalog=Prueba;Integrated Security=True");
myConnection.Open();
And I connect to this SQL server IGOR-PC (SQL 10.50.4044 - Igor-PC\Igor)
My question is if I could connect to this server with JDBC with c#?
Can I connect to a SQL server with different ways or can only one way?
There are many ways to connect to SQL depending on what kind of security your SQL database have. You can use OLEDB or JDBC ODBC bridge. Weather your SQL server is installed as a named instance or not.
Checkout different ways to connect to SQL Server here : https://www.connectionstrings.com/sql-server-2008/
here is a code that allow you to connect sql server and java :
String url = "jdbc:sqlserver://IGOR-PC:1433;databaseName=Prueba;integratedSecurity=true";
Connection conn = DriverManager.getConnection(url);

Categories

Resources