Connecting to remote SQL Server using SQL Driver in Java - java

Key notes:
Using SQL Server 2005
Using JDK 1.8
JDK 1.8 DOES NOT SUPPORT JDBC-ODBC Bridge ANYMORE
I have both Microsoft SQLJDBC 4.0 and 6.2 in my library
I am trying to connect to a remote SQL Server in Java for quite some time now and still have not been able to. I am hoping to connect using my DSN that I have created. The new driver JDK 1.8 uses needs a connection URL which I have created below. Everything in the code below looks correct. But I am still getting the error:
SEVERE: Java Runtime Environment (JRE) version 1.8 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
java.lang.UnsupportedOperationException: Java Runtime Environment (JRE) version 1.8 is not supported by this driver.
Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
Here is my code:
String connectionUrl = "jdbc:sqlserver://InterfaceDSN" +
"databaseName=DB_Local;Trusted_Connection=True;integratedSecurity=true";
// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
String SQL = "SELECT TOP 10 * FROM Person.Contact";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()) {
System.out.println(rs.getString(4) + " " + rs.getString(6));
}
}catch (Exception e)
{
e.printStackTrace();
}
To conclude:
Knowing what I have and what I need, is there another way to connect to the database, I am running out of options here considering I am using JDK 1.8 with SQL Server 2005.

What is happening
As the message says:
java.lang.UnsupportedOperationException: Java Runtime Environment (JRE) version 1.8 is not supported by this driver.
Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
This is because JDBC4.0 has a lot of changes, meaning you need to use a newer version of the driver. You cannot run the old driver on the new version.
How to fix it
Using just 6.2 or version 6.2.2.jre8 should fix the issue.
Extra info
You shouldn't need the below code after JDBC 4.0 (Java 1.6+)
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Starting with the Microsoft JDBC Driver 4.2 for SQL Server, Sun Java
SE Development Kit (JDK) 8.0 and Java
You can read it here where you can find the requirements for the JDBC Driver.
So, you have to update your JDBC driver to v 4.2 at least. This is clear enough.

Related

Java connect to a remote MS SQL server database

I am trying to connect to the database on the remote MS SQL server, using the provided connection string. The connection string is as follows:
data source=qsss.gar.de\SQL2012,3000;initial catalog=City;persist security info=True;user id=usr;password=usr##2009;
I understand I should have the suitable drivers, so I have downloaded the SQL Server drivers from microsoft.com/download/en/details.aspx?id=21599
, and added them to the build path of my project. Those are sqljdbc.jar and sqljdbc4.jar.
I am just using this test code to see if the connection is established.
public static void main(String[] args) throws Exception {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection connection = DriverManager.getConnection(
"qsss.gar.de\SQL2012,3000;initial catalog=City;persist security info=True;user id=usr;password=usr##2009;");
System.out.println("Connected!");
}
Doing so, I get an error: Java Runtime Environment (JRE) version 1.8 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
What am I doing wrong here?
Do not add both jars to your classpath since they both contain the com.microsoft.sqlserver.jdbc.SQLServerDriver-class and in your situation the program loads the one from sqljdbc.jar. Remove sqljdbc.jar from your classpath and only add sqljdbc4.jar so your program will always load the correct driver-version.

java connect to backend explanation

I am super newer in java language. Now i am getting started java.
I am keen interst know about how to connect java with back end.
For that I have been installed oracle 11g express edition and sql developer.
First of all i just confused here, is these two tools are (oracle 11g express edition and sql developer.) enough for connect java with back end.?
And i am using jdk1.8.0_25 and eclipse editor.
If yes, means, what are the further process to achieve?
Already i know little bit about php, i did something connecting database, retrive the data from database..
So what is my goal is, what is the best way to connect backend with java?
Can anyone java expert explain step step by process?
Step 1 : Download Oracle JDBC Drivers
You can download Oracle JDBC drivers from here. Choose the version appropriate for your database version. In this example, I use the Oracle 11g JDBC driver since I connect to Oracle 11g database. There are two versions available for Oracle 11g, ojdbc5.jar (for JDK 1.5) and ojdbc6.jar (for JDK 1.6). Use appropriate version for your Java installation (Oracle now requires online registration for downloading drivers). I use ojdbc6.jar for this tutorial.
Step 2 : Java Program to Connect to Oracle
The following Java program uses Oracle JDBC driver to connect to a running Oracle database instance. You can use this program on any Oracle database as this example uses Oracle’s built-in dummy table DUAL for fetching system date. DUAL enables us to get values such as system date using a normal SQL query.
// Example Java Program - Oracle Database Connectivity
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class OracleSample {
public static final String DBURL = "jdbc:oracle:thin:#localhost:1521:XE";
public static final String DBUSER = "system";
public static final String DBPASS = "manager";
public static void main(String[] args) throws SQLException {
// Load Oracle JDBC Driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to Oracle Database
Connection con = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
Statement statement = con.createStatement();
// Execute a SELECT query on Oracle Dummy DUAL Table. Useful for retrieving system values
// Enables us to retrieve values as if querying from a table
ResultSet rs = statement.executeQuery("SELECT SYSDATE FROM DUAL");
if (rs.next()) {
Date currentDate = rs.getDate(1); // get first column returned
System.out.println("Current Date from Oracle is : "+currentDate);
}
rs.close();
statement.close();
con.close();
}
}
Before you run the program ensure that you change the values for DBURL, DBUSER and DBPASS. DBURL is of the form,
jdbc:oracle:thin:#machinename:1521:databasename
Replace machinename with the name of the machine where oracle is running and replace databasename with service name of the database instance.
See this page for more details on JDBC API.
Step 3 : Add ojdbc.jar to Classpath
In order to compile or run the above program, you need to add ojdbc.jar to the classpath of your program. If you are using IDE such as NetBeans or Eclipse, you can add ojdbc.jar as a dependent library and NetBeans will automatically add it to classpath.
If you are running the above program from command line, copy ojdbc.jar to the folder where the above Java program is located and then compile the file using the following command (this adds ojdbc.jar to classpath),
javac -classpath ./ojdbc6.jar OracleSample.java
Run the Java program using the following command (ojdbc.jar is added to classpath),
java -classpath "./ojdbc6.jar;." OracleSample
Note that when you are running OracleSample, you need both the JDCB jar file and the current folder in the classpath.

eclipse says it's using JRE 1.7 even though it's using 1.6

I'm trying to build a simple jdbc sql example. it's 1 class. I have both jdbc driver jars referenced it's runtime is JavaSE-1.6. It connects to a database I already have setup using this code...
// Load the JDBC driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Create a connection to the database
connection = DriverManager
.getConnection("jdbc:sqlserver://localhost:1433;database=AboardTestDB;integratedSecurity=true");
System.out.println("Connected DB successfully");
// STEP 4: Execute a query
// writes the tables in local and not on GANESHN1\SQLSERVER2008
System.out.println("Creating table in given database...");
Statement stmt = connection.createStatement();
And the thing is it worked before. It worked beautifully. But a colleague of mine was having trouble with it. so I removed a jar to get the same error as him. but when I put it back it gave me this error message (again)
Dec 20, 2013 9:50:53 AM com.microsoft.sqlserver.jdbc.SQLServerConnection
SEVERE: Java Runtime Environment (JRE) version 1.7 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
Exception in thread "main" java.lang.UnsupportedOperationException: Java Runtime Environment (JRE) version 1.7 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
at com.microsoft.sqlserver.jdbc.SQLServerConnection.(SQLServerConnection.java:304)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1011)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at sqldriver.doConnection(sqldriver.java:23)
at sqldriver.main(sqldriver.java:59)
Everything else has stayed the same. I tried restarting, unstalling/reinstalling, creating a new project, trying different JRE's nothing.
Is there something I'm missing here? something else I can try?
never mind! I got it working again by removing one of the jars. The thing is, I had them both in there and it was working just fine before :P
Mysterious

Create Connection to .accdb Access File

I've been on this class for a few days now searching the web trying to find a solution. What I am trying to do here is connect to a Access 2010 database with an extension .accdb I have been successful connecting to older databases with extensions of .mdb but not .accdb
I have tried uninstalling Office and re-installing it for x64 versions and then installing the Access x64 tools. The error I received when I use the below code is as follows:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
At this point I have no idea what could be causing this. To point out, I need to have this setup with no DSN specified because this may run on multiple machines and I do not want to have to setup and maintain a DSN on each.
String database = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Users\\Brandon\\Dropbox\\Work\\Angent Profiles\\Database1.accdb;";
Connection conn = null;
try {
conn = DriverManager.getConnection(database, "", "");
} catch (SQLException SQLE) {
System.out.println("ERROR: " + SQLE);
}
Any advice on this would be grateful.
EDIT:
C:\Windows\system32>java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
C:\Windows\system32>
EDIT:
Provider=Microsoft.ACE.OLEDB.15.0;Data Source=C:\Users\Brandon\Dropbox\Work\Angent Profiles\Database1.accdb;Persist Security Info=False
String database = "jdbc:odbc:DRIVER={Microsoft.ACE.OLEDB.15.0 (*.mdb, *.accdb)};Data Source=C:\\Users\\Brandon\\Dropbox\\Work\\Angent Profiles\\Database1.accdb;";
I believe you need to run a 32 bit JRE to connect to access databases. From my understanding there is currently no 64 bit access ODBC driver available.

Which TYPE of driver it is? if i write

Which TYPE of driver it is? (Type 1,2,3,4) if i write -
Connection con = null;
Statement stmt = null;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/");
stmt =con.CreateStatement();
}
catch(Exception e)
{
e.printstacktrace();
}
And how to recognize various *TYPE of drivers if code snippet is written?
Yes it's MySQL Database driver!
But, I mean how to recognize the TYPE in JDBC? like we have-
Type1:JDBC-ODBC bridge.
Type2:Native-API/Partially Java Driver.
Type3:Net-Protocol/ All- Java driver.
Type4:Native-Protocol/All- Java driver.*
According to the documentation, MySQL's Connector/J driver is a JDBC Type-4 Driver:
MySQL Connector/J is a JDBC Type 4 driver. Different versions are available that are compatible with the JDBC 3.0 and JDBC 4.0 specifications. The Type 4 designation means that the driver is a pure Java implementation of the MySQL protocol and does not rely on the MySQL client libraries.
You know that it's Connector/J because the reference manual also states that
The name of the class that implements java.sql.Driver in MySQL Connector/J is com.mysql.jdbc.Driver.
Edit: (in response to the edit of the question) The only way to "recognize" the driver type is to read the documentation that came with the driver.
Check the manifest file inside the driver jar. It will have content like this :-
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_30-b03 (Sun Microsystems Inc.)
Implementation-Vendor: Oracle Corporation
Implementation-Title: JDBC
Implementation-Version: 11.2.0.3.0
Repository-Id: JAVAVM_11.2.0.3.0_LINUX_110823
Specification-Vendor: Sun Microsystems Inc.
Specification-Title: JDBC
Specification-Version: 4.0
Main-Class: oracle.jdbc.OracleDriver
Well since the driver says "mysql" in it, it's the driver for MySQL. You can find out more about how to use this driver from this link: http://dev.mysql.com/doc/refman/5.1/en/connector-j.html
This is an older looking piece of code, however I think the answer you're looking for is MySQL. It is a MySQL Database driver.
http://dev.mysql.com/downloads/connector/j/
Hope that helps!

Categories