Wednesday, October 17, 2012

Huong dan tao ket noi jdbc don gian

http://youtu.be/576PGkeXZ-Q

Huong dan tao ket noi jdbc don gian

Tạo và sử dụng dịch vụ web đơn giản

http://youtu.be/YW1k8bqRtdg

Web service

Friday, October 12, 2012

Tài liệu PHP tham khảo

Hi đây là tài liệu PHP và MySql khá hay tôi share ra đây để mọi người tham khảo nhé.

PHP and MySQL Web Development 4th Edition

Tuesday, October 9, 2012

RMI - ví dụ tạo ứng dụng phân tán đơn giản

Create RMI Application




  1. Create Remote Interface

  2. Create Remote Object

  3. Implement the Remote Interface

  4. Built RMI Server

  5. Built RMI Client


Step I.               Create Java Application

Create the Java application with name RMI.

Create Remote Interface

[sourcecode language="csharp"]

/*Create Remote Interface
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;

/**
*
* @author Administrator
*/
public interface WeatherInterface extends Remote{
public Weather getWeather() throws RemoteException;
}
[/sourcecode]

Step II.            Create Remote Object

[sourcecode language="csharp"]
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package rmi;

import java.io.Serializable;

/**
*
* @author Administrator
*/
public class Weather implements Serializable{

private String name = "";
private float temperature = 0.0F;

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the degree
*/
public float getTemperature() {
return temperature;
}

/**
* @param degree the degree to set
*/
public void setTemperature(float temperature) {
this.temperature = temperature;
}

public Weather() {
name = "NoCity";
temperature = 0.0F;
}
}
[/sourcecode]

Step III.         Implement the Remote Interface

[sourcecode language="csharp"]
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
*
* @author Administrator
*/
public class WeatherImpl extends UnicastRemoteObject implements WeatherInterface{

Weather weather;

public WeatherImpl(Weather weatherObj) throws RemoteException
{
super();
this.weather = weatherObj;
}

public Weather getWeather() throws RemoteException {
return weather;
}

}
[/sourcecode]

Step IV.          Built RMI Server

When click on Start server

[sourcecode language="csharp"]
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
weather = new Weather();
weather.setName("Can Tho");
weather.setTemperature(50.5F);
try {
weatherImpl = new WeatherImpl(weather);
LocateRegistry.createRegistry(5000);
Naming.rebind("rmi://localhost:5000/WeatherServer",weatherImpl);
JOptionPane.showMessageDialog(this, "Server started");
jButton1.setEnabled(false);
jButton2.setEnabled(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
[/sourcecode]


When click on  Update

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

        // TODO add your handling code here:

        weather.setName(jTextField1.getText());

        weather.setTemperature(Float.parseFloat(jTextField2.getText()));

    }

Step V.             Built RMI Client

When click on Get weather

[sourcecode language="csharp"]
Weather weather;
WeatherInterface weatherInter;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try {
weatherInter = (WeatherInterface)Naming.lookup("rmi://localhost:5000/WeatherServer");
weather = weatherInter.getWeather();
JOptionPane.showMessageDialog(this, weather.getName() + " is " + weather.getTemperature());
} catch (RemoteException ex) {
ex.printStackTrace();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (NotBoundException ex) {
ex.printStackTrace();
}
}
[/sourcecode]




Sunday, October 7, 2012

Lập trình phân tán với .Net - (.Net remoting)

DOT NET REMOTING

  1. Create Remote Object

  2. Create Server

  3. Create Client



  • Create Class Library  project

  • Create a class MyProxy


[sourcecode language="csharp" wrapline="false"]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Security.Permissions;
using System.Data;
using System.Data.SqlClient;

namespace ProxyObject
{
public class MyProxy: MarshalByRefObject
{
ArrayList Re_list = new ArrayList();
/// <summary>
/// Ham khoi tao
/// </summary>
public MyProxy(){}

public bool MakeRequest(string UserName)
{
this.Re_list.Add(UserName + " - " + DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"));
return true;
}

public string GetInfor()
{
string re = "Hello: ";
for (int i = 0; i < this.Re_list.Count; i++)
{
re += "\n"+this.Re_list[i];
}

return re;
}

public DataSet GetEmployees()
{
DataSet ds = new DataSet();

SqlConnection conn = new SqlConnection("Server=.\\SqlExpress;database=Northwind;Integrated Security=SSPI;");
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter("Select * from Employees", conn);
adp.Fill(ds, "Employee");

return ds;
}

[PrincipalPermissionAttribute(SecurityAction.Demand, Role = "Administrators")]
public string Shutdown()
{
Process.Start("shutdown", "/l");
return "OK";
}
}
}
[/sourcecode]

  • Combine project

  • Create new Window Application Project

  • Add References library  project

  • Add new form and design as




[sourcecode language="csharp" wrapline="false"]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using ProxyObject;
using System.Runtime.Remoting.Channels.Http;

namespace Server
{
public partial class FrmServer : Form
{
HttpChannel httpChanel;
public FrmServer()
{
InitializeComponent();
AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
}

private void btnStart_Click(object sender, EventArgs e)
{
StartServer();
}

private void StopServer()
{
if (ChannelServices.GetChannel("http") != null)
ChannelServices.UnregisterChannel(httpChanel);
lstLog.Items.Add("Server stopped !");
}

private void StartServer()
{
StopServer();
int port = Convert.ToInt32(txtPort.Text);
WellKnownObjectMode mode = chkCall.Checked ? WellKnownObjectMode.SingleCall : WellKnownObjectMode.Singleton;
httpChanel = new HttpChannel(port);
ChannelServices.RegisterChannel(httpChanel, false);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyProxy), "MyServices", mode);

lstLog.Items.Add("Server started !");
}

private void btnStop_Click(object sender, EventArgs e)
{
StopServer();
}

private void button1_Click(object sender, EventArgs e)
{
ProxyObject.MyProxy obj = new MyProxy();
obj.Shutdown();
}
}
}
[/sourcecode]

  • Create new Window Application Project

  • Add References library  project

  • Add new form and design as




[sourcecode language="csharp"]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using ProxyObject;
using System.Security.Principal;

namespace Client
{
public partial class Form1 : Form
{
MyProxy obj;
public Form1()
{
InitializeComponent();

AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
}

private void btnConnect_Click(object sender, EventArgs e)
{
string url = "http://" + txtServer.Text + ":" + txtPort.Text + "/MyServices";
RemotingConfiguration.RegisterWellKnownClientType(typeof(MyProxy), url);
btnGet.Enabled = true;
button1.Enabled = true;
obj = new MyProxy();
btnConnect.Enabled = false;
}

private void btnGet_Click(object sender, EventArgs e)
{
obj.MakeRequest(textBox1.Text);
rTContent.Text = obj.GetInfor();
}

private void button1_Click(object sender, EventArgs e)
{
try
{
obj = new MyProxy();
dataGridView1.DataSource = obj.GetEmployees().Tables[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
[/sourcecode]



Test application



Chúc bạn thành công.

Translate