Thứ Hai, 1 tháng 8, 2011

Gửi Mail trong Java

Cuối cùng vất vả cả buổi tối mới làm xong cái send mail
Ghi lại vài bữa có lúc dùng tới..Code cho nhanh


Tạo file SendMail.java
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


import java.util.*;
public class SendMailApp {
private final String host = "smtp.gmail.com";
private final String port = "587";
private final String to = "vantientran.it@gmail.com";
private String from="";
private String subject="";
private String content="";
public SendMailApp(String from,String subject,String content){
this.from = from;
this.subject = subject;
this.content = content;
}
public void Send() throws Exception{
Properties props = System.getProperties();
props.put("mail.host", this.host);
props.put("mail.smtp.port",this.port);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
Authenticator pa = null;
props.put("mail.smtp.auth", "true");
pa = new Authenticator (){
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("EmailCanGuiDen", "MatKhauTruyCapEmail");
}
};
Session mailSession = Session.getInstance(props, pa);
Message msg = new MimeMessage(mailSession);
msg.setSubject(this.subject);
msg.setSentDate(new Date());
msg.setText(this.content);
msg.setFrom(new InternetAddress(this.from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(this.to));
Transport.send(msg);
}

}

Thứ Ba, 9 tháng 11, 2010

Thủ tục trong SQL Server

Bạn đã biết vê lập trình hướng thủ tục.Trong SQL cũng hổ trợ viết thủ tục.Nhìn chung nó cũng đơn giản.Thủ tục trong SQL cũng có thể mô tả như sau
a.Thủ tục không tham số
b.Thủ tục có tham số
c.Thủ tục không có tham số và không có trị trả về
d.Thủ túc có tham số và có giá trị trả về
Cú pháp:
1.Thục tục không có tham số
CREATE PROCEDURE (tên thủ tục)
AS (các câu lệnh SQL)

ví dụ:
Viết thủ tục liệt kê danh sách sinh viên của của lớp

create procedure sp_lietke
as
select * from lop_KT

Sau khi tạo xong thủ tục bạn chạy nó.
Để gọi thủ tục bạn thực hiện lệnh
EXECUTE sp_lietke

2.Thủ tục có tham số
Cú pháp
CREATE PROCEDURE (tên thủ tục)
(tham_so1 kiểu dữ liệu [giá trị khởi tạo],....)

AS
(câu lệnh sql)

Ví dụ:
Tìm sinh viên có first_name = "A" và last_name="B"
create procedure sp_tim_sv(
@f_name varchar(30),@l_name varchar(30))
as
select * from tb_KT
where (first_name like
@f_name)and(last_name like @l_name)



Duyệt tất cả table hay Database của CSDL SQL Server

Yêu cầu: Hãy duyệt xem trong Database đã có table A chưa,nếu chưa có thì tạo mới table đó còn nếu đã có thì xóa nó đi

if not exists(SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_NAME LIKE 'A')

begin
create table A
(
x varchar(10),
y varchar(10),
primary key(x)
)
end
else
drop table A

Ghi chú:
Bạn có thể thấy những thông tin liên quan đến table hay database chỉ cần tìm đến table và database cần chọn và chọn View -> System View

Code ví dụ:
1.Danh sách các table của SQL Server
SELECT * FROM sysobjects
2.Danh sách Database của SQL Server
SELECT name FROM master..sysdatabases


Đọc thấy hay thì thanks tí nha ^^