Accessing MySQL database through JDBC with IP Address - java

I'm trying to connect MySQL by passing an IP address as a parameter instead of localhost.
Class.forName("com.mysql.jdbc.Driver");
try{
String url = "jdbc:mysql://192.168.1.116:3306/db";
DBConnection = DriverManager.getConnection(url, "root", "");
}
catch(Exception ex){ ex.printStackTrace(); }
The problem is the connection takes 1-2 minutes before showing this error.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
By the way, I'm using XAMPP.

Related

Using eclipse to call database from Google Cloud SQL

I m trying to create a dynamic website where I can get data from the database and output it to the user.
I created the connection between MySQL workbench to Google cloud app engine, and I run on the cloud shell to test the database, it works.
But when I try to change the URL on eclipse, it just cannot connect to the server, and gives me errors "Failed to load resource: the server responded with a status of 500 ()".
the URL sting are:
String url = "jdbc:mysql:///film?cloudSqlInstance="
+ "causal-relic-333309:europe-west6:jiayaodb="
+ "&socketFactory = com.google.cloud.sql.mysql.SocketFactory"
+ "&user=root"
+ "&password=password";
private Connection openConnection(){
// loading jdbc driver for mysql
try{
Class.forName("com.mysql.jdbc.Driver");
} catch(Exception e) { System.out.println(e); }
// connecting to database
try{
// connection string for demos database, username demos, password demos
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
} catch(SQLException se) { System.out.println(se); }
return conn;
}
So I think is the connection error where I can't get it to connect to Google Cloud SQL.
you need to use Cloud SQL proxy to connect to the database from your local machine.

Why does my SQL connection get stuck when I use DriverManager.getConnection, without any error?

I need to connect to my database from a standalone Java application.
I try with this code, but it gets stuck on the DriverManager.getConnection line.
try{
String url = "jdbc:mysql://192.168.2.11:1121/TEST";
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Before");
Connection conn = DriverManager.getConnection(url,"test","test");
System.out.println("After");
} catch (SQLException e ) {
System.err.println("Sql exception! ");
System.err.println(e.getMessage());
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
The output I get is the following:
Before
and nothing else.
This is the db configuration page in MySQL(obviously it works via MySQL)
I'd appreciate any help
Same old question's but without answer
It seems that you are trying to connect to an Oracle database with MySQL driver. According to your configuration, you should change the connection string from jdbc:mysql://192.168.2.11:1121/TEST to jdbc:oracle:thin:#192.168.2.11:1121:1521:xe and use the Oracle driver oracle.jdbc.driver.OracleDriver instead of com.mysql.jdbc.Driver. You can download it here.
As suggested from Nigel Ren, the error was that is it an Oracle db.
I solved editing the code as follow
String url = "jdbc:oracle:thin:#192.168.2.11:1511:XE";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(url,"test","test");

Connection to two different databases in single class

I need to connect to two different database from my code. First is Hive database and another one is Oracle. For this purpose I need to load two different drivers one for Hive and another for Oracle. Here is what I did:
Class.forName("org.apache.hive.jdbc.HiveDriver");
System.out.println("Driver Found");
Connection connection = DriverManager.getConnection(
"jdbc:hive2://10.8.219.36:10000/default", "lab", "lab");
System.out.println("Connection established");
System.out.println("------------------");
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver Found");
Connection connection1 = DriverManager.getConnection(
"jdbc:oracle:thin:#97.253.16.117:1521:NATTDB11",
"CDR_LOADER", "CDR_LOADER");
System.out.println("Connection established");
and when I am running the above code, this is what is got error:
Driver Found
Connection established
------------------
Driver Found
java.lang.IllegalArgumentException: Bad URL format
at org.apache.hive.jdbc.Utils.parseURL(Utils.java:185)
at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:84)
at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:104)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Demo.main(Demo.java:17)
How do I do this? Thank you in advance.
Try to explicitly register both drivers using:
DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
DriverManager.registerDriver (new org.apache.hive.jdbc.HiveDriver());
Then test both Connection types.

JDBC connection not happening [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 7 years ago.
Below is the code I used for jdbc connection
String dbUrl="jdbc:mysql://localhost:3306/mysql";
String user= "kumar";
String pwd="ratiol";
try (Connection connection = DriverManager.getConnection(dbUrl, user, pwd)) {
System.out.println("Database connected!");
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
but I got error as below-
Exception in thread "main" java.lang.IllegalStateException: Cannot connect the database!
at jdbcConnection.Jdbcdemo.main(Jdbcdemo.java:22)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mysql
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at jdbcConnection.Jdbcdemo.main(Jdbcdemo.java:19)
Can you please tell me how can I get jdbc url?
I am using eclipse(mars) in ubuntu 14.04
if you are using netbeans, right click project -->properties -->libraries-->add library and select MySQL JDBC Driver
just add the com.mysql.jdbc.Driver to the lib folder in you program files.
This is what you wrote
try (Connection connection = DriverManager.getConnection(dbUrl, user, pwd)) {
System.out.println("Database connected!");
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
it would be like this:
Connection con = null;
try {
//registering the jdbc driver here, your string to use
//here depends on what driver you are using.
Class.forName("something.jdbc.driver.YourFubarDriver");
Connection connection = DriverManager.getConnection(dbUrl, user, pwd)
} catch (SQLException e) {
throw new RuntimeException(e);
}
Please check Class.forName not more needed when using JDBC v.4
Let's take a quick look at how we can use this new feature to load a
JDBC driver manager. The following listing shows the sample code that
we typically use to load the JDBC driver. Let's assume that we need to
connect to an Apache Derby database, since we will be using this in
the sample application explained later in the article:
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection conn =
DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
But in JDBC 4.0, we don't need the Class.forName() line. We can
simply call getConnection() to get the database connection.
Note that this is for getting a database connection in stand-alone
mode. If you are using some type of database connection pool to manage
connections, then the code would be different.
There are plenty of reason for the exception No suitable driver found for jdbc:mysql like
Your JDBC URL can be wrong.
ClassPath/BuildPath/Lib folder missing the connector jar.
Driver Information is Wrong.
Just load the driver first:
Class.forName("com.mysql.jdbc.driver");

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.

Categories

Resources