Why getting error when connecting to remote mysql server? - java

I need to access database with remote mysql server, but when i try it, i get an error like this :
SQLException: Access denied for user 'u121252012_iya'#'114.79.63.156' (using password: YES)
SQLState: 28000
VendorError: 1045
and this is my connection code :
package Include;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* #author Ruslan
*/
public class Koneksi {
private static Connection koneksi;
public static void main (String []args){
Connection conn = null;
try {
conn =
DriverManager.getConnection("jdbc:mysql://my_ip_server/u121252012_coba?" +
"user=u121252012_iya&password=qwertyu");
// Do something with the Connection
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
}
please, i need your assistance to solve it.

CREATE USER '<username>'#'%' IDENTIFIED BY '<pwd>';
Here % indicates user is allowed access from all IP.
Also, in addition you might need to grant permissions to the user
GRANT ALL PRIVILEGES ON *.* TO '<username>'#'%' WITH GRANT OPTION;
And also, the connection mechanism should be like.
conn = DriverManager.getConnection(<DB_URL>,<USER>,<PASS>);
So change
conn =
DriverManager.getConnection("jdbc:mysql://my_ip_server/u121252012_coba?" +
"user=u121252012_iya&password=qwertyu");
To
conn =
DriverManager.getConnection("jdbc:mysql://my_ip_server/u121252012_coba?",
"u121252012_iya","qwertyu");

Related

How come I'm getting authenticate error, but when I login via the app, I am able to log in

com.microsoft.sqlserver.jdbc.SQLServerException: Failed to
authenticate the user fakeaccount#gmail.com in Active Directory
(Authentication=ActiveDirectoryPassword). at
com.microsoft.sqlserver.jdbc.SQLServerADAL4JUtils.getSqlFedAuthToken(SQLServerADAL4JUtils.java:62)
~[mssql-jdbc-8.4.1.jre8.jar:na] at
com.microsoft.sqlserver.jdbc.SQLServerConnection.getFedAuthToken(SQLServerConnection.java:4442)
~[mssql-jdbc-8.4.1.jre8.jar:na] at
com.microsoft.sqlserver.jdbc.SQLServerConnection.onFedAuthInfo(SQLServerConnection.java:4415)
~[mssql-jdbc-8.4.1.jre8.jar:na]
As Suggested by Jatin. You could refer to Azure Active Directory authentication document, where you could find different authentication methods and below is the sample example of connecting Azure Active Directory with MSI authentication method.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
public class AAD_MSI {
public static void main(String[] args) throws Exception {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setServerName("aad-managed-demo.database.windows.net"); // Replace with your server name
ds.setDatabaseName("demo"); // Replace with your database name
ds.setAuthentication("ActiveDirectoryMSI");
// Optional
ds.setMSIClientId("94de34e9-8e8c-470a-96df-08110924b814"); // Replace with Client ID of User-Assigned Managed Identity to be used
try (Connection connection = ds.getConnection();
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT SUSER_SNAME()")) {
if (rs.next()) {
System.out.println("You have successfully logged on as: " + rs.getString(1));
}
}
}
}

Connect to SQL server from Java

I'm trying to connect from SSMS(SQL Server Management Studio) to Eclipse(Java), but I keep getting this error message:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host localhost, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:6132)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:2609)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2346)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2213)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1276)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:861)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at net.codejava.sql.JavaConnect2SQL.main(JavaConnect2SQL.java:16)
The code I do have is:
import java.sql.DriverManager;
import java.sql.SQLException;
//import com.sun.jdi.connect.spi.Connection;
public class JavaConnect2SQL {
public static void main(String[] args) {
// TODO Auto-generated method stub
String url = "jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName=students"; // This is where I think the error is
String user = "sa";
String password = "123";
try {
java.sql.Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("Succesfully connected to Microsoft SQL Server");
}catch (SQLException e) {
System.out.println("Oops! There was an error: ");
e.printStackTrace();
}
}
}
I think the problem is in the connection URL, but I don't know the host name/instance name. If you can tell me how to get my host name/instance name, that would be appreciated. But if the problem is something else, please tell me!
Edit: I have changed the code, and I'm now getting a different. Here is the new code:
package net.codejava.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JavaConnect2SQL {
public static void main(String[] args) {
// Create a variable for the connection string.
String url = " jdbc:sqlserver://LAPTOP-5697KK36:1433;databaseName=students";
String user= "sa";
String password = "123";
try {
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println("Connection succesful!");
}
// Handle any errors that may have occurred.
catch (SQLException e) {
System.out.println("Here is your error message: ");
e.printStackTrace();
}
}
}
The new error message is this:
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://LAPTOP-5697KK36:1433;databaseName=students
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at net.codejava.sql.JavaConnect2SQL.main(JavaConnect2SQL.java:40)
I think the problem is that I installed the wrong version of JDBC Driver. My JRE version is 12.0.1. What is the correct JDBC Driver to install.
You can check in SQL server configuration manager and Microsoft SQL server management studio. The state must be running in server configuration manager and you can try to connect your database in Microsoft SQL server management with your password.
if you connect successfully then there is another problem but it looks like the database is stopped.

Unable to connect hive2 via JDBC java program

enter image description here
As I have import all required JAR file as shown in above image.
But still it gives me error while trying to connect hive2 via java program.
Error gives me on this line.
Connection connect = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", "");
Error:
18/05/30 17:12:15 INFO jdbc.Utils: Supplied authorities: localhost:10000
18/05/30 17:12:15 INFO jdbc.Utils: Resolved authority: localhost:10000
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.hadoop.hive.common.auth.HiveAuthUtils.getSocketTransport(Ljava/lang/String;II)Lorg/apache/thrift/transport/TTransport;
at org.apache.hive.jdbc.HiveConnection.createUnderlyingTransport(HiveConnection.java:519)
at org.apache.hive.jdbc.HiveConnection.createBinaryTransport(HiveConnection.java:539)
at org.apache.hive.jdbc.HiveConnection.openTransport(HiveConnection.java:309)
at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:196)
at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:107)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at Hive_java.main(Hive_java.java:15)
JAVA Code:
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class Hive_java{
private static String driver = "org.apache.hive.jdbc.HiveDriver"; //driver used for hiveserver2
public static void main(String[] args) throws SQLException {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Connection connect = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", ""); //connecting to default database
Statement state = connect.createStatement();
System.out.println("!!!!!!!!!!Running 1st query!!!!!!!!!!");
System.out.println("Listing tables in hive");
ResultSet show_tables = state.executeQuery("show tables");
while (show_tables.next()) {
System.out.println(show_tables.getString(1));
}
System.out.println("!!!!!!!!!!Running 2nd query!!!!!!!!!!");
System.out.println("Describing slave3_tbl table");
ResultSet describe_table = state.executeQuery("describe slave3_tbl");
while (describe_table.next()) {
System.out.println(describe_table.getString(1) + "\t" + describe_table.getString(2));
}
}
}

"invalid database address" for PostgreSQL in JDBC

I use PostgreSQL to create my database and save my users list to it, when i try to connect db by java jdbc, i get error that say:
"java.sql.SQLException: invalid database address:
jdbc:postgresql://localhost:5432/users".
i use "JDBC41 Postgresql Driver, Version 9.3-1102" from PostgreSQL website.
and this is my code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class javaconnect {
private static Connection c = null;
public static Connection connectDb() {
try {
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/users", "postgres", "12345");
return c;
} catch (ClassNotFoundException | SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
return null;
}
}
}
Thanks.
As the error,
"java.sql.SQLException: invalid database address:
says your database name is incorrect . check for the database name if you have something like sql developer installed.
Valid database name should be specified here "jdbc:postgresql://localhost:5432/users" after the /localhost:5432/
Read JDBC using postgresql to connect to PostgreSQL database using jdbc

Connection between Java and MYSQL server

I know that this issue has been discussed a lot of times but I cannot establish a connection between my Java app and MySQL server.
I'm using code form tutorials:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCTest{
private Connection conn = null;
public void connect(){
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("test");
conn = DriverManager.getConnection("jdbc:mysql://mysql.***.***.**:3306/******","*******","top_secret_password");
System.out.println("test2");
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " ex.getMessage());
System.out.println("SQLState: " ex.getSQLState());
System.out.println("VendorError: " ex.getErrorCode());
}catch(Exception e){e.printStackTrace();}
}
}
Error that occured is:
test
SQLException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
SQLState: 08S01
VendorError: 0
Have you got any ideas how fix it?
Try to provide the IP-address of the database instead of its name.
Say
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/*******","*******","top_secret_password");
and make sure that your firewall doesn' block 3306 port

Categories

Resources