CREATING A CRYSTAL REPORT WITHOUT A DATASOURCE

If you want to generate crystal reports without using database tables, stored procedures, datasets but you want to generate reports with direct user inputs, for example printing receipts directly from user inputs, then here is your stop. It is all about using parameters. It is assumed you know how to generate reports with crystal reports.

Below is a simple procedure for you to do so.

To start with

Add a new crystal report document to your   project by choosing a blank report. Also create a form and add a crystalreportviewer to it. Design your report by creating the needed parameters that the report will accept from the user. For example as shown below

no datasource

Then add the method below to a click event. Don’t forget to do these imports

1.  Imports CrystalDecisions.CrystalReports.Engine

2. Imports CrystalDecisions.Shared

Private Sub printfees()

Dim rpd As New ReportDocument

‘loading the report from a specified location

rpd.Load(Application.StartupPath & "\reports\rptPaymentReceipt.rpt")

‘assigning values to your report parameters

rpd.SetParameterValue("prog", Me.txtProgram.Text)

rpd.SetParameterValue("sname", Me.txtName.Text.Replace("–", "").Trim)

rpd.SetParameterValue("acadyear", "Year: " & Me.lblYear.Text & " Sem:  " & Me.lblSemester.Text)

rpd.SetParameterValue("pdate", CDate(Me.dtpPaid.Value))

rpd.SetParameterValue("indexno", Me.txtIndexNo.Text)

rpd.SetParameterValue("ptype", Me.dspaytype.Rows(Me.cmbPaytype.SelectedIndex)(1).ToString)

‘frmReportsFace is a form with a crystalreportsviewer called crv1

Dim f As New frmReportsFace

f.crv1.ReportSource = rpd

‘below is a method to make your crystal reports independent on a ‘patrticular database server

SetDBLogonForReport(rpd)

f.Show()

End Sub

‘ a method that makes your crsystal reports independent on the database sever

Private Sub SetDBLogonForReport(ByVal myReportDocument As ReportDocument)

Dim myConnectionInfo As ConnectionInfo = New ConnectionInfo()

‘dbase is a class to return certain information about databases,

myConnectionInfo.DatabaseName = dbase.getDataname

myConnectionInfo.UserID = dbase.getUName

myConnectionInfo.Password = dbase.getdbasePassword

myConnectionInfo.ServerName = dbase.getServerName

myConnectionInfo.IntegratedSecurity = dbase.getIntegratedSecurity

myConnectionInfo.AllowCustomConnection = True

Dim myTables As Tables = myReportDocument.Database.Tables

For Each myTable As CrystalDecisions.CrystalReports.Engine.Table In myTables

Dim myTableLogonInfo As TableLogOnInfo = myTable.LogOnInfo

myTableLogonInfo.ConnectionInfo = myConnectionInfo

myTable.ApplyLogOnInfo(myTableLogonInfo)

Next

End Sub

Advertisements

Running Client part of Java Enterprise Application using a batch file

Below is a simple way of running the client part of a J2EE application, by creating a batch file. Some assumtions made are :

1. JDK is installed on the Computer

2. Glassfish is installed and the EJB module deployed

To create a batch file, open notepad.exe, type your need commands and save your file as filename.bat

Below is the content of the batch file

Rem the portion below starts the Glassfish server if it has been shutdown

cd C:\Sun\SDK\lib
call “C:\Sun\SDK\config\asenv.bat”
call “%AS_INSTALL%\bin\asadmin.bat” start-domain domain1

goto e

Rem the portion below call the jar file, by going to its directory and

Rem calling the full class name (including package name of the start up file

:e
cd c:\
cd C:\sabon\eduSSS
set APPCPATH=.\
appclient -client EducationSSSClient.jar -mainclass com.sabonay.educationSss.ui.LoginForm
goto end

:end

Below is a picture after running the script

scriptspage

 

Backup MYSQL database from a JAVA Application

Below is a simple application to back up your mysql database from a Java application. this post show the GUI of the application and some selected methods that do the job. Hope this will be useful

ma blog

//search for this import on the net

import org.apache.commons.io.FileUtils;

public class dbasebackupdetailpanel extends javax.swing.JPanel {

JFileChooser fc = new JFileChooser();

private boolean isConnected = false;

Connection conn = null;

List tables = null;

String url, password, uname;

//handles the backup button  click

private void btnBackupActionPerformed(java.awt.event.ActionEvent evt) {

try {

fc.setDialogTitle("Create a file to BackUp database");

fc.setCurrentDirectory(fc.getCurrentDirectory());

if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {

File file = new File(fc.getSelectedFile().getAbsolutePath() + ".sql");

String data = null;

if (chkTables.isSelected() == false) {

data = this.getData(tfHost.getText(), tfPort.getValue().toString(), tfUser.getText(), tfPassword.getText(), "dbFStation");

} else {

data = this.getData(tfHost.getText(), tfPort.getValue().toString(), tfUser.getText(), tfPassword.getText(),    "dbFStation",cmbTables.getSelectedItem().toString());

}

FileUtils.writeStringToFile(file, data);//avaible when import org.apache.commons.io.FileUtils;

xputils.showMessage("Backup Successfull");

}

} catch (Exception e) {

xputils.showErrorMessage(e.toString());

}}

//method that handles the connect button

private void btnConnectActionPerformed(java.awt.event.ActionEvent evt) {

//validate controls

url = "jdbc:mysql://" + tfHost.getText() + ":" + tfPort.getValue() + "/dbFstation";

uname = tfUser.getText();

password = tfPassword.getText();

try {

conn = DriverManager.getConnection(url, uname, password);

if (conn != null) {

isConnected = true;

this.btnBackup.setEnabled(true);

getTableNames(conn);

this.cmbTables.setModel(tablesModel());

xputils.showMessage("Connection was  Succesfull");

} else {

this.btnBackup.setEnabled(false);

isConnected = false;

xputils.showMessage("Connection was not Succesfull");

}

} catch (SQLException ex) {

Logger.getLogger(dbasebackupdetailpanel.class.getName()).log(Level.SEVERE, null, ex);

}}

private int BUFFER = 10485760;

//method to backup mysql database all tables

private String getData(String host, String port, String user,

String password, String db) throws Exception {

Process run = Runtime.getRuntime().exec(

"mysqldump –host=" + host + " –port=" + port +" –user=" + user + " –password=" + password +    " " +                db);

InputStream in = run.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(in));

StringBuffer content = new StringBuffer();

int count;

char[] cbuf = new char[BUFFER];

while ((count = br.read(cbuf, 0, BUFFER)) != -1) {

content.append(cbuf, 0, count);

}

br.close();

in.close();

return content.toString();

}

//method to backup mysql database –one  table

private String getData(String host, String port, String user,

String password, String db, String table) throws Exception {

Process run = Runtime.getRuntime().exec(

"mysqldump –host=" + host + " –port=" + port +  " –user=" + user + " –password=" + password + " " +                db + "  " + table);

InputStream in = run.getInputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(in));

StringBuffer content = new StringBuffer();

int count;

char[] cbuf = new char[BUFFER];

while ((count = br.read(cbuf, 0, BUFFER)) != -1) {

content.append(cbuf, 0, count);

}

br.close();

in.close();

return content.toString();

}

  //method to retrive table names in mysql

private void getTableNames(Connection con) {

String[] DB_TABLE_TYPES = {"TABLE"};

String COLUMN_NAME_TABLE_NAME = "TABLE_NAME";

ResultSet rs = null;

try {

DatabaseMetaData meta = conn.getMetaData();

rs = meta.getTables(null, null, null, DB_TABLE_TYPES);

if (rs != null) {

tables = new ArrayList();

while (rs.next()) {

String tableName = rs.getString(COLUMN_NAME_TABLE_NAME);

if (tableName != null) {

tables.add(tableName);

}}}

con.close();

} catch (Exception e) {

e.printStackTrace();

}}

//binding a list of tablenames to a comboboxmodel

public ComboBoxModel tablesModel() {

ComboBoxModel model;

if (tables == null || tables.size() <= 0) {

Object[] d = new Object[1];

ComboBoxModel mo = new DefaultComboBoxModel(d);

return mo;

}

Object[] days = new Object[tables.size()];

int i = 0;

try {

ListIterator lg = tables.listIterator();

while (lg.hasNext()) {

days[i] = tables.get(i);

i = i + 1;

}

} catch (Exception ex) {

System.out.println("error occured " + ex.toString());

}

model = new DefaultComboBoxModel(days);

return model;

}