I am trying to access MS SQL server 2005 from a servlet file. I am using JDBC 4.0 driver.
I have already added the JAR files sqljdbc.jar
and sqljdbc4.jar
files to my Tomcat /lib
folder.
But while running code I am getting an error
HTTP Status 500 - 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.
How is this caused and how can I solve it?
My code is:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = conn = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=school;user=sa;password=123");
PrintWriter pwOut = res.getWriter();
pwOut.println("Connected");
Statement st = conn.createStatement();
String searchCriteria = req.getParameter("txtSearch");
ResultSet rs = st.executeQuery("select * from student");
res.setContentType("text/html");
The error message is pretty clear. Tomcat is using the wrong driver.
You state that you copied sqljdbc.jar
and sqljdbc4.jar
into the Tomcat lib folder. That is most probably the reason for your problem.
You only need sqljdbc4.jar
otherwise Tomcat picks up the wrong one.
Try to delete sqljdbc.jar
from the Tomcat lib folder
Here is my Code to connect java to Microsoft sql Server 2012
You only need sqljdbc4.jar that avail on offical Microsoft website. Here is the link:
http://download.microsoft.com/download/0/2/A/02AAE597-3865-456C-AE7F-613F99F850A8/sqljdbc_4.0.2206.100_enu.exe
It contains 2 jar files, and I am trying to use sqljdbc4.jar. This is the code I am using to connect:
package com.Sql.ConnectDB;
import java.sql.*;
public class DbClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
**String url="jdbc:sqlserver://localhost;databaseName=Student";**//important
String user="username";
String pass="password";
**Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");**//important
Connection con=DriverManager.getConnection(url,user,pass);
System.out.println("Conneccted Successfully");
}catch(Exception e){
e.printStackTrace();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With