Friday, March 27, 2015

php 连接 mysql以及嵌入html代码


";
  }

mysql_close($conn);


//以下是php内嵌html代码
$ar = array('demo' => '111', 'test' => '222');
$html = <<
        

HTML; echo $html;

Friday, March 20, 2015

mysql 常见操作 --经常更新

 CREATE USER 'hr'@'localhost' IDENTIFIED BY '1234';
 GRANT ALL PRIVILEGES ON company.* TO 'hr'@'localhost';
CREATE TABLE user_table(
id INT NOT NULL AUTO_INCREMENT,
user VARCHAR(20) NOT NULL,
password VARCHAR(30) NOT NULL,
PRIMARY KEY (id )
);
insert into user_table (user, password) values("guest","123456");
alter table location add my_point point;// location with 经纬坐标
update location SET my_point = PointFromText(CONCAT('POINT(',long,' ',latt,')'));
将会自动计算,以blob形式存储


Wednesday, March 18, 2015

javascipt 模拟测试


 

  
 
 
12*12=?


122 124 134 144
11*11=?
122 131 121 111
这个结果和实际不相符的原因是blgspot可能对html进行了加工,在正常的网页中正常显示
document.forms[0];是指获取第一个form
另外,这种单选框的name必须一直才能保证单选

显示效果:



12*12=?

122
124
134
144

11*11=?
122
131
121
111

Monday, March 16, 2015

格式化代码

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
using pre tag:    <pre class="brush: java"></pre>
// Comment
public class Testing {
public Testing() {
}
 
public void Method() {
/* Another Comment
on multiple lines */
int x = 9;
}
}

Java当中的常见同步方法

在进行多线程编程时,难免还要碰到两个问题,那就线程间的互斥与同步:
线程同步是指线程之间所具有的一种制约关系,一个线程的执行依赖另一个线程的消息,当它没有得到另一个线程的消息时应等待,直到消息到达时才被唤醒。线程互斥是指对于共享的进程系统资源,在各单个线程访问时的排它性。当有若干个线程都要使用某一共享资源时,任何时刻最多只允许一个线程去使用,其它要使用该资源的线程必须等待,直到占用资源者释放该资源。线程互斥可以看成是一种特殊的线程同步(下文统称为同步)。

线程间的同步方法大体可分为两类:用户模式和内核模式。顾名思义,内核模式就是指利用系统内核对象的单一性来进行同步,使用时需要切换内核态与用户态,而用户模式就是不需要切换到内核态,只在用户态完成操作。
用户模式下的方法有:原子操作(例如一个单一的全局变量),临界区。内核模式下的方法有:事件,信号量,互斥量。
方法1:使用同步法,该方法的核心在于synchronized (lock) 其中lock必须是一个对象。

public class CriticalSectionSynchronizedBlock
{
 static String message = "Now Is The Time For All Good Men\n";
 static int numThreads = 200;
 
 static Integer lock = 1;

 public static void main(String args[])
 {
  for (int idx = 0; idx < numThreads; idx++) {
   Thread t = new Thread(new StreamPrinter());
   t.start();
  }
 }

 static class StreamPrinter implements Runnable
 {
  @Override
  public void run()
  {
   while (true) {
    byte chars[] = message.getBytes();
    synchronized (lock) {
     for (int idx = 0; idx < chars.length; idx++) {
      byte achar = chars[idx];
      System.out.write((char) achar);
      Thread.yield();
     }
    }
   }
  }
 }
}
方法2: 使用信号灯:Semaphore(1)表示只能运行一个程序,Semaphore(n)可以运行n个线程
public class CriticalSectionSemaphore

{

 static String message = "Now Is The Time For All Good Men\n";

 static int numThreads = 200;



 public static void main(String args[])

 {

  for (int idx = 0; idx < numThreads; idx++) {

   Thread t = new Thread(new StreamPrinter());

   t.start();

  }

 }



 static class StreamPrinter implements Runnable

 {

  static Semaphore sema = new Semaphore(1);



  @Override

  public void run()

  {

   try {

    while (true) {

     byte chars[] = message.getBytes();

     sema.acquire();

     for (int idx = 0; idx < chars.length; idx++) {

      byte achar = chars[idx];

      System.out.write((char) achar);

      Thread.yield();

     }

     sema.release();

    }

   }

   catch (InterruptedException ex) {

    System.err.println(ex.getLocalizedMessage());

   }

  }

 }

}
acquire();表示获取信号 release();表示释放信号
1:  public class CriticalSectionMonitor  
2:  {  
3:       static String message = "Now Is The Time For All Good Men\n";  
4:       static int numThreads = 200;  
5:       static Object monitor = new Object();  
6:       public static void main(String args[])  
7:       {  
8:            for (int idx = 0; idx < numThreads; idx++) {  
9:                 Thread t = new Thread(new StreamPrinter());  
10:                 t.start();  
11:            }  
12:            // Needed to start / unblock the first waiting thread.   
13:            synchronized(monitor) {  
14:                 monitor.notify();  
15:            }  
16:       }  
17:       static class StreamPrinter implements Runnable  
18:       {  
19:            @Override  
20:            public void run()  
21:            {  
22:                 try {  
23:                      while (true) {  
24:                           byte chars[] = message.getBytes();  
25:                           synchronized(monitor) { // Start Critical Section  
26:                                monitor.wait();  
27:                           }  
28:                           for (int idx = 0; idx < chars.length; idx++) {  
29:                                byte achar = chars[idx];  
30:                                System.out.write((char) achar);  
31:                                Thread.yield();  
32:                           }  
33:                           synchronized(monitor) { // End Critical Section  
34:                                monitor.notify();  
35:                           }  
36:                      }  
37:                 }  
38:                 catch (InterruptedException ex) {  
39:                      System.err.println(ex.getLocalizedMessage());  
40:                 }  
41:            }  
42:       }  
43:  }  




方法3:monitor


  1. public class CriticalSectionMonitor
  2. {
  3. static String message = "Now Is The Time For All Good Men\n";
  4. static int numThreads = 200;
  5. static Object monitor = new Object();
  6. public static void main(String args[])
  7. {
  8. for (int idx = 0; idx < numThreads; idx++) {
  9. Thread t = new Thread(new StreamPrinter());
  10. t.start();
  11. }
  12. // Needed to start / unblock the first waiting thread.
  13. synchronized(monitor) {
  14. monitor.notify();
  15. }
  16. }
  17. static class StreamPrinter implements Runnable
  18. {
  19. @Override
  20. public void run()
  21. {
  22. try {
  23. while (true) {
  24. byte chars[] = message.getBytes();
  25. synchronized(monitor) { // Start Critical Section
  26. monitor.wait();
  27. }
  28. for (int idx = 0; idx < chars.length; idx++) {
  29. byte achar = chars[idx];
  30. System.out.write((char) achar);
  31. Thread.yield();
  32. }
  33. synchronized(monitor) { // End Critical Section
  34. monitor.notify();
  35. }
  36. }
  37. }
  38. catch (InterruptedException ex) {
  39. System.err.println(ex.getLocalizedMessage());
  40. }
  41. }
  42. }
  43. }


注意下面两个例子中的错误,第一个是是没有同步方法,第二个是同步错误,该class使用同步方法,但是没有静态化,所以200个线程只是锁定了自己的方法,同步没有达到该有的效果。
  1. public class CriticalSectionBroken
  2. {
  3. static String message = "Now Is The Time For All Good Men\n";
  4. static int numThreads = 200;
  5. public static void main(String args[])
  6. {
  7. for (int idx = 0; idx < numThreads; idx++) {
  8. Thread t = new Thread(new StreamPrinter());
  9. t.start();
  10. }
  11. }
  12. static class StreamPrinter implements Runnable
  13. {
  14. @Override
  15. public void run()
  16. {
  17. while (true) {
  18. byte chars[] = message.getBytes();
  19. for (int idx = 0; idx < chars.length; idx++) {
  20. byte achar = chars[idx];
  21. System.out.write((char) achar);
  22. Thread.yield();
  23. }
  24. }
  25. }
  26. }
  27. }
  1. public class CriticalSectionFailedSynchronize
  2. {
  3. static String message = "Now Is The Time For All Good Men\n";
  4. static int numThreads = 200;
  5. public static void main(String args[])
  6. {
  7. for (int idx = 0; idx < numThreads; idx++) {
  8. Thread t = new Thread(new StreamPrinter());
  9. t.start();
  10. }
  11. }
  12. static class StreamPrinter implements Runnable
  13. {
  14. @Override
  15. public void run()
  16. {
  17. while (true) {
  18. printMessage();
  19. }
  20. }
  21. private synchronized void printMessage()
  22. {
  23. byte chars[] = message.getBytes();
  24. for (int idx = 0; idx < chars.length; idx++) {
  25. byte achar = chars[idx];
  26. System.out.write((char) achar);
  27. Thread.yield();
  28. }
  29. }
  30. }
  31. }

Saturday, March 14, 2015

批量导入.dat 文件进入sql server

先create table,确保和dat文件的数据格式一直

在在query里面
BULK INSERT table_name
   FROM 'D:\data.DAT'
   WITH 
      (
         FIELDTERMINATOR =',',   //分隔符
         ROWTERMINATOR ='\n'  //换行
      )

Monday, March 9, 2015

java读取excel文件并改变格式输出到txt文件

1. java导入导出Excel文件要引入jxl.jar包,最关键的是这套API是纯Java的,并不依赖Windows系统,即使运行在Linux下,它同样能够正确的处理Excel文件。下载地址:http://www.andykhan.com/jexcelapi/
2. 2.jxl对Excel表格的认识:每个单元格的位置认为是由一个二维坐标(i,j)给定,其中i表示列,j表示行,并且从上到下递增,从左到右递增。
3. 本代码读取text.xls的头三列,按行输出到result.txt
稍微改变格式


  1. import java.io.File;
  2. import java.io.FileOutputStream;

  3. import jxl.*; 




  4. public class Read_excel{
  5.     public static void main(String[] args) {
  6.         int i;
  7.         Sheet sheet;
  8.         Workbook book;
  9.         Cell cell1,cell2,cell3;
  10.         String content;
  11.         try { 
  12.             //t.xls为要读取的excel文件名
  13.             book= Workbook.getWorkbook(new File("e:\\test.xls")); 
  14.             
  15.             //获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
  16.             sheet=book.getSheet(0); 
  17.             //获取左上角的单元格
  18.             cell1=sheet.getCell(0,0);
  19.             System.out.println("标题:"+cell1.getContents()); 
  20.             
  21.             File outFile=new File("e:\\result.txt");
  22.             FileOutputStream fos=new FileOutputStream(outFile);
  23.             
  24.             
  25.             i=1;
  26.             while(true)
  27.             {
  28.                 //获取每一行的单元格 
  29.                 cell1=sheet.getCell(0,i);//(列,行)
  30.                 cell2=sheet.getCell(1,i);
  31.                 cell3=sheet.getCell(2,i);
  32.                 if("".equals(cell1.getContents())==true)    //如果读取的数据为空
  33.                     break;       System.out.println(cell1.getContents()+"\t"+cell2.getContents()+"\t"+cell3.getContents());
  34.                 content=cell1.getContents()+"\t"+cell2.getContents()+"0000&&&&&"+cell3.getContents()+"\r\n";
  35.                 byte[] contentInBytes = content.getBytes();
  36.                 fos.write(contentInBytes);
  37.                 i++;
  38.             }
  39.             book.close(); 
  40.             fos.close();
  41.         }
  42.         catch(Exception e)  { } 
  43.     }
  44. }

Sunday, March 8, 2015

创建WCF web服务并使用WPF调用

1. 新建WCF application
2. 删除系统生成的两个文件IService1.cs与Service1.svc(或者自己重命名,在系统生成的基础文件中进行编码,此处感觉重命名麻烦,于是选择删除–新建)
3. 添加自定义的WCF【服务文件】Test.svc, 此时vs会自动生成WCF接口文件ITest.cs
接口文件ITest.cs
namespace WCFServiceDemo
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“ITest”。
[ServiceContract]
public interface ITest
{
[OperationContract]
int Square(int num);
}
}  主类文件Test.svc.cs
namespace WCFServiceDemo
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Test”。
public class Test : ITest
{
public int Square(int num)
{
return num*num;
}
}
}
4. F5运行, 可得到服务地址 http://localhost:11471/Test.svc
不可退出,必须保持run的状态




5. 现在新建客户端程序,使用WPF,首先添加reference 就是上面的服务地址
http://localhost:11471/Test.svc?wsdl


6. 在wpf主界面添加一个文本框和按钮
按钮的点击代码如下:

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            WCFClient2.WCFService.TestClient tc = new WCFService.TestClient();
            int x=0;
            try {x = Convert.ToInt32(textbox1.Text); }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                return;
            }
           int result = tc.Square(x);
            MessageBox.Show("result= "+result);
        }


结果如下:

注意:wcf服务的调用使用普通网页调用需要更多的设置,WCF的使用在网站服务用处很大,需要更多的学习,感觉asp.net在中小型服务中使用比java方便
另外,exception 后面不能使用e变量,改成了ex