2010年4月18日星期日

[Microsoft SQL Server 2000]安装程序配置服务器失败,参考服务器错误日志和sqlstp.log了解更多信息

 

遇到了这个问题,解决的方法:

1、把原有的数据库文件备份,安装时不要指定原来的数据库数据目录。待安装完成后再附加数据库

2、再次安装时会遇到“以前进行的程序创建了挂起的文件操作,运行安装程序前,必须重新启动  ”,其实可以不重启动的:

  • 在开始->运行中输入regedit  
  • 到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session   Manager   位置  
  • 在右边窗口右击PendingFileRenameOperations,选择删除,然后确认  

2010年4月17日星期六

新版090论坛广告过滤脚本

 

// ==UserScript==
// @name           090广告过滤器 greasemonkey脚本
// @namespace      Filter::GM
// @include        http://bbs.wm090.com/*
// ==/UserScript==
(function(){
    var xpath = "//div[contains(@id,'ad_') or contains(@id,'Adv')]";
    remove(xpath);

    function remove(xpath){
        var el;
        var els = document.evaluate(xpath, document, null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
        for (var i = 0; i < els.snapshotLength; i++) {
          el = els.snapshotItem(i);
          el.parentNode.removeChild(el);
        }
    }
}) ();

今天很囧很郁闷

 

一、囧事

囧,为了控制小孩上网,在宽带路由器中设置了MAC地址白名单,自己笔记本一直无线上网,今天想起用网线上网了,结果插上后网络不通,思考良久,才发现了原因所在。

这已经是第二次发生了,上次是一个iphone上wifi上网,结果死活上不了。。。

 

二、郁闷事

客户说系统用不了很慢。于是我今天去了,原来是他们换过主板但是没重新安装系统,结果新主板没win2000的驱动,又换回旧主板就出问题了。只好重新安装操作系统,好不容易把那块烂主板的网卡、声卡驱动程序全部找齐安装上。

发现没有SQL Server 2000的光盘,从网上下载了一个,花了一个小时,结果不能用。然后再下载,要3小时才行。明天还得去重新来过。

真是郁闷!

2010年4月16日星期五

线程安全的数据库连接管理器

package cn.cslg.xk.util;
/**
* @author 李庆
*@Email Kingstarforever@gmail.com
*@dtaeTime 2010-4-2
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

public class ConnectionManger {

    private static ThreadLocal<Connection> connectionHoder = new ThreadLocal<Connection>();

    public static Connection getConnection() {
        Connection conn = connectionHoder.get();
        if (conn == null) {
            try {
                conn = JndiBean.getConnection();
                connectionHoder.set(conn);
                System.out.println(conn);
            } catch (Exception e) {
            e.printStackTrace();
                //throw new AppException("系统出现故障,请联系管理员!");
            }
        }
        return conn;
    }

    public static void closeConnection() {
        Connection conn = connectionHoder.get();
        if (conn != null) {
            try {
                conn.close();
                connectionHoder.remove();
            } catch (SQLException e) {
            }

        }
    }

    public static void rollback() {
        try {
            Connection conn = connectionHoder.get();
            if (conn != null) {
                conn.rollback();
            }
        } catch (SQLException e) {
        }
    }

    public static void setCommit(boolean b) {
        try {
            Connection conn = connectionHoder.get();
            if (conn != null) {
                conn.setAutoCommit(b);
            }
        } catch (SQLException e) {
        }

    }

    public static void Commit() {
        try {
            Connection conn = connectionHoder.get();
            if (conn != null) {
                conn.commit();
            }
        } catch (SQLException e) {
        }

    }

    public static void closePs(PreparedStatement ps) {

        try {
            if (ps != null) {
                ps.close();
            }
        } catch (SQLException e) {
        }
    }

    public static void closeRs(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
        }
    }

    public static void closeStatment(Statement s) {

        try {
            if (s != null) {
                s.close();
            }
        } catch (SQLException e) {
        }
    }
}

2010年4月15日星期四

昨天帮忙解决的网络问题

Untitled-1

使用mysql++对mysql数据库表进行增删改查操作

和mysql connector/c++相比,只要在开始时设置好字符集,之后不需要进行转码操作。
conn.set_option(new mysqlpp::SetCharsetNameOption("gbk"));

//查询方法1
vector<mysqlpp::Row> v;
        query.storein(v);

        cout.setf(ios::left);
        cout<<setw(10)<<"username"<<setw(10)<<"password"<<endl;

        for (vector<mysqlpp::Row>::iterator it = v.begin(); it != v.end(); ++it) {
            cout<<setw(10)<<it->at(0)<<setw(10)<<it->at(1)<< endl;
        }

//查询方法2
mysqlpp::StoreQueryResult res = query.store();
        if(res){
            cout.setf(ios::left);
            cout<<setw(10)<<"username"<<setw(10)<<"password"<<endl;
            for (size_t i = 0; i < res.num_rows(); ++i)
                cout << setw(10) << res[i]["username"] << setw(10) << res[i]["password"] << endl;
        }else {
            cerr << "Failed to get stock table: " << query.error() << endl;
            return 1;
        }

//修改记录
query<<"update users set password='hello' where username='沈健'";
query.execute();

//插入
query<<"insert into users(username,password) values('飞翔','123456')";
query.execute();

//删除记录
query<<"delete from users where username='遨游'";
query.execute();

2010年4月13日星期二

使用mysql connector/c++对mysql进行增删改查操作

 

//查询
result = stmt->executeQuery("select * from users"); 
while(result->next()) 

    string username = result->getString("username"); 
    string password = result->getString("password"); 
    cout << utf2ansi(username) << " : " << password << endl;

//插入
string sql="insert into users(username,password) values('" + ansi2utf8(string("飞翔"))+ "','asdfgh')";
stmt->executeUpdate(sql);

//删除
sql="delete from users where id=4";
stmt->executeUpdate(sql);

//修改
sql="update users set password='hello' where username='" + ansi2utf8(string("沈健")) +"'";;
stmt->executeUpdate(sql);

查出来的数据需要转换成ANSI编码,传给mysql服务器的数据需要转换为unicode编码(mysql数据库中所建的数据库、表和字段采用utf-8编码)

下面是相应的转换函数

//将标准string(unicode)转换为string(ansi)
string utf2ansi(const string &src){
    int nLen = MultiByteToWideChar( CP_UTF8, 0, src.data(), -1, NULL, NULL );
    LPWSTR lpwsz = new WCHAR[nLen];
    MultiByteToWideChar( CP_UTF8, 0, src.data(), -1, lpwsz, nLen );

    int nLen1 = WideCharToMultiByte( CP_ACP, 0, lpwsz, nLen, NULL, NULL, NULL, NULL );
    LPSTR lpsz = new CHAR[nLen1];
    WideCharToMultiByte( CP_ACP, 0, lpwsz, nLen, lpsz, nLen1, NULL, NULL );
    string str(lpsz);
    delete []lpsz;
    return str;
}

//将标准string(ansi)转换为string(unicode)
string ansi2utf8(const string &src){
    int nLen = MultiByteToWideChar( CP_ACP, 0, src.data(), -1, NULL, NULL );
    LPWSTR lpwsz = new WCHAR[nLen];
    MultiByteToWideChar( CP_ACP, 0, src.data(), -1, lpwsz, nLen );

    int nLen1 = WideCharToMultiByte( CP_UTF8, 0, lpwsz, nLen, NULL, NULL, NULL, NULL );
    LPSTR lpsz = new CHAR[nLen1];
    WideCharToMultiByte( CP_UTF8, 0, lpwsz, nLen, lpsz, nLen1, NULL, NULL );
    string str(lpsz);
    delete []lpsz;
    return str;
}

2010年4月12日星期一

使用mysql connector/C++进行数据库编程的几个注意点

 

1、环境准备

下载mysql connector/c++并解压

2、在项目设置中

(1)项目属性,配置属性,C/C++,常规,附加包含目录:将第一步解压出来的目录中的include和include/cppconn添加进去

(2)项目属性,配置属性,链接器,输入,附加依赖项:mysqlcppconn.lib mysqlcppconn-static.lib libmysql.lib

(3)项目属性,配置属性,链接器,常规,附加库目录:将第一步解压出来的目录中的lib目录添加进去

(4)编译项目

(5)运行时,需要将mysqlcppconn.dll libmysql.dll复制到生成可执行文件的目录下

使用mysql++进行数据库编程时的几个注意点

 

1、mysql++的准备

http://tangentsoft.net/mysql++/releases/mysql++-3.0.9.tar.gz下载源代码,解压缩后,用VS2008打开VS2008文件夹中的解决方案文件(sln),注意此时要用到mysql的include目录和libmysql.lib,如果你在本机上安装了mysql数据库,则可以从安装目录中找到,将include和libmysql.lib添加到项目的额外的包含文件目录和库文件目录中,并在链接选项的输入中键入 libmysql.lib。如果不想安装mysql服务器,也可以下载mysql connector C driver,其中也包含了相应的文件。

设置好之后编译mysql++,如果无误。双击运行其中的install.hta完成安装过程。

2、使用mysql++的项目的相关设置

(1)添加额外的包含文件目录:

  • mysql++的include目录
  • mysql server的include目录

(2)添加额外的库文件目录,并将 libmysql.lib mysqlpp_d.lib 加入到 链接器|输入|附加依赖项中

(3)编译代码

(4)运行时要将libmysql.dll和mysqlpp_d.dll放到生成的可执行文件的文件夹下

使用mysql++连接mysql数据库进行数据查询

#include <iostream>
#include <iomanip>
#include <mysql++.h>
#include <stdio.h>
#include <Windows.h>

using namespace std;
using namespace mysqlpp;

int main(){

    //在取得连接之前首先设置字符编码
    mysqlpp::Connection conn(false);
    conn.set_option(new mysqlpp::SetCharsetNameOption("GBK"));
    conn.connect("mydb","127.0.0.1","user","password",3306);

    mysqlpp::Query query=conn.query();
    vector<mysqlpp::Row> v;
    query<<"select * from nations";
    query.storein(v);

    cout.setf(ios::left);
    cout<<setw(10)<<"Nation"<<setw(10)<<"Notation"<<endl;
    for (vector<mysqlpp::Row>::iterator it = v.begin(); it != v.end(); ++it) {
        cout<<setw(10)<<it->at(0)<<setw(10)<<it->at(1)<< endl;
    }
    getchar();
    return 0;
}

2010年4月9日星期五

使用mysql connector/C++查询mysql数据库

#include <iomanip>
#include <mysql_connection.h> 
#include <mysql_driver.h>
#include <cppconn/driver.h> 
#include <cppconn/statement.h> 
#include <cppconn/exception.h>
#include <cppconn/resultset.h>

using namespace std;

void RunConnectMySQL()  

    try{
        //sql::Driver *driver;
        sql::mysql::MySQL_Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt; 
        sql::ResultSet *result; 

        //driver = get_driver_instance();
        driver=sql::mysql::get_mysql_driver_instance();

        con = driver->connect("tcp://127.0.0.1:3306", "user", "password"); 

        stmt = con->createStatement(); 
        stmt->execute("use doclibexp"); 

        result = stmt->executeQuery("select * from nations"); 

        while(result->next()) 
        { 
            string nation = result->getString("Nation"); 
            string notation = result->getString("Notation"); 
            cout << nation << " : " << notation << endl;
        } 
        delete stmt; 
        delete con;

    }catch (sql::SQLException &e) {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }

int main(int argc, char **argv) 

    RunConnectMySQL(); 
    getchar(); 
    return 0; 

C++中的标准string由unicode转换为ansi[windows环境]

#include <windows.h>

string utf2ansi(const string &src){
    int nLen = MultiByteToWideChar( CP_UTF8, 0, src.data(), -1, NULL, NULL );
    //得到UTF8编码的字符串长度
    LPWSTR lpwsz = new WCHAR[nLen];
    MultiByteToWideChar( CP_UTF8, 0, src.data(), -1, lpwsz, nLen ); 

    int nLen1 = WideCharToMultiByte( CP_ACP, 0, lpwsz, nLen, NULL, NULL, NULL, NULL );
    LPSTR lpsz = new CHAR[nLen1];
    WideCharToMultiByte( CP_ACP, 0, lpwsz, nLen, lpsz, nLen1, NULL, NULL );
    string str(lpsz);
    delete []lpsz;
    return str;
}

2010年4月7日星期三

win7+vs2008编译openssl

转自:http://chang.baidu.com/mycbg/snap/84d42ff18c21541db037b42b.html

==================================================================================

在编译OpenSSL前,需要正确安装Perl,因为在编译OpenSSL时需要使用到该程序。

下载最新版本的Perl:http://downloads.activestate.com/ActivePerl/Windows/5.8/ActivePerl-5.8.8.822-MSWin32-x86-280952.zip。然后安装之。

下载最新版本的OpenSSL:http://www.openssl.org/source/openssl-0.9.8g.tar.gz

然后将源码释放的c:\openssl-0.9.8g目录中。

进入openssl源码目录。
cd c:\openssl-0.9.8.g

以下为参照该目录下的文件INSTALL.W32的执行过程:

在命令行下先执行一次一遍设置nmake,ml,cl等命令的路径:
C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat

运行configure:
perl Configure VC-WIN32 –prefix=c:/openssl

创建Makefile文件:
ms\do_ms

编译动态库:
nmake -f ms\ntdll.mak
编译静态库:
nmake -f ms\nt.mak

测试动态库:
nmake -f ms\ntdll.mak test
测试静态库:
nmake -f ms\nt.mak test

安装动态库:
nmake -f ms\ntdll.mak install
安装静态库:
nmake -f ms\nt.mak install

清除上次动态库的编译,以便重新编译:
nmake -f ms\ntdll.mak clean
清除上次静态库的编译,以便重新编译:
nmake -f ms\nt.mak clean

error C2664: 'GetModuleFileNameW' : cannot convert parameter 2 from 'char [260]' to 'LPWCH'

 

在VC6中可以编译成功的代码在VS2005,VS2008中编译出错。

原因:LPWCH是指long pointer to wide char,VS2005和2008默认使用unicode编码,而vc6默认使用多字节编码。

解决方法:

(1)将char改为wchar_t,但是会引起很多问题(在既有代码中)

(2)在项目的属性>配置属性>常规中,将“字符集”由“使用Unicode字符集”改为“使用多字节字符集”