Search code examples
sql-serversql-server-2005jasper-reportsireport

MS SQL Server connection with JasperReports


Please tell me how to set a connection with MS SQL Server 2005 and JasperReports.

Please tell me the steps for the connection.


Solution

  • You can use jdbc driver for connection with MS SQL Server.

    Connection string looks like:

    jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]

    You car read about each parameter here.

    The sample connection string:

    jdbc:sqlserver://server:port;DatabaseName=dbname

    The sample code of connecting with JasperReports API to MS SQL Database:

    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    
    String url = "jdbc:sqlserver://localhost:1433;DatabaseName=test";
    Connection connection = DriverManager.getConnection(url, "userName", "password");
    
    Map<String, Object> params = new HashMap<String, Object>();
    JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, connection);
    
    JasperExportManager.exportReportToPdfFile(jasperPrint, targetFileName);
    

    You can download JDBC Driver 2.0 here or JDBC Driver 3.0 here.