I am trying to use below code for java to sqlserver connectivity..

import java.sql.*;
import java.util.*;
import java.io.*;
import java.lang.*;
public class AuthorsInfo1
{
	public static void main(String args[])
	{
		try
		{
			String str = "select * from authors where au_city like 's%'";
			String str1 = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
			Class.forName(str1);
			Connection con = DriverManager.getConnection("jdbc:sqlserver://sqlserver01;databaseName = java;user = sneha;password = sneha_2608");
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(str);
			System.out.println("Author ID\tAuthor Name\tCity");
			while (rs.next())
			{
				String id = rs.getString("au_id");
				String name = rs.getString("au_name");
				String city = rs.getString("au_city");
				System.out.print(id + "\t");
				if (name.length() <= 7)
				{
					System.out.println(name + "\t\t");
				}
				else
				{
					System.out.println("\t" + name + "\t");
				}
				System.out.println(city);
			}
			con.close();
		}
		catch (SQLException e)
		{
			System.out.println("Error Occured");
			System.out.println("Error:" + e);
		}
	}
}

but it give me this error:
AuthorsInfo1.java:13: unreported exception java.lang.ClassNotFoundException; mus
t be caught or declared to be thrown
Class.forName(str1);

please give me solution for it.....

Recommended Answers

All 10 Replies

Sorry, if you don't, yet know how to handle exceptions you should not be attempting JDBC, yet. See this.

i know how to handle exception ok....

i used try and catch block also for that but they have prob with com.microsoft.sqlserver.....sentence...

according to error they dont get this class........


now tell me the solution.

@sneha_mehta
Your code handle only SQLException which is not complete. You still need to handle ClassNotFoundException which is the exception you got from your code...

but dear if i handle this exception then also no matters now it will give me run time error.....

please, just once click the link posted by masijade

i know how to handle exception ok....

Seemingly not if you are asking how to solve that compiler message.

but dear if i handle this exception then also no matters now it will give me run time error.....

So what, you still need to add the catch block (or the throws clause) or your program won't even compile and that will make it a bit hard to run. If you then get a runtime exception, then you either typed the class/package name wrong, or you do not have the JDBC driver on your classpath (you know about that, don't you?).

As masijade said the error has nothing to do with database.

You call a method that throws an exception. Regardless if that method will ever throw that exception at Runtime or not, according to the declaration of that method you must catch the exception that it throws.

The above has nothing to do with adding the sql driver at the classpath or not.

So what, you still need to add the catch block (or the throws clause) or your program won't even compile and that will make it a bit hard to run. If you then get a runtime exception, then you either typed the class/package name wrong, or you do not have the JDBC driver on your classpath (you know about that, don't you?).

Which driver i have to install for it...??


i think driver type 4....i have installed sqljdbc_1.0 (enu)....then i got jar file now what to do with this jar file means where should i paste that jar file.?? please reply...

Uhm, the JDBC Driver for the DB you're using? And, yes, the type 4 driver is best.

As far as where to put it, if you are using an IDE then see it's documentation, if not then use the -cp command line option, or properly configure the class-path attirbute in the manifest file of the jarfile if using the -jar option.

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.