How to make a connection with a sqlServer database by using Java?

To do this, the simplest way is to use a JDBC driver :

  1. Downlaod the integral API from here.
  2. Put it in the /lib of your Tomcat installation folder if you want to use it in a web project or any folder accessible in your hard disk
  3. Right click on your project in Eclipse > Properties > Java Build Path > Add External JARs… > Choose the jar :  sqljdbc4.jar from the installation folder > OK. the jar will appear in the build path. Click on OK.
  4. Use the following code in your method :
    private static final String connectionString =”jdbc:sqlserver://IP_OR_NAME_SERVER;DatabaseName=DATABASE_NAME;user=USER;password=PWD“;
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    conn = DriverManager.getConnection(connectionString);
    stmt = conn.createStatement();
    String sqlQuery=”YOUR_QUERY_HERE “; // You put here your query : SELCET, UPDATE, DELETE, INSERT
    stmt.executeQuery(sqlQuery); // If it is a SELECT QUERY e.g : sqlQuery=”SELECT user,email FROM user”;
    while ( rs.next() ){ // To get data from result
    Person user = new Person();          user.setName(rs.getString(“user”));          user.setEmail(rs.getString(“email”));

    stmt.executeUpdate(sqlQuery); // If it is an UPDATE, DELETE or INSERT QUERY
    } catch (Exception e) {
    } finally {
    if (rs != null)
    try { rs.close(); } catch (Exception e) { // LOGGER }
    if (stmt != null)
    try { stmt.close(); } catch (Exception e) { // LOGGER}
    if (conn != null)
    try { conn.close(); } catch (Exception e) {// LOGGER }
  5. Congratulation… You made a connection with your sqlServer database by using JAVA and JDBC.


See also :



Share it with your freinds on Facebook Tweet it to your freinds on Twitter Myspace Friendfeed Technorati del.icio.us Digg Google Yahoo Buzz StumbleUpon


No ping yet

  1. Boite de com says:

    I’ve recently been searching for precisely this info. While I used to be expecting to locate…

  2. Low Fat Foods says:

    Hello! I realize this is kind of off-topic however I needed to ask. Does operating a well-established website like yours require a massive amount work? I am completely new to blogging but I do write in my diary every day. I’d like to start a blog so I will be able to share my experience and feelings online. Please let me know if you have any kind of suggestions or tips for new aspiring blog owners. Appreciate it!

    1. jalal mjadli says:

      Hello! I see what you want to say, but to do this quickly:
      To have a good blog (layout + content): yes it requires a massive amount work : you start by finding the idea of your blog + to choose a good layout + to write
      To find idea : I think you should focus on your experience and your knowledge.
      I’m thinking seriously to write a post sharing my experience in blogging even it still young and modest…

  3. Forex trading systems says:

    Appreciation pro this article. Present are categorically tips participating in at this juncture to I choice benefit.

  4. car repo says:

    Hey blogger, nice work with the choice of theme on this blog. It looks reall nice.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

WP SlimStat