Results 1 to 11 of 11
- 02-17-2012, 12:32 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Need help to connect my java application to sql server 2005
Dear all,
I am trying to connect my java application to sql server 2005
I am running the application on eclipse,and also I have added specified sqljdbc.jar file
but still it is giving this error : SQLException: No suitable driver found for jdbc
Here is my code. Can someone please help?
import java.sql.*;
public class conection
{
public static void main(String a[]) throws ClassNotFoundException, SQLException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:8080/DatabaseName=user" ,"ab","***");
Statement st=con.createStatement();
String sql="select * from user1";
ResultSet rs=st.executeQuery(sql);
while(rs.next())
{
System.out.println("Name: "+rs.getString(1));
System.out.println("Address : "+rs.getString(2));
}
if(st!=null)
st.close();
if(con!=null)
con.close();
}
catch(SQLException sqle)
{
System.out.println("Sql exception "+sqle);
}
}
// TODO Auto-generated method stub
}
Error:
Sql exception java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost:8080/DatabaseName=user
- 02-17-2012, 12:39 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Need help to connect my java application to sql server 2005
You should be loading the sqlserver driver, not the JDBC/ODBC bridge.Java Code:Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- 02-17-2012, 01:23 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Re: Need help to connect my java application to sql server 2005
@Tolls: Thank you for looking into it.
I tried to use sqlserver driver as suggested
Class.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver").
But now it is giving an error as :
java.lang.ClassNotFoundException: com.sqlserver.jdbc.SQLServerDriver
So any suggestions for this please?
- 02-17-2012, 01:37 PM #4
Re: Need help to connect my java application to sql server 2005
Is the class (or the jar containing it) on the classpath?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-17-2012, 02:01 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Need help to connect my java application to sql server 2005
And I take it one of those is a typo on your part, as the error does not match the code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").
java.lang.ClassNotFoundException: com.sqlserver.jdbc.SQLServerDriver
- 02-22-2012, 01:29 PM #6
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Re: Need help to connect my java application to sql server 2005
Thank you for your inputs, somehow it is now working fine, however I have a new issue now.
The code mentioned below works perfectly fine for the java page throwing no exceptions but for jsp it is throwing java.lang.ClassNotFoundException.
Any help/suggestions please?
<html><head></head> <body> <%@ page import="java.sql.*" %> <%try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver"); %> <% Connection conn = DriverManager.getConnection("jdbc:sqlserver://"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from IIP"); while(rs.next()) { System.out.println("ID:"+rs.getInt(1)); System.out.println("SEQNO:"+rs.getInt(2)); System.out.println("Client:"+rs.getString(3)); System.out.println("ClientError:"+rs.getString(4)) ; }} catch(Exception e) {System.out.println(e);} %> </body> </html> Output : java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
- 02-22-2012, 01:40 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Need help to connect my java application to sql server 2005
That code is unreadable.
My first suggestion is to use [code] tags [/code] and ensure the formatting is reasonable.
My second would be that the driver jar file is not in the WEB-INF/lib directory where it should be.
- 02-23-2012, 11:08 AM #8
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Re: Need help to connect my java application to sql server 2005
I tried your suggestion but it is still throwing the same error:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
Here is the code again:
<%@ page import="java.sql.*" %>
<%
try
{
System.out.println("In try");
Class.forName("com.microsoft.sqlserver.jdbc.SQLSer verDriver");
System.out.println("After Class.forname");
String url = "jdbc:sqlserver://localhost;databaseName=cab";
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT *FROM bm");
System.out.println("Connected");
while(rs.next())
{
System.out.println(rs.getString("CLIENT"));
}
}
catch(Exception e)
{
System.out.println(e);
}
//,"sa","SQL1423#3"
%>
output :
In try
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
Am I doing something wrong?
- 02-23-2012, 11:21 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Need help to connect my java application to sql server 2005
Yes.
Your driver jar is not in the WEB-INF/lib directory of your server.
If it's a Tomcat server that would be:
<tomcat-installation-dir>/webapps/<your-app>/WEB-INF/lib
A similar situation applies to other Java web-app servers.
- 02-23-2012, 11:42 AM #10
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Re: Need help to connect my java application to sql server 2005
Yes it worked ...thnx a ton :)
- 02-23-2012, 11:51 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Connect SQL Server 2005 Express with java.net.UnknownHostException
By karl cheng in forum JDBCReplies: 0Last Post: 09-20-2011, 09:29 AM -
Can't connect my connection string in MS SQL sERVER 2005
By ashin in forum SWT / JFaceReplies: 3Last Post: 12-13-2010, 01:42 PM -
Netbeans 6.5 unable to connect MS SQL Server 2005 Express
By dpk_vash in forum NetBeansReplies: 4Last Post: 09-07-2010, 06:51 AM -
how to connect sql server 2005 with java without going to odbc
By kiki2009 in forum AWT / SwingReplies: 3Last Post: 03-28-2010, 06:55 AM -
Connect to SQL server 2005
By comp in forum JDBCReplies: 1Last Post: 03-25-2009, 11:25 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks