Home > Java > Connect to sql server 2005 Express Edition by jdbc

Connect to sql server 2005 Express Edition by jdbc


To make a connection to sql server using jdbc driver

you need to download jdbc driver here

after download sqljdbc_3.0.1301.101_enu.exe, you will extract it into a folder.

Find in the folder file sqljdbc 4.jar (just find to be sure you download the right one).

Next, you create a new project in NetBean, i mean every type of project you want.

then, Right-click in Properties -> Add Jar Folder and find the sqljdbc4.jar you downloaded.

Now you had a configuration your IDE to make your connection.

Okie, now everything is ready for your connection function

Make a class like this:

Note: in this class i had a database named:

and i configued my SQL server TCP/IP port is 5530

import java.sql.*;
/**
*
* @author user
*/
public class Main {

/**
* @param args the command line arguments
*/
public static java.sql.Connection GetConnection()
{
Connection con=null;
try
{
String conStr="jdbc:sqlserver://localhost:5530;DatabaseName =[YourDatabaseName];";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection(conStr,"[YourUserID]","YourUserPassword");
System.out.println("Successful");
}
catch(SQLException e)
{
System.out.println("SQL Exception: "+ e.toString());
}
catch (ClassNotFoundException cE) {
System.out.println("Class Not Found Exception: "+ cE.toString());
}
return con;
}
public static void main(String[] args) {
// TODO code application logic here
Connection sqlConnection=null;
sqlConnection =GetConnection();
}
catch(SQLException e)
{
System.out.println("SQL Exception: "+ e.toString());
}

}
}

Now run it and you will get the successful result.

How about the way to configue your sql server TCP/IP, i will show it in the next Post. wait and see ok?

Good luck!!

prmhuitedu

Advertisements
Categories: Java Tags: ,
  1. Dao
    May 18, 2011 at 12:15 AM

    hihi,thanks

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: