This post is for generating HTML content in java. You can use 3rd party jar called rendersnake to do this kind of thing very easily. This has class called HTMLCanvas and you use hierarchical structure to build the HTML.
You can find more samples and information from
http://rendersnake.org/index.html
https://code.google.com/p/rendersnake/
This is a free opensource library and can find the license here : http://www.apache.org/licenses/LICENSE-2.0.html
Here is a sample code snippet
HtmlCanvas htmlCanvas = new HtmlCanvas();
HtmlAttributes tableAttributes = new HtmlAttributes();
tableAttributes
.add("border", "1")
.add("bgcolor", "#E8E8E8")
.add("cellspacing", "0")
.add("cellpadding", "5")
.add("bordercolor", "#B0B0B0");
HtmlAttributes rowHeaderAttribute = new HtmlAttributes();
rowHeaderAttribute.add("bgcolor", "#C8C8C8");
HtmlAttributes taskRowHeaderAttribute = new HtmlAttributes();
taskRowHeaderAttribute.add("bgcolor", "#C8C8C8");
taskRowHeaderAttribute.add("width", "400");
taskRowHeaderAttribute.add("height", "50");
HtmlAttributes cellColor = new HtmlAttributes();
cellColor.add("bgcolor", "#009933");
htmlCanvas.html()
.body()
.b().content("Status : OK")
.br()
.table(tableAttributes).th(taskRowHeaderAttribute).content("Task")
.th(rowHeaderAttribute).content("User")
.th(rowHeaderAttribute).content("Host")
.th(rowHeaderAttribute).content("Status");
for(int i=0; i<10; i++) {
htmlCanvas.tr().td().content("Task " + i)
.td().content("User " + i)
.td().content("Host " + i)
.td(cellColor).content("Status " + 1)
._tr();
}
htmlCanvas._table().br().br()._body()._html();
System.out.println("Result : " + htmlCanvas.toHtml());
Following is the result of above code.
You need to add the jar that downloaded from the above link and can do this kind of table very easily. If there is a exception occurred when you run the application call specifically "commons-lang3-3.1.jar" not found, you need to add that jar file as well. You can find this jar from http://commons.apache.org/proper/commons-lang/download_lang.cgi
Friday, March 29, 2013
Wednesday, March 27, 2013
Java DateTime Formatting
You can use following mechanism to get the date and time as a format you preferred. You need to use the Calendar class in the java.util package. It is possible to filter the date time using SimpleDateFormat class in the java.text package by passing the format to the constructor.
public String getTimeStamp(){
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_hhmmss");
String timeStamp = "";
try {
timeStamp = format.format(date);
} catch (Exception e1) {
e1.printStackTrace();
}
return timeStamp;
}
| Letter | Date or Time Component | Presentation | Examples |
|---|---|---|---|
G | Era designator | Text | AD |
y | Year | Year | 1996; 96 |
M | Month in year | Month | July; Jul; 07 |
w | Week in year | Number | 27 |
W | Week in month | Number | 2 |
D | Day in year | Number | 189 |
d | Day in month | Number | 10 |
F | Day of week in month | Number | 2 |
E | Day in week | Text | Tuesday; Tue |
a | Am/pm marker | Text | PM |
H | Hour in day (0-23) | Number | 0 |
k | Hour in day (1-24) | Number | 24 |
K | Hour in am/pm (0-11) | Number | 0 |
h | Hour in am/pm (1-12) | Number | 12 |
m | Minute in hour | Number | 30 |
s | Second in minute | Number | 55 |
S | Millisecond | Number | 978 |
z | Time zone | General time zone | Pacific Standard Time; PST; GMT-08:00 |
Z | Time zone | RFC 822 time zone | -0800 |
public String getTimeStamp(){
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_hhmmss");
String timeStamp = "";
try {
timeStamp = format.format(date);
} catch (Exception e1) {
e1.printStackTrace();
}
return timeStamp;
}
Thursday, July 12, 2012
Fire Alarm When You Insert a Row to SQL Table
First you have to write a particular trigger for run the .exe file, when you insert a record to the data table.
Following is the Trigger
create trigger runexe
on tbDetails
for insert
As
Begin
print 'Inserted'
Exec Master..xp_cmdshell 'E:\Hiran\Alarm.exe'
End
Some SQL Server versions does not allowed to run the "xp_cmdshell" command. In that case you have to enable that feature by executing the following commands.
After that you have to write a particular C# program to run the particular batch (.bat) file. The program.cs should look like below. We can run the batch file at once. But then, it will not be Asynchronous and we have to wait until finish the wave file to insert the record. Therefore the best way is this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security;
namespace Alarm
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
System.Diagnostics.Process.Start("cmd.exe", @"/c E:\Hiran\aaa.bat");
Application.Exit();
}
}
}
Then open the notepad and write the below cording. After that save it using .bat extension.
sndrec32 /play /close "E:\Hiran\SirenSound.wav"
Now You are done. Please comment if you need any help.
Thank You.
Following is the Trigger
create trigger runexe
on tbDetails
for insert
As
Begin
print 'Inserted'
Exec Master..xp_cmdshell 'E:\Hiran\Alarm.exe'
End
Some SQL Server versions does not allowed to run the "xp_cmdshell" command. In that case you have to enable that feature by executing the following commands.
EXEC sp_configure ‘show advanced options’, 1
EXEC sp_configure ‘xp_cmdshell’, 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security;
namespace Alarm
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
System.Diagnostics.Process.Start("cmd.exe", @"/c E:\Hiran\aaa.bat");
Application.Exit();
}
}
}
Then open the notepad and write the below cording. After that save it using .bat extension.
sndrec32 /play /close "E:\Hiran\SirenSound.wav"
Now You are done. Please comment if you need any help.
Thank You.
Subscribe to:
Posts (Atom)