Friday, February 28, 2014

Wednesday, January 22, 2014

Suy ngẫm cuối năm

Ảnh
Ảnh
Tờ 1000 đồng và tờ 500.000 đồng tái ngộ.
Tờ 1000 đồng hỏi:
- Lâu này cậu biến đi đâu thế?
Tờ 500.000 đồng kiêu hãnh trả lời:
- Tớ lang thang đến mấy sòng bạc, chu du trên biển,
tham dự 10 trận bóng đá, tới vài quán bar, vũ trường … Mấy thứ đại loại thế. Còn cậu?
Tờ 1000 đồng mỉm cười và nói: ” Đến với những mảnh đời bất hạnh …” 

Friday, November 1, 2013

Một ví dụ nhỏ cho - mã hóa mật khẩu bằng giải thuật md5

Chào các bạn trong quá trình thực hiện đồ án java desktop application tôi đã nhận khá nhiều câu hỏi của các bạn về các mã hóa mật khẩu trong hệ thống phần mềm của các bạn. Để các bạn có thể tham khảo tôi viết lại thành 1 ví dụ đơn giản bao gồm thêm dữ liệu (đăng ký) có mã hóa mật khẩu và so sánh dữ liệu đã mã hóa (login) bằng giải thuật md5.

Bảng dữ liệu đơn giản Users(username,password,gender)

Giao diện thêm dữ liệu
register

Giao diện so sánh dữ liệu
login

Code mã hóa chuỗi theo giải thuật md5
[sourcecode language="java"]
public static String md5(String msg) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(msg.getBytes());
byte byteData[] = md.digest();
//convert the byte to hex format method 1
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteData.length; i++) {
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
} catch (Exception ex) {
return "";
}
}
[/sourcecode]

Khi nhấn vào nút add
[sourcecode language="java"]
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn;
conn = DriverManager.getConnection("jdbc:sqlserver://ntdan-lt;user=sa;password=Admin@123;database=Northwind;");
PreparedStatement command = conn.prepareStatement("Insert into Users(username, password,gender) values(?,?,?)");
command.setString(1, txtUser.getText());
command.setString(2, MDI_Java_md5.md5(txtPass.getText()));
command.setInt(3, rdMale.isSelected() == true ? 1 : 0);
command.executeUpdate();

JOptionPane.showMessageDialog(this, "Register OK !");
this.setVisible(false);

} catch (Exception ex) {
Logger.getLogger(FrmAdd.class.getName()).log(Level.SEVERE, null, ex);
}
}
[/sourcecode]
Khi nhấn vào nút login
[sourcecode language="java"]
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn;
conn = DriverManager.getConnection("jdbc:sqlserver://ntdan-lt;user=sa;password=Admin@123;database=Northwind;");
PreparedStatement command = conn.prepareStatement("Select * from Users where username=? and password = ?");
command.setString(1, txtUser.getText());
command.setString(2, MDI_Java_md5.md5(txtPass.getText()));
ResultSet rs = command.executeQuery();

if (rs.next()) {
JOptionPane.showMessageDialog(this, "Login OK !");
this.setVisible(false);
} else {
JOptionPane.showMessageDialog(this, "Login fail !");
}

} catch (Exception ex) {
Logger.getLogger(FrmAdd.class.getName()).log(Level.SEVERE, null, ex);
}
}
[/sourcecode]
chúc các bạn thành công.

Thursday, October 31, 2013

Java server face (JSF) - trang đa ngôn ngữ

OBJECTIVES

  1. -Create jsf web page support multi language interface

  2. -Using Face component Event

  3. -Setting current Locale and Global Locale


Hints:

  1. Create JSF web site

  2. Create 03 jsp page and configure navigation rule for them from properties file support two language Vietnam and English

  3. Setting current Locale and Global Locale at runtime and design time


Create JSF website



  • Create 03 jsp file: index.jsp, add.jsp, list.jsp

  • Open Face-config.xml, design navigation rule as figure 01 and insert new code as code 01


page-flow


[sourcecode language="xml"]
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
<resource-bundle>
<var>bundle</var>
<base-name>code.guiMessage</base-name>
</resource-bundle>
<locale-config>
<default-locale>vi</default-locale>
</locale-config>
</application>
[/sourcecode]

Code 01: Register bundle file


Create properties file



  • Name: guiMessage in side package code

  • Add two Locale vi_VN and en_US

  • Rename file as figure 02


bundle

Right click o guiMessage file select open and insert some key as figure 03

properties

Create manage bean



  • Language in code package

  • Modified code as



[sourcecode language="java"]
package code;

import java.util.Locale;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

public class language {

public String getLang() {
return lang;
}

public void setLang(String lang) {
this.lang = lang;
}
String lang = &amp;amp;quot;vi&amp;amp;quot;;

/**
* Creates a new instance of language
*/
public language() {
}

public void change(ValueChangeEvent event) {
lang = event.getNewValue().toString();
FacesContext.getCurrentInstance().getApplication().setDefaultLocale(new Locale(lang));
FacesContext.getCurrentInstance().getViewRoot().setLocale(new Locale(lang));
}
}
[/sourcecode]

This code allow we setting Local and Global Locale of websie

Open index.jsp


Modified as figure 03 (using EL language: after bundle. Using Ctrl+Space bar for virtual code)

index

Index.jsp with two language

index1

Add Add.jsp page

add

[sourcecode language="html"]
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<f:view>
<h:form>
<table>
<tr>
<td align="center" colspan="2">
<h1><h:outputText value="#{bundle.register}"/></h1>
<h4><h:commandLink value="#{bundle.home}" action="home"/></h4>
<h:messages layout="table"/>
</td>
</tr>
<tr>
<td>
<h:outputText>
<f:attribute name="value" value="#{bundle.id}"/>
</h:outputText>
</td>
<td>
<h:inputText value="#{Customer.customerID}">

</h:inputText>
</td>
</tr>
<tr>
<td><h:outputText value="#{bundle.CompanyName}"/></td>
<td><h:inputText value="#{Customer.companyName}"/></td>
</tr>
<tr>
<td><h:outputText value="#{bundle.Address}"/></td>
<td><h:inputText value="#{Customer.address}"/></td>
</tr>
<tr>
<td></td> <td><h:commandButton value="#{bundle.register}" action="list"
actionListener="#{Customer.AddNew}"/></td>
</tr>
</table>
</h:form>
</f:view>
[/sourcecode]

Đổi không khí học winform C# - Sử dụng timer control viết game click to win

Bắt đâu: Giao diện game

Game

Click các số 0 cho đến khi chúng mất hêt

win

Vậy là thắng rồi :D (chỉ để đổi không khí học một tí nhe các bạn)

Source nguồn đây.

Friday, October 11, 2013

Xây dựng và sử dụng WCF đơn giản (WCF basic demo for student)

Objectives

1. Create WCF service (host on web server )

2. Using WCF service from window form application

Hits: Step for create and using service
  • Create WCF service “Student” with two operations “list()” and “detail(id)”
    1. Define service interface (ServiceContract)
    2. Define class describe detail of student (DataContract)
    3. Define Service by implement from server interface
  • Using WCF service from window form application
    1. Find service description of WCF “student”
    2. Reference WCF service
    3. Call operations of student service

Student table

No. Data field Data type
1. S_ID Int
2. S_FullName Text
3. S_Birthdate DateTime
4. S_Address Text

WCF basic demo 2013 (PDF version of this post)

http://www.mediafire.com/?ji9uiz5l70d8n (Source code of this post)

BEGIN

Open visual studio 2008 (or later)

File -> Create Project

clip_image002

Select as figure

Right click on project -> Add new Item

clip_image004

Select as figure

Right click on project -> Add new Item

clip_image006

Select as figure

Modified class as

1 namespace students
2 {
3 [DataContract]
4 public class StudentDetail
5 {
6 [DataMember]
7 public int ID;
8 [DataMember]
9 public string FullName;
10 [DataMember]
11 public DateTime Birthdate;
12 [DataMember]
13 public string Address;
14 }
15 }
16
17 Modified IStudent interface as
18
19 namespace students
20 {
21 [ServiceContract]
22 public interface IStudent
23 {
24 [OperationContract]
25 DataSet List();
26 [OperationContract]
27 StudentDetail Detail(int ID);
28 }
29 }

Edit Student service as


clip_image002[4]



1 namespace students
2 {
3 public class Student : IStudent
4 {
5 public DataSet List()
6 {
7 DataSet dsS = new DataSet();
8 // insert C# code to get data from student table
9 return dsS;
10 }
11 public StudentDetail Detail(int ID)
12 {
13 StudentDetail sDetail = new StudentDetail();
14 return sDetail;
15 }
16 }
17 }
18
Now right click on service and select view in browser


clip_image005


Right click on solution -> add new project


clip_image007


Select as figure


Design form as


clip_image010


GUI of client


Add service reference as


clip_image012


Modify form load event:


1 wcf.StudentClient proxy;
2 private void Form1_Load(object sender, EventArgs e)
3 {
4 proxy = new WCF_Client.wcf.StudentClient();
5 }

Double click on button Find and insert code as


1 private void btnFind_Click(object sender, EventArgs e)
2 {
3 wcf.StudentDetail detail = proxy.Detail(Convert.ToInt32(txtID.Text));
4 MessageBox.Show("Student id: " + detail.ID + "\nStudent fullname: " + detail.FullName);
5 }
6

Double click on button List and insert code as


1 private void btnList_Click(object sender, EventArgs e)
2 {
3 dgrStudent.DataSource = proxy.List();
4 dgrStudent.DataMember = "student";
5 }

Now run client


clip_image014


Client at runtime








Xem hướng dẫn video trên


Mã nguồn:  http://www.mediafire.com/download/f4l6dpz2wla513w/WebApplication1.rar

Translate