Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i am trying Connect Java Application with Sql server database 2008(JDBC)
1) I created data base in sql server
SQL
create table Emp
(
Emp_id int not null primary key,
Emp_Name nvarchar(50),
Emp_Address nvarchar(50)
)
insert into Emp values(001,'xyz','BBSR')
insert into Emp values(002,'abc','CTC')

2)Taking one class and add the following code in eclipse IDE
Java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Demo {

	public static void main(String[] args) {
		try{
			Class.forName("com.microsoft.sqlserver.jdbc.SqlServerDriver");
Connection con=DriverManager.getConnection("jdbc:sqlserver://Servername;database=Employee;"+"IntegratedSecurity=true;");

		Statement stmt=con.createStatement();
		ResultSet rs=stmt.executeQuery("Select * from Emp");
		while(rs.next())
			System.out.println(rs.getInt(1)+ " "+rs.getString(2)+" "+rs.getString(3));
		con.close();		
	
			
		}
		catch(Exception e)
		{
			System.out.println(e);
		}

but when i run this program in Eclipse following error was found "java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SqlServerDriver. So how to solve this, Please help

Note: i added Jar "sqljdbc4-2.0.Jar" in build path under library tab in Eclipse IDE. and using SQl Server 2008 management studio Express for creating a database.
Posted
Updated 30-Jan-14 3:42am
v2

You're lucky, I got this link which explains the issues clearly with screenshots
Connecting JSP with SQL Server 2008 R2 Express (JDBC)[^]
 
Share this answer
 
Comments
reshma786 30-Jan-14 23:46pm    
Hi,
Thank you for your valuable solution.
Yes now the "java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SqlServerDriver" error was solved but now showing the error when i run the class "com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host DSILDT10146, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".Please suggest
reshma786 31-Jan-14 0:46am    
Hi,
I enabled TCP/IP in Sql Server Configuration Manager and please suggest what i have to change the Properties of TCP/IP
thatraja 31-Jan-14 1:49am    
Simple Google search could have helped you. Check this
TCP/IP port numbers required to communicate to SQL over a firewall
The link above was dead, the new one is [IT] Connecting JSP with SQL Server 2008 R2 Express (JDBC).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900