Load driver for SQL Server : SqlServer « Database SQL JDBC « Java






Load driver for SQL Server

 

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class Main {
  public static void main(String[] argv) throws Exception {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

    Connection con = DriverManager.getConnection("jdbc:sqlserver://MYSERVER;databaseName=MYDATABASE",
        "USERID", "PASSWORD");

    CallableStatement proc_stmt = con.prepareCall("{ call generateID(?) }");

    proc_stmt.setString(1, "employee");
    ResultSet rs = proc_stmt.executeQuery();

    if (rs.next()) {
      int employeeId = rs.getInt(1);
      System.out.println("Generated employeeId: " + employeeId);
    } else {
      System.out.println("Stored procedure couldn't generate new Id");
    }
  }
}

   
  








Related examples in the same category

1.Connect to a database and read from table
2.Connecting to a SQLServer Database using the NetDirect JDBC driver
3.Creating a SQLServer Table to Store Java Types