"; } mysql_close($conn); //以下是php内嵌html代码 $ar = array('demo' => '111', 'test' => '222'); $html = <<
HTML; echo $html;
Computer has revolutionized the world and programming is the most important tool to make our dream come true;
Friday, March 27, 2015
php 连接 mysql以及嵌入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=?11*11=? 这个结果和实际不相符的原因是blgspot可能对html进行了加工,在正常的网页中正常显示
document.forms[0];是指获取第一个form
另外,这种单选框的name必须一直才能保证单选显示效果: 12*12=?
11*11=?
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必须是一个对象。
线程同步是指线程之间所具有的一种制约关系,一个线程的执行依赖另一个线程的消息,当它没有得到另一个线程的消息时应等待,直到消息到达时才被唤醒。线程互斥是指对于共享的进程系统资源,在各单个线程访问时的排它性。当有若干个线程都要使用某一共享资源时,任何时刻最多只允许一个线程去使用,其它要使用该资源的线程必须等待,直到占用资源者释放该资源。线程互斥可以看成是一种特殊的线程同步(下文统称为同步)。
线程间的同步方法大体可分为两类:用户模式和内核模式。顾名思义,内核模式就是指利用系统内核对象的单一性来进行同步,使用时需要切换内核态与用户态,而用户模式就是不需要切换到内核态,只在用户态完成操作。
用户模式下的方法有:原子操作(例如一个单一的全局变量),临界区。内核模式下的方法有:事件,信号量,互斥量。
方法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
- public class CriticalSectionMonitor
- {
- static String message = "Now Is The Time For All Good Men\n";
- static int numThreads = 200;
- static Object monitor = new Object();
- public static void main(String args[])
- {
- for (int idx = 0; idx < numThreads; idx++) {
- Thread t = new Thread(new StreamPrinter());
- t.start();
- }
- // Needed to start / unblock the first waiting thread.
- synchronized(monitor) {
- monitor.notify();
- }
- }
- static class StreamPrinter implements Runnable
- {
- @Override
- public void run()
- {
- try {
- while (true) {
- byte chars[] = message.getBytes();
- synchronized(monitor) { // Start Critical Section
- monitor.wait();
- }
- for (int idx = 0; idx < chars.length; idx++) {
- byte achar = chars[idx];
- System.out.write((char) achar);
- Thread.yield();
- }
- synchronized(monitor) { // End Critical Section
- monitor.notify();
- }
- }
- }
- catch (InterruptedException ex) {
- System.err.println(ex.getLocalizedMessage());
- }
- }
- }
- }
注意下面两个例子中的错误,第一个是是没有同步方法,第二个是同步错误,该class使用同步方法,但是没有静态化,所以200个线程只是锁定了自己的方法,同步没有达到该有的效果。
- public class CriticalSectionBroken
- {
- 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
- {
- @Override
- public void run()
- {
- while (true) {
- byte chars[] = message.getBytes();
- for (int idx = 0; idx < chars.length; idx++) {
- byte achar = chars[idx];
- System.out.write((char) achar);
- Thread.yield();
- }
- }
- }
- }
- }
- public class CriticalSectionFailedSynchronize
- {
- 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
- {
- @Override
- public void run()
- {
- while (true) {
- printMessage();
- }
- }
- private synchronized void printMessage()
- {
- byte chars[] = message.getBytes();
- for (int idx = 0; idx < chars.length; idx++) {
- byte achar = chars[idx];
- System.out.write((char) achar);
- Thread.yield();
- }
- }
- }
- }
Saturday, March 14, 2015
批量导入.dat 文件进入sql server
先create table,确保和dat文件的数据格式一直
在在query里面
在在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
稍微改变格式
2. 2.jxl对Excel表格的认识:每个单元格的位置认为是由一个二维坐标(i,j)给定,其中i表示列,j表示行,并且从上到下递增,从左到右递增。
3. 本代码读取text.xls的头三列,按行输出到result.txt
稍微改变格式
- import java.io.File;
- import java.io.FileOutputStream;
- import jxl.*;
- public class Read_excel{
- public static void main(String[] args) {
- int i;
- Sheet sheet;
- Workbook book;
- Cell cell1,cell2,cell3;
- String content;
- try {
- //t.xls为要读取的excel文件名
- book= Workbook.getWorkbook(new File("e:\\test.xls"));
- //获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
- sheet=book.getSheet(0);
- //获取左上角的单元格
- cell1=sheet.getCell(0,0);
- System.out.println("标题:"+cell1.getContents());
- File outFile=new File("e:\\result.txt");
- FileOutputStream fos=new FileOutputStream(outFile);
- i=1;
- while(true)
- {
- //获取每一行的单元格
- cell1=sheet.getCell(0,i);//(列,行)
- cell2=sheet.getCell(1,i);
- cell3=sheet.getCell(2,i);
- if("".equals(cell1.getContents())==true) //如果读取的数据为空
- break; System.out.println(cell1.getContents()+"\t"+cell2.getContents()+"\t"+cell3.getContents());
- content=cell1.getContents()+"\t"+cell2.getContents()+"0000&&&&&"+cell3.getContents()+"\r\n";
- byte[] contentInBytes = content.getBytes();
- fos.write(contentInBytes);
- i++;
- }
- book.close();
- fos.close();
- }
- catch(Exception e) { }
- }
- }
Sunday, March 8, 2015
创建WCF web服务并使用WPF调用
1. 新建WCF application
2. 删除系统生成的两个文件IService1.cs与Service1.svc(或者自己重命名,在系统生成的基础文件中进行编码,此处感觉重命名麻烦,于是选择删除–新建)
3. 添加自定义的WCF【服务文件】Test.svc, 此时vs会自动生成WCF接口文件ITest.cs
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“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;
}
}
}
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;
}
}
}
不可退出,必须保持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
Subscribe to:
Comments (Atom)