quarta-feira, 6 de outubro de 2010

Comparação entre CFML, JSP, PHP e ASP

Por Bruno Rafael, baseado no post de meu amigo Tofinha...


Os seguintes exemplos de códigos ilustram como criar uma página que fará consulta de dados de um funcionário no banco de dados e depois enviará, por e-mail, uma cópia do curriculum desse funcionário para o seu gerente. Com o ColdFusion, desempenhar essa tarefa exigirá cerca de 4 a 5 vezes menos linhas de código.

ColdFusion
<cfquery name="emplInfo" datasource="HRDatabase">
SELECT * FROM tblResume
WHERE EmployeeID = #URL.EmployeeID#
</cfquery>
<cfmail to="#Left(ManagerFName,1)##ManagerLastName#@company.com" from="hrmailer@company.com" subject="#EmployeeFName# #EmployeeLName# Resume" mimeattach="#ResumeLocation#">
Here is the resume of #EmployeeFName# #EmployeeLName#
</cfmail> 


JSP
<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String Url = "jdbc:odbc:HRDatabase";
Connection cn = DriverManager.getConnection(url);
Statement stmt = cn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM tblResume WHERE EmployeeID ="& request.getParameter("EmployeeID"));%>

<%while(rs.next())[

String from="hrmailer@company.com";
String to=Left(rs.getString("ManagerFName"),1) & rs.getString("ManagerLastName") % "@company.com";
try[
SmtpClient client = new SmtpClient("mail.xxxxx.xxx");
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.printIn("To; " +to);
message.printIn("Subject: " & rs.getString("EmployeeFName") & " " & rs.getString("EmployeeLName") & "'s Resume");
message.printIn("Here is the Resume of" & rs.getString("EmployeeFName") & " " & rs.getString("EmployeeLName"));

client.closeServer();
]
catch (IOException e)[
System.out.printIn("ERROR SENDING EMAIL:"+e);
]
]
%>


PHP
<?php

$hostname = "local";
$username = "sa";
$password = "pass";
$dbname = "HRDatabase";
$tablename = "tblResume";

$connection = mssql_connet($hostname.$username.$password);
mssql_select_db($dbname);
$sql_query = "select * from $tablename";
$query_result = mssql_query($sql_query);

$ManagerEmail = left)mssql_result($query_result,1,3) & mssql_result($query_result,1,4) & "@company.com";

$mailheaders = "From: hrmailer@.com ";
$mailheaders = "X-Mailer:PHP ";

$subject="The Resume";

$msg_body = "Here is " & mssql_result($query_result,1,1) & " " & mssql_result($query_result,1,2) & "s resume: ";

$attach = mssql_result($query_result,1,5)
$attach_size=filesize($attach);

$file = fopen($attach, "r");
$contents = fhead($file, $attach_size);
$encoded_attach = chunk_split(base64_encode($contents)); fclose($file);

$mailheaders .= "MIME-version: 1.0 ";
$mailheaders .= "Content-type: multipart/mixed; ";
$mailheaders .= "boundary="Message-Boundary" ";
$mailheaders .= "Content-transfer-encoding: 7BIT ";
$body_top = "--Message-Boundary ";
$body_top .= "Content-type: text/plain; charset=US-ASCII ";
$body_top .= "Content-transfer-encoding: 7BIT ";
$body_top .= "Content-description: Mail message body ";
$msg_body = $body_top . $msg_body;
$msg_body .= " --Message-Boundary ";
$msg_body .= "Content-type: application/msword; name="$attach" ";
$msg_body .= "Content-Transfer-Encoding: BASE64 ";
$msg_body .= "Content-disposition: attachment;
filename="$attach" ";
$msg_body .= "$encoded_Attach ";

$msg_body .= "--Message-Boundary-- ";

mail(stripslashes($ManagerEmail),$subject, $msg_body, $mailheaders)

?>


ASP
<%@ Language=VBScript %>
<%
UserID = "sa"
Password = ""
database = "HRDatabase"
InitialCatalog = "local"

set conn = server.CreateObject("adodb.connection")
set rs = server.createobject("adodb.recordset")
Set MyCDONTSMail3 = CreateObject("CDONTS.NewMail")

conn.Open "Provider=SQLOLEDB.1;Persist Security Info=False;
Password=& Password &";
UserID=" & UserID & ";
InitialCatalog=" & InitialCatalog & ";
Data Source=" & database

SelectStatement = "SELECT * FROM tblResume WHERE EmployeeID = "
SelectStatement = SelectStatement & request("EmployeeID")

set rs = conn.execute(SelectStatement)

while not rs.eof
MyCDONTSMail3.To = Left(rs("ManagerFName"),1) &
rs("ManagerLastName") &
"@company.com"
MyCDONTSMail3.Subject = rs("EmployeeFName") & " " &
rs("EmployeeLName") &
"'s Resume"
MyCDONTSMail3.AttachFile (rs("ResumeLocation"))
MyBody2 = "Here is the resume os " & rs(EmployeeFName") & " " &
rs("EmployeeLName") & vbCrLf
MyCDONTSMail3.Body = MyBody2
MyCDONTSMail3.Send
rs.movenext
wend

set MyCDONTSMail3=nothing
set rs = nothing
set conn = nothing

%>

Quais conclusões conseguiram tirar disso? agora é com vocês! Abraço e bom desenvolvimento.

Nenhum comentário:

Postar um comentário

Admin: Bruno

Olá Galera! muito grato por estarem acessando nosso blog. Espero que seja possível transmitir de forma compreensível um pouco de meus conhecimentos em programação, para esta comunidade de desenvolvedores que cresce cada vez mais! Espero que Gostem! Abraço! E meu enorme obrigado à Renato Simões, Átila Soares,Wanderson Quinto, Emerson e a toda galera que sempre ajudou meu sincero obrigado....
Especialmente a Natalia Failache e Rita de Cassia que sempre apoiaram este sonho....

De seu amigo Bruno Rafael.