JavaEE MySQL connection: No suitable driver found - java

I want to establish a connection to my MySQL database from my Java EE web application so I can query it from it. I added mysql-connector-java-5.1.14-bin.jar to my WEB-INF/lib folder.
Now when I try to establish a connection it says:
No suitable driver found for jdbc:mysql://localhost?autoReconnect=true.
The lines in my code where the error occurs are:
m_url = "jdbc:mysql://" + mDatabaseHost +"?autoReconnect=true";
m_connection = DriverManager.getConnection(m_url, mDatabaseUser, mDatabasePassword);
User and password are correct. What might be wrong?
I tested this with Tomcat 7.0.

I think that you forgot to initialize driver.
You have to say something like the following: Class.forName("com.mysql.jdbc.Driver"); bofore connecting to DB.

Try to add the database name after localhost and ensure that you don't have skip-networking in the my.cnf file
UPDATE
Put the Connector Jar into $CATALINA_HOME/lib

Related

Mysql pool connection error with glassfish/payara server

Ping Connection Pool failed for MySQL Pool. Connection could not be allocated because: 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. Please check the server.log for more details.
In my case this problem got fixed by adding "useSSL" to the pool properties and setting it to false.
This solution works for payara 5 and Mysql 8
Select Mysql 8 as Database Driver Vendor
When you try create the connection pool remove all default propierties and write the properties manually as shown in the below image, something is wrong with default properties becuase the connection can't be reached so to avoid issues please remove them and try add them manually.
Note: don't forget add your mysql connector to
glassfish5\glassfish\lib
glassfish5\glassfish\domains\domain1\lib
If you are trying to connect to MySQL on cloud (Ex. Azure MySQL) make sure the SSL enforcement is off in addition to above and setting useSSL = false.
Azure MySQL
I just want to give solution here as I already read and view many solutions regarding this error but not anyone of them is good for me.
So please follow the step by step process:-
Download and Install Glassfish/Payara Server.
Download and Install latest mysql.
Install it as full and in mysql folder you can find an Connector j folder.
from there copy the executable jar file and paste it into two folders.
glassfish5\glassfish\lib
glassfish5\glassfish\domains\domain1\lib
Now restart the server from netbeans.
Open admin console and then goto JDBC and then select jdbc connection pool
Add particular property :-
Database name
Password
port :- 3306
ServerName :- localhost
Url :-jdbc:mysql://localhost:3306/test
User
6.Enable ping
save
Best of luck

Unable to connect Java application to SQL Server database

I am trying to write a Java desktop app that can connect to my database made with Microsoft SQL Server Manager to allow me to view and update it. But, I am having trouble getting the connection to work. I've read through a bunch of tutorials and threads here on Stack Exchange of similar problems, and I'm not sure what I'm doing wrong.
The server is called "SQLEXPRESS" using Windows authentication. I downloaded the JDBC driver found here: https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774 installed it in NetBeans by going to "Services-Databases(right click)-New Connection-Add", but I also added it as a library in my project.
When I try this code, I get the exception that the TCP/IP connection failed either because the server isn't running or port 1433 is locked:
try{
String
URL="jdbc:sqlserver://sqlexpress:1433;DatabaseName=GreenhouseManagement";
Connection conn = DriverManager.getConnection(URL,"","");
System.out.println("connected");
}catch(Exception e){
System.out.println("Oops\n"+e);
}
What do I need to change to fix this?
You might need to reconfigure your connection string into this format.
jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE
HOST in this case is most likely to be "localhost" since you are connecting on a local machine.
DATABASE will be the name of your database
Reference: http://alvinalexander.com/java/jdbc-connection-string-mysql-postgresql-sqlserver

Where exactly should JDBC URL value be changed?

I'm trying to run some application and get the following error:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for
JDBC]Can't start a cloned connection while in manual transaction mode.
I know that I should add the parameter ;SelectMethod=Cursor to your JDBC URL
But I'm having problem understanding where exactly should I change it? Should it be some conf file in JDBC driver folder somewhere? Or can I do it from sql management studio?
Also is there some easy way to determine if and what version of JDBC driver I have?
Help is very much appreciated!
You specify the URL when creating your JDBC connection, e.g.:
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]",
username,
password);
Of course you have to replace the stuff in the brackets with your values.
Quite the same is true for every other tool (e.g. IntelliJ, Eclipse) I know of that connects to a DB via JDBC. See e.g. attached screenshot. Here you also specify the connection parameters via the JDBC URL.

Java JDBC - How to connect to Oracle using tnsnames.ora

tnsnames.ora file contains the Databases and the their description (host + port).
Is it possible to establish a connection relying on the file mentioned above? (Say by providing only the DB name):
In order to find this file, I have to know the default oracle home I need to check in the windows registry for HKEY_LOCAL_MACHINE\Software\Oracle and then to have all the KEY_XXX files and then check which one appears first on the %PATH%. Is there a way to automatically find this file on the client computer?
I wasn't even aware that using tnsnames with the thin driver is possible, but apparently it was added somewhere in version 10:
http://docs.oracle.com/cd/B19306_01/java.102/b14355/urls.htm#BEIDIJCE
In particular:
Note:
When using TNSNames with the JDBC Thin driver, you must set the oracle.net.tns_admin property to the directory that contains your tnsnames.ora file.
java -Doracle.net.tns_admin=%ORACLE_HOME%\network\admin
As mentioned, I haven't checked if this actually works.
I don't think that the "find the actual network config directory" logic is available via some Oracle function. You'll have to do it manually as outlined in your question, or maybe rely on the TNS_ADMIN environment variable being present. In that case, the java invocation would be
java -Doracle.net.tns_admin=%TNS_ADMIN%
Well, in some GUIs the TNS driver configuration is simply not implemented or not working (NetBeans for example :-) )
https://netbeans.org/bugzilla/show_bug.cgi?id=231526
There is simple workaround here. You can take the entry directly from the tnsnames.ora file and attach it to the jdbc driver string as following:
Example from using odbc7.jar (Oracle 12c JDBC driver for JDK 7) to
connect to Oracle 11gR2 RAC cluster:
jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=
TCP)(HOST=hostA)(PORT=
1522))(ADDRESS=(PROTOCOL=TCP)(HOST=hostB)(PORT=1521)))(SOURCE_ROUTE=yes)(CONNECT_DATA=(SERVICE_NAME=DatabaseService)))
Be aware of putting double :: characters in the end as host:port:service, if you will put :: in the end like this:
jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=
TCP)(HOST=hostA)(PORT=
1522))(ADDRESS=(PROTOCOL=TCP)(HOST=hostB)(PORT=1521)))(SOURCE_ROUTE=yes)(CONNECT_DATA=(SERVICE_NAME=DatabaseService)))::
You will end up with "NL Exception was generated" exception.
Another approach is to configure following property:
System.setProperty("oracle.net.tns_admin","C:/app/product/11.2.0/client_1/NETWORK/ADMIN");
Of course, instead of hardcoded value, you can for example set up environment variable in your operating system like ORACLE_TNS_ADMIN and then reference it:
System.setProperty("oracle.net.tns_admin",System.getenv("ORACLE_TNS_ADMIN"));
or pass it to java process via -D switch
on linux:
-Doracle.net.tns_admin=$ORACLE_TNS_ADMIN
and windows:as
-Doracle.net.tns_admin=%ORACLE_TNS_ADMIN%
Once our application is aware of TNS config file, we can connect by reference service name in TNSNAMES.ora file as in this full example:
// tell the driver where to look for the TNSNAMES.ORA file
System.setProperty(
"oracle.net.tns_admin",
"C:/app/product/11.2.0/client_1/NETWORK/ADMIN");
// ORCL is net service name from the TNSNAMES.ORA file
String dbURL = "jdbc:oracle:thin:#ORCL";
// load the driver
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = null;
Statement stmt = null;
try {
conn = DriverManager.getConnection(dbURL,
"your_username",
"your_password");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT dummy FROM dual");
Starting from 18.3, TNS_ADMIN that provides the location of the tnsnames.ora file can be passed as part of the connection URL. Please note the syntax.
jdbc:oracle:thin:#jdbctest_medium?TNS_ADMIN=/test/cloud/network
Firstly make sure that SQL Developer software is properly installed in your machine. If you are using thin driver, ensure that your ojdbcX.jar file is in your build path. The steps to connect to Oracle data source using TNS Alias name are:
Set System Property for oracle.net.tns_admin. This should point to the directory which has your tnsnames.ORA file
System.setProperty("oracle.net.tns_admin", DIRECTORY_PATH_TO_TNSNAME.ORA_FILE);
Register an Oracle driver
DriverManager.registerDriver(new OracleDriver());
Create a connection object
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:username/password#TNS_ALIAS_NAME");
This should establish the Database connection.
You can also try the following
Try this, after some hours of troubleshooting came across a sample which I modified and it works like a gem.
jdbc:oracle:thin:#(description=(address_list=(address=(protocol=tcp)(port=1521)(host=19.16.200.12)) (address=(protocol=tcp)(port=1521)(host=19.16.200.10)))(load_balance = yes)(connect_data=(SERVICE_NAME=stackdb)))
A none load balance sample is given Below:
jdbc:oracle:thin:#(description=(address_list=(address=(protocol=tcp)
(port=1521)(host=prodHost)))(connect_data=(INSTANCE_NAME=ORCL)))
Here is the URL that helped https://docs.oracle.com/cd/E11882_01/java.112/e16548/jdbcthin.htm#JJDBC28202

Java ODBC and Microsoft.Jet.OLEDB.4.0

I want to access a database using a connection string that is given by a third party application. I have one example configuration that has a connection string like the following:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\path\to\theDatabase.mdb;Persist Security Info=False
Calling
DriverManager.getConnection("jdbc:odbc:" + connectionString);
gives me an SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
The third party application can access the database without problems.
The OS is Windows XP Service Pack 3 and up to date.
The msjet40.dll in system32 folder has version 4.0.9511.0 (up to date according to http://support.microsoft.com/kb/239114/en-us)
The file exists and I can access it using jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};C:\path\to\theDatabase.mdb
I just don't know what I'm doing wrong.
Problem is in your odbc connection
To connect access database try following
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:connSource");
goto ControlPanel->AdministrativeTools->DataSource(ODBC)->System DSN->ADD->MicrosoftAccess->
then in the name field give the Source Name as connSource.
you have to use this name instead of database name in your DriverManager.getConnection method.
Because getConnectionMethod take the source name not the database name. so your code is not working.
This might be a problem. I don't know of any JDBC drivers for OLE DB data sources. Here on SO this questions sits without answers from March: https://stackoverflow.com/questions/5184046/jdbc-oledb-bin .
Refer to the wesite below it contains connection strings of all variants for all the databases
http://www.connectionstrings.com/

Categories

Resources