Anu
Anu

Reputation: 195

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.

Upvotes: 1

Views: 10310

Answers (1)

Alex K
Alex K

Reputation: 22867

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.

Upvotes: 6

Related Questions