This question already has answers here:
What is the purpose of 'Class.forName("MY_JDBC_DRIVER")'?
(4 answers)
Closed 6 years ago.
Somebody can explain to me for what this link: org.apache.derby.jdbc.ClientDriver is necessary.
for example:
public class Demo1 {
public static void main(String[] args) {
String driverName = "org.apache.derby.jdbc.ClientDriver";
try {
// loaded the driver
Class.forName(driverName);
System.out.println("driver loaded");
String url = "jdbc:derby://localhost:1527/db1";
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
It isn't necessary, and hasn't been since Java 6. The JDBC 4.0-only features says (in part)
Autoloading of JDBC drivers. In earlier versions of JDBC, applications had to manually register drivers before requesting Connections. With JDBC 4.0, applications no longer need to issue a Class.forName() on the driver name; instead, the DriverManager will find an appropriate JDBC driver when the application requests a Connection.
In earlier versions of Java, it was required to load (and register) the JDBC driver.
Related
This question already has answers here:
Where should the JDBC driver JAR files reside on a Tomcat deployment with a datasource?
(2 answers)
Closed 7 years ago.
So i followed all the steps listed in other posts to add mysql-connector-java-5.1.36-bin to my class path in intellij 12 and when i'm writing code intellij clearly sees it and lets me import it, but when I run the application inside of tomcat I get a ClassNotFoundException
Below are screen shots of my set ups and my code.
public void runLookUp(String s){
String url = "jdbc:mysql://localhost:3316/test";
String username = "java";
String password = "password";
System.out.println("Connecting database...");
System.out.println("Loading driver...");
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded!");
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Cannot find the driver in the classpath!", e);
}
try {
Connection connection = (Connection) DriverManager.getConnection(url, username, password);
System.out.println("Database connected!");
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
}
I am aware that doing it in global libraries instead of my lib directory is bad form I did it originally there and moved it to global as an attempted fix to this issue and just haven't moved it back yet. Given that this seems to work for everyone else I assume its something in the way I have my tomcat deploying from intellij, any thoughts?
This has nothing at all to do with IntelliJ.
You need to put JDBC driver JARs in the Tomcat /lib folder, not in any project WEB-INF/lib.
Your code as written is still far from optimal:
Hard coded URL, driver class, and credentials make it difficult to change.
Username and password in plain text.
No JNDI lookup.
No connection pooling.
This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 7 years ago.
So I was testing out MySQL databases for the first time, (For the following code, all I want to do is establish a connection to the data base):
import java.sql.*;
public class Driver {
public static void main(String[] args) {
Connection con = null;
try{
String url = "jdbc:mysql://localhost:3306/movie";
String user = "root";
String pw = "RockoAndLuke739969";
con = DriverManager.getConnection(url, user, pw);
}
catch(SQLException e){
e.printStackTrace();
}
}
}
And here is the Exception:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/movie
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Driver.main(Driver.java:13)
And I don't know why it isn't working.... thanks for taking your time to read :)
(I am new to stackoverflow by the way, so sorry if I screwed something up xD)
You need to add the driver in your classpath.
If you are using maven you have to add the following dependency
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
If you aren't using maven check your classpath manually and add the driver to it.
In addition add
Class.forName("com.mysql.jdbc.Driver").newInstance();
as first line of your connection code. This line is needed to load the class driver and is used by DriverManager to know wich driver must be used.
Here the reference documentation link
In java I am trying to connect with Sybase database through java program as shown below
public static void connect() {
SybDriver sybDriver = null;
Connection conn;
try {
sybDriver = (SybDriver) Class.forName(
// "com.sybase.jdbc3.jdbc.SybDriver").newInstance();
"com.sybase.jdbc2.jdbc.SybDriver").newInstance();
System.out.println("Driver Loaded");
conn = DriverManager.getConnection(url, username, password);
boolean isTrue = conn.isValid(3);
System.out.println(isTrue);
But i am getting the below exception
Driver Loaded
Exception in thread "main" java.lang.AbstractMethodError: com.sybase.jdbc2.jdbc.SybConnection.isValid(I)Z
at connectionTry.connect(connectionTry.java:97)
at connectionTry.main(connectionTry.java:23)
I have done analysis in google what i have to came up to know jconnn.jar is missing as the issue is the method isValid(I)Z is not there in jconn2.jar is not there please advise how to overcome from this error please.
The driver you are using is - based on the class name in the stacktrace - a JDBC 2 driver. The isValid method was added in Java 6 (or: JDBC 4), so you can't use it with a driver that doesn't implement it.
You either need to upgrade to a newer driver: contact Sybase for that, or simply not call the isValid method. In the code you show there is no reason to call it: you just created the connection, of course it is valid. This method is intended to check the validity of long-living connections (eg in the context of a connection pool).
AbstractMethodError is thrown when the implementation class doesn't comply to the signature defined in the abstract class. Use a compatible version of the implementation or alternatively avoid the methods that conflict.
Ideally latest version of drivers must be having matching implementations or you will have to contact sybase to fix that.
I'm writing the below codes for connection between the java and Oracle 10g XE using 3 way(OCI, THIN and data source), the code is running successfully but don't know difference between the THIN and OCI with data source connection.
1-
public static void main (String args[]) throws SQLException
{
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:hr/hr#localhost:1521/XE");
Connection con = ods.getConnection();
System.out.println("Connected");
con.close();
}
2-
public static void main(String args[])
{
try
{
// load oracle driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// connect using Thin driver
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","hr","hr");
System.out.println("Connected Successfully To Oracle");
con.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
3-
public static void main(String args[])
{
try
{
// load oracle driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// connect using Native-API (OCI) driver
Connection con = DriverManager.getConnection("jdbc:oracle:oci:#","hr","hr" );
System.out.println("Connected Successfully To Oracle using OCI driver");
con.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
Oracle provides four types of drivers for their database, but I'll only enumerate the two you asked about.
The OCI driver is a type 2 JDBC driver and uses native code to connect to the database. Thus, it is only an option on platforms that have native Oracle drivers available and it is not a "pure" Java implementation.
Oracle's JDBC Thin driver is a type 4 JDBC Driver that uses Java sockets to connect directly to Oracle. It implements Oracle's SQL*Net TCP/IP protocol directly. Because it is 100% Java, it is platform independent and can also run from an Applet. (not that you should)
Both the JDBC thin driver and the JDBC OCI driver speak the same network protocol. From the server standpoint there is no difference between the two. The JDBC thin driver is 100% Java and comes in a single standalone jar (some extra jars will be needed for advanced features). The JDBC OCI driver makes JNI calls to the OCI C client library and hence depends on the Oracle full client to be installed (OCI is also what sqlplus uses). Oracle recommends to use the JDBC thin driver which is what most customers use. It's the fastest driver and the most robust one.
This question already has answers here:
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 7 years ago.
When trying to connect to mysql I always get this error:
java.sql.SQLException: No suitable driver found for localhost test
I already included the mysql-connector.jar in the /WEB-INF/lib in my app. What else do I need to configure to make it work? Do I need to add something in web.xml? I'm not using the appengine.
Here is my code in the server:
package com.mysql.server;
import java.sql.Connection;
import java.sql.DriverManager;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.mysql.client.ConnDb;
public class ConnDbImpl extends RemoteServiceServlet implements ConnDb {
public Connection con;
#Override
public String tryConn() {
try{
String host = "localhost";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "pwd";
Class.forName(driver).newInstance();
con = DriverManager.getConnection(host+db, user, pass);
return "Connected to Database";
} catch(Exception ex) {
return ex.toString();
}
}
}
You will get this exception when the JDBC URL is not accepted by any of the loaded JDBC drivers as per the Driver#acceptsURL() method. You actually forgot the JDBC driver specific URI prefix. For the MySQL JDBC driver this is jdbc:mysql://. The full connection URL should look like this:
con = DriverManager.getConnection("jdbc:mysql://localhost/test", user, pass);
See also:
Connector/J documentation - Obtaining a connection
I found another cause for this error message. In my case the user simply had no privilege to the database e.g. to the selected table. Dear driver developers, why do you use such misleading error messages? A lot of people have real trouble with this.
For me, it was forgetting to include the MySQLJDBC Driver in the project libraries. DOH!
This was giving that error:
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/lib_db","root","root");
but when I changed that to:
Connection connection =DriverManager.getConnection("jdbc:mysql://localhost/db_name?"+"user=root&password=root");
error was gone