Reputation: 53
Here is my code
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost/employee","root","root");
When we connect with Oracle Driver we specify Driver Type, that is (type-1,type-2,type-3,type-4")
-
say
Class.forName("jdbc.oracle.driver.JdbcOracleDriver");
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:XE","System","System");
Why don't we do this for a MySQL Database?
Upvotes: 1
Views: 134
Reputation: 3649
Using jdbc, we do not specify the driver type in code. We specify the driver class and the connection string. By observing these two, you may (or may not) infer the type. In your example, for both mysql and oracle, you are using their Type-4 drivers.
When you see some connection string or driver name containing odbc
then you can be pretty sure that this is type-1 driver. You can use odbc for almost all databases. In your code the sql will be fairly neutral towards db provider. But this is the slowest way.
Type-2 drivers are rare. Mysql does not have one. Oracle has a OCI
driver which you can use in place of thin. If you use oci driver, you have to use a different connection string, a different driver name, different jar, and some extra installation/configuration on your system. These drivers provide some extra features not commonly available on oracle thin, ex. Clustering. Some databases like SQLite has only T-2 driver, nothing else.
I have not encountered any Type-3 driver so i cannot speak intelligently on them.
Type-4 is the most common way to connect to databases. Both of your examples are of the same type. These drivers require minimum installation/configuration on your system, are able to provide common sql subset (like T1) and db specific features (like T2). Being pure java, you can be quite sure that that your code will work fine across diffetent os (_un_like T2).
As you can see, there is no simple way to infer the type of a driver. Most of the times it does not even matter. Trend is away from odbc and towards T4. Some high perf things are still being done in T2 but it is rare.
Hope this helps
Upvotes: 1