import java.io.*;
import java.sql.*;
import java.net.*;

public class sqlDemo 
{
	int eno;
	ResultSet rs;
	ResultSetMetaData md;
	String /*insq,*/ selq;
	Statement stmt;
	
	public sqlDemo()
	{
		try
		{
			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
			Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver; DatabaseName= Admin.sdf", "sa", "deepa#247");
			
			//BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
			//System.out.println("Enter Employee id");
			//eno=Integer.parseInt(br.readLine());

			stmt=con.createStatement();
			selq="select * from emp";
			

			//insq="insert into emp values(?,?,?)";
			//int j=stmt.executeUpdate(insq);
			//System.out.println("No of rows inserted \t" +j);
			
			rs=stmt.executeQuery(selq);
			md=rs.getMetaData();
			int col=md.getColumnCount();
			
			while(rs.next())
			{
				for(int i=0; i <= col; i++)
				{
					System.out.print(rs.getString(i) + "\t");
					//rs.next();
				}
				System.out.println();
			}
		
			rs.close();
			stmt.close();
			con.close();
		}
		
		catch(Exception e)
		{
			System.out.println("Exception  \t" +e.getMessage());
		}
	}
	
	public static void main(String args[]) throws Exception
	{
		new sqlDemo();
	}
}

there are no errors in this program but wen i execute it an exception is raised at "com.microsoft.jdbc.sqlserver.SQLServerDriver"

Please help me.....

The problem is probably because it cannot find this class:
com.microsoft.jdbc.sqlserver.SQLServerDriver
when you call the Class.forName

Have you download and set to your classpath this driver: com.microsoft.jdbc.sqlserver.SQLServerDriver ?

no i have not set the class path for this driver.... but even if i use the access database an exception is raised as "[microsoft] Invalid descriptor exception"

Does this call executes correctly? Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); Try to run it separately. If you want to get the connection you will need the above driver. And in order for the the Class.forName to "find it" you need to tell your program where that class is. I assume you downloaded a jar file?
Then you should be able to find instructions on the net on how to set it to your classpath.

Are you using some kind of IDE?

no i dun have an IDE... this is a simple console application which i used to run at my college last year....at home i am getting such kind of exception

I have found this link:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2691&lngWId=2
I don't know if it is helpful, but from what I understood, if the code runs at some other computer then you need to transfer not only the code, but whatever other jar files or library it uses.
Usually for mySQL you download a jar file that has inside the driver like the one you have: com.microsoft.jdbc.sqlserver.SQLServerDriver So you might want to get from your college the jar file or in what other form they use.

I assume that you have set the database at your computer.


Another code I found is this:

private static final String accessDBURLPrefix = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="; 
private static final String accessDBURLSuffix = ";DriverID=22;READONLY=false}"; 

// Initialize the JdbcOdbc Bridge Driver 
static { 
  try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
  catch(ClassNotFoundException e) { 
    System.err.println("JdbcOdbc Bridge Driver not found!"); 
  } 
} 

/** Creates a Connection to a Access Database */ 

public static Connection getAccessDBConnection(String filename) throws SQLException { 
  filename = filename.replace('', '/').trim(); 
  String databaseURL = accessDBURLPrefix + filename + accessDBURLSuffix; 
  return DriverManager.getConnection(databaseURL, "", ""); 
}

It uses a different driver than your own:
"sun.jdbc.odbc.JdbcOdbcDriver"

If you search the net you will probably find both.

no i have not set the class path for this driver.... but even if i use the access database an exception is raised as "[microsoft] Invalid descriptor exception"

Well, that's a different exception, isn't it? What does the one have anything to do with the other?

You need to print more than just the message so that you actually know what is going on. I.E. your first error message was just a class name. If you don't know, beforehand it's a CNFE then all you can do is think, "oh, goody, what's this?". Whereas if you use printStackTrace() you see "ClassNotFoundException: <classname>" instead of just "<classname>". At least a bit more informative, don't you think. And the rest of it also tells you exactly where it happened. That is also nice to know, isn't it?

Well, that's a different exception, isn't it? What does the one have anything to do with the other?

You need to print more than just the message so that you actually know what is going on. I.E. your first error message was just a class name. If you don't know, beforehand it's a CNFE then all you can do is think, "oh, goody, what's this?". Whereas if you use printStackTrace() you see "ClassNotFoundException: <classname>" instead of just "<classname>". At least a bit more informative, don't you think. And the rest of it also tells you exactly where it happened. That is also nice to know, isn't it?

thanks,
as you said, i used the printStackTrace() method and came up with the following list of exceptions :

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid descriptor index
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLColAttributes(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getColAttribute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getColumnType(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getMaxCharLen(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
at sqlDemo1.<init>(sqlDemo1.java:40)
at sqlDemo1.main(sqlDemo1.java:59)

Can you post the part of the code where you get the exception?

sorry i am not getting it....when i execute the program ( coded with access database) i am getting these exceptions

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid descriptor index
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLColAttributes(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getColAttribute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getColumnType(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getMaxCharLen(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
at sqlDemo1.<init>(sqlDemo1.java:40)
at sqlDemo1.main(sqlDemo1.java:59)

i dun kno where the jar file is located...and i have ms sql server 2005 management studio...i am not able to undrstnd from where to download the jar file you are specifying.... :(
i have attached both the files one with ms access and other with sql server... plz chk it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.