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.
Related
Hy!
I really need your help. I'm trying to connect a database to a java program. This is my first time, so I have no idea what i'm doing.
My sql server: Sql server
I want to connect the Test database to the java program.
I tried the following code:
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql:WIN2CNG9\\SQLEXPRESS:3306/Test", "win2cng9\\kmim1437", "");
conn.close();
The error:
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql:WIN2CNG9\SQLEXPRESS:3306/Test
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at javaapplication42.JavaApplication42.main(JavaApplication42.java:24)
Java Result: 1
What is my mistake?
Thank you for any help.
Class.forName("oracle.jdbc.driver.OracleDriver");
DriverManager.getConnection("jdbc:mysql:
Why your use Oracle driver with mysql database? Use following code:
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql:WIN2CNG9\\SQLEXPRESS:3306/Test", "win2cng9\\kmim1437", "");
conn.close();
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");
I am completely new to java, and I want to make a connection to a remote SQL server 2008 R2 database (like 192.168.17.11) and load data from it.
Please suggest alternative ways if you know any.
First you must have JDBC driver for MS SQL, you have two jars (jtds-1.2.5.jar or sqljdbc4-2.0.jar), which added to your classpath
Second you need to create your connection as below:
String password="pass";
String driver= "net.sourceforge.jtds.jdbc.Driver"; // For sqljdbc4, use: com.microsoft.sqlserver.jdbc.SQLServerDriver
String username="user";
String URL="jdbc:jtds:sqlserver://serverIP:port/dbname"; // For sqljdbc4, use: jdbc:sqlserver://serverIP:port;databaseName=dbname
Class.forName(driver);
Connection conn = DriverManager.getConnection(URL, username, password);
// Use your connection here
// Don't forget to close the connection
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.
I am getting this exception
No suitable driver found for
jdbc:derby://localhost:1527/Test [saad on SAAD]
Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/Test [saad on SAAD]");
Before this
Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/Test [saad on SAAD]", dbusername, dbpassword); // default username = "root" , default password = ""
You have to write this
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
But first of all, you must import the driver jar for apache derby