Identifying the right URL to access a database in Java - java

I'm wondering which would be the right path/url to access the "aliens" database I have in my MySQL Workbench.
As you can see, the connection name is "new connection_1", the user is "root", and the port is "3306".
Then, I think the database name is aliens.
Until now I've tried this in my eclipse IDE for java:
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/aliens","root", "myDatabase");
System.out.println(conn);
The url is now "jdbc:mysql://localhost:3306/aliens","root", "myDatabase", but i receive the following exception:
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/aliens
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at EdoardoDatabase.main(MainClass.java:9)
Moreover, I would like to print the "connection", nothing else.

YOu'd need a MySql Connector Jar in your classpath and and a Driver set before initiating the connection.
Class.forName("com.mysql.jdbc.Driver");
You can use maven or download the Driver from Mysql page https://www.mysql.com/products/connector/

your Connection String doesn't look bad at all,
your problem is that Java doesn't know to which driver it should map the mysql driver...
if you are sure you have the Mysql jdbc driver on the ClassPath try
Class.forName("com.mysql.jdbc.Driver")
before you do you getConnection()
this calls the static{} block in the Driver registers the mysql driver for JDBC Connection lookups via the Factory

Related

Problem in Oracle JDBC Connection Configuration/JDBC Request - JMETER

I'm a problem in JDBC Connection Configuration. When i execute my test (only JDBC Request - insert), doesn't appear any results in report (View Results Tree):
"Cannot create JDBC driver of class 'oracle.jdbc.OracleDriver' for connect URL 'jdbc:oracle:JDTST'"
The connection is Oracle. See below the configuration:
database URL: jdbc:oracle://${myURL}
JDBC driver Class: com.microsoft.sqlserver.jdbc.SQLServerDriver
Username: ${user}
Password: ${password}
Print below:
lister.ora file:
My login in Oracle DB:
Could someone help me?
Thanks!!!
I don't think your URL is correct, it should be something like:
jdbc:oracle:thin:#your-oracle-hostname-or-ip-address:your-oracle-port:your-oracle-SID
Correct JDBC Driver fully qualified name is oracle.jdbc.driver.OracleDriver
The "Validation query" should be select 1 from dual
You will need to download Oracle JDBC Driver and drop it to JMeter Classpath
More information:
OracleDriver Documentation
DBCP - validationQuery for different Databases
The Real Secret to Building a Database Test Plan With JMeter
Refer to JDBC Developer's guide on how the JDBC connection URL is formed.
The easiest way is to use the below URL. You can use DataSourceSample.java for checking the connection.
jdbc:oracle:thin:db_user/db_password#localhost:5221:orcl
Database SID is no longer supported; please use a service name as in Nirmala's response.

JMeter Couldnt Connect to Phoenix using JDBC Connection

I'm setting up a JMeter Connection to Phoenix using its JDBC Connection Configuration but it wont connect due to some jars and connection problem. I have tried configuring all of the compatible jars and configure the connection but it won't work either.
I've added Phoenix Jar to Classpath in Test Plan Setting.
The JDBC connection setting are as below:
Database Url: jdbc:phoenix://ip:port:/hbase-unsecure/schema_name
Driver Class: org.apache.phoenix.jdbc.PhoenixDriver
After configuring the phoenix database all i want to do is to do some simple query like SELECT * FROM table_name
Using MySQL Connection was successful and it shows me the data inside the database. However using this Phoenix Configuration, it won't even connect to the database and I am getting various error such as
Cannot create JDBC driver of class 'org.apache.phoenix.jdbc.PhoenixDriver' for connect URL
or
Couldn't established connection
or
java.lang.NoClassDefFoungError: Could not initialize class org.apache.phoenix.jdbc.PhoenixDriver
I got it resolved. turns out the driver for phoenix (phoenix-4.7.0.2.6.1.0-129-client) is suitable for JMeter version 2.9.
And in order the phoenix driver to work in JMeter, use phoenix-4.7.0.2.6.4.0-91-client, not phoenix-4.7.0.2.6.1.0-129-client.
and if you have service mapping enabled problem, extract your jar, find hbase-default file, and add another property for service mapping enabled = true.

How to connect to H2 database using jmeter?

I am using Jmeter with JDBC to connect to h2 database. I have created JDBC connection configuration in the Jmeter Test Plan and given variable name as 'mydb' and my JDBC configuration is as follows:
database connection URL: jdbc:h2:tcp://localhost/d:/h2/sample
Driver class: org.h2.Driver
Username: sa
I have created a JDBC request under the Thread Group and am using the same variable name 'mydb' for the jdbc request. The jmeter throws me an exception.. Cannot create PoolableConnectionFactory (Connection is broken: "unexpected status 16777216" [90067-176])
however, i am able to access the database on my browser.
I am using the below link as a reference for what i am doing:
https://www.coursehero.com/file/p6a7t19/1-Set-up-an-H2-database-Download-an-H2-database-distribution-from/
Any hints on what i am missing??

NetBeans with Connector/J

Well I'm working on a GNU/Linux machine and I'm still learning how to connect to my database
I've got the Connector/J File downloaded and edited my CLASSPATH, here's the reusult of echoing the path
/home/user/Connector-J/mysql-connector-java-5.1.30/mysql-connector-java-ver-bin.jar:
I also have my SQL File.. let's say: sqlfile.sql
Threw the documentation and while searching I found how I should connect to the Connector.. My question is: where should I put my SQL file?
Also, Here's a piece of code I found that is used to connect, is it right?
String userName = "root";
String password = "password";
String url = "jdbc:mysql://localhost/somefile";
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
To build an application that connects to a database, you will need:
A database engine (e.g. MySQL, SQL Server, Oracle, etc)
A JDBC driver library (e.g. Connector/J for MySQL, jTDS for SQL Server, ojdbc for Oracle, etc)
Java application itself.
Your code is only creating connection. Even that, the url looks incorrect. The "somefile" should be replaced with the name of database schema you want to connect to.
To interact with database, creating connection is still far from done.
To read, insert, update, delete, etc records from/to database, you need to create statement object by passing your SQL query.
The SQL queries should not be put in a file, but written in your code instead.
Complete JDBC tutorial is here: http://docs.oracle.com/javase/tutorial/jdbc/

Error while trying to connect to database through eclipse database development perspective

This is error stack trace i am getting...
Could not connect to mydb.
Error creating SQL Model Connection connection to mydb. (Error: oracle.jdbc.OracleDriver)
oracle.jdbc.OracleDriver
Error creating JDBC Connection connection to mydb. (Error: oracle.jdbc.OracleDriver)
oracle.jdbc.OracleDriver.
I am using oracle thin driver to connect to database..
SID: db
Host: localhost
Port number 1521
User name: system
Password:
Connection URL: jdbc:oraclethin:©lccalhost:1521:db
I can't even able to ping the database...
There is a typo in your connection URL. It says lccalhost instead of localhost. Try:
jdbc:oraclethin:©localhost:1521:db
Furthermore, your log says mydb instead of db as stated in your connection settings. So, this may be another possible cause of fault.
I think you are using xpress edition 11-g and if so then try changing the SID from db to xe and then it should work fine .
If you are using the standard edition then you must check with the odbc jars .
Try adding odbc6 jar

Categories