锘??xml version="1.0" encoding="utf-8" standalone="yes"?>青青草原亚洲视频,亚洲伊人久久大香线蕉在观 ,最新亚洲成av人免费看http://www.tkk7.com/sealyu/archive/2010/11/29/339341.htmlsealsealMon, 29 Nov 2010 10:49:00 GMThttp://www.tkk7.com/sealyu/archive/2010/11/29/339341.htmlhttp://www.tkk7.com/sealyu/comments/339341.htmlhttp://www.tkk7.com/sealyu/archive/2010/11/29/339341.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/339341.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/339341.htmlI found out today that MS Sql server seems to handle Unicode in a very special way. Instead of having some support a database or table level, each Unicode column have to be created as “national”. That is be either nchar, nvarchar or ntext.

Ms SQL Server 2005 seems to go one step further by announcing future deprecation for ntext, text and image types.

From Sql Server 2005 notes:

ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.”

When working with Hibernate it seems there is no dialect to handle Unicode integration properly. You have to get down and write a custom dialect that maps to the new data types.

/**
* Unicode support in SQL Server
*
* @author icocan
*/
public class UnicodeSQLServerDialect extends SQLServerDialect {

public UnicodeSQLServerDialect() {
super();

// Use Unicode Characters
registerColumnType(Types.VARCHAR, 255, "nvarchar($l)");
registerColumnType(Types.CHAR, "nchar(1)");
registerColumnType(Types.CLOB, "nvarchar(max)");

// Microsoft SQL Server 2000 supports bigint and bit
registerColumnType(Types.BIGINT, "bigint");
registerColumnType(Types.BIT, "bit");
}
}
read more ...

You have to write your own SQLServerDialect class, it looks something like this:
publicclassSQLServerNativeDialectextendsSQLServerDialect{
     publicSQLServerNativeDialect(){
         super();
         registerColumnType(Types.VARCHAR,"nvarchar($l)");
         registerColumnType(Types.CLOB,"nvarchar(max)");
     }

    publicString getTypeName(int code,int length,int precision,int scale)throwsHibernateException{
        if(code !=2005){
            returnsuper.getTypeName(code, length, precision, scale);
        }else{
            return"ntext";
        }
    }
}

This class maps Hibernate's types to SQL types, so the class will map the nvarchar(max) SQL Data Type to Hibernate's CLOB data type.

The getTypeName method is used to return "ntext" when Hibernate asks about the data type with code 2005 (which looks like it's the nvarchar(max) data type).

Finally, you need to change your hibernate persistence dialect to this new SQLServerDialect class, which allows hibernate to translate data types into SQL data types.




seal 2010-11-29 18:49 鍙戣〃璇勮
]]>
nvarchar涓巚archar鐨勫尯鍒?/title><link>http://www.tkk7.com/sealyu/archive/2010/11/29/339293.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Mon, 29 Nov 2010 02:51:00 GMT</pubDate><guid>http://www.tkk7.com/sealyu/archive/2010/11/29/339293.html</guid><wfw:comment>http://www.tkk7.com/sealyu/comments/339293.html</wfw:comment><comments>http://www.tkk7.com/sealyu/archive/2010/11/29/339293.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/sealyu/comments/commentRss/339293.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/sealyu/services/trackbacks/339293.html</trackback:ping><description><![CDATA[<p>nvarchar涓巚archar鐨勫尯鍒?/p> <p>varchar[(n)]   <br />   闀垮害涓?  n   涓瓧鑺傜殑鍙彉闀垮害涓旈潪   Unicode   鐨勫瓧絎︽暟鎹俷   蹇呴』鏄竴涓粙浜?  1   鍜?  8,000   涔嬮棿鐨勬暟鍊箋傚瓨鍌ㄥぇ灝忎負(fù)杈撳叆鏁版嵁鐨勫瓧鑺傜殑瀹為檯闀垮害錛岃屼笉鏄?  n   涓瓧鑺傘傛墍杈撳叆鐨勬暟鎹瓧絎﹂暱搴﹀彲浠ヤ負(fù)闆躲倂archar   鍦?  SQL-92   涓殑鍚屼箟璇嶄負(fù)   char   varying   鎴?  character   varying銆?  <br />     <br />   nvarchar(n)   <br />   鍖呭惈   n   涓瓧絎︾殑鍙彉闀垮害   Unicode   瀛楃鏁版嵁銆俷   鐨勫煎繀欏諱粙浜?  1   涓?  4,000   涔嬮棿銆傚瓧鑺傜殑瀛樺偍澶у皬鏄墍杈撳叆瀛楃涓暟鐨勪袱鍊嶃傛墍杈撳叆鐨勬暟鎹瓧絎﹂暱搴﹀彲浠ヤ負(fù)闆躲俷varchar   鍦?  SQL-92   涓殑鍚屼箟璇嶄負(fù)   national   char   varying   鍜?  national   character   varying銆?nbsp;  </p> <p>閫氫織涓榛炲氨鏄痸archar閬╁悎杓稿叆鑻辨枃鍜屾暩瀛楋紝nvarchar涓鑸敤鍋氫腑鏂囨垨鍏跺畠瑾炶█鐨勮幾鍏ワ紝閫欐ǎ鍒板垾鐨勮獮緋諱笉鏈冨嚭鐝句簜紕?))</p> <img src ="http://www.tkk7.com/sealyu/aggbug/339293.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/sealyu/" target="_blank">seal</a> 2010-11-29 10:51 <a href="http://www.tkk7.com/sealyu/archive/2010/11/29/339293.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>sql server 鍒ゆ柇琛?瑙嗗浘/瀛樺偍榪囩▼鏄惁瀛樺湪錛堣漿錛?/title><link>http://www.tkk7.com/sealyu/archive/2010/05/28/322100.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 27 May 2010 18:40:00 GMT</pubDate><guid>http://www.tkk7.com/sealyu/archive/2010/05/28/322100.html</guid><wfw:comment>http://www.tkk7.com/sealyu/comments/322100.html</wfw:comment><comments>http://www.tkk7.com/sealyu/archive/2010/05/28/322100.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/sealyu/comments/commentRss/322100.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/sealyu/services/trackbacks/322100.html</trackback:ping><description><![CDATA[--濡傛灉鏄疄琛ㄥ彲浠ョ敤<br /> if exists (select * from sysobjects where id = object_id(N'[dbo].[琛ㄥ悕]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) <br /> drop table [dbo].[琛ㄥ悕] <p>--濡傛灉鏄復(fù)鏃惰〃鍙互鐢?璇存槑,濡傛灉鐢ㄦ煡鎵懼疄琛ㄦ柟娉曟潵鎵撲復(fù)鏃惰〃浼?xì)鎵句笉鍒?鍙戝竷鍖哄埆瀵逛唬.)<br /> if object_id('tempdb..##temp') is not null<br />    drop table ##temp</p> <p>--鍒ゆ柇瀛樺偍榪囩▼鏄惁瀛樺湪<br /> if exists(select 1 from sysobjects where id=object_id('鎵鏈夎?瀛樺偍榪囩▼鍚?) and xtype='P')   <br /> print '瀛樺湪'   <br /> else   <br /> print '涓嶅瓨鍦?</p> <p>--鍒ゆ柇瑙嗗浘鏄惁瀛樺湪<br /> --SQL Server 2000 <br /> IF EXISTS (SELECT * FROM sysviews WHERE object_id = '[dbo].[瑙嗗浘鍚峕' <br /> --SQL Server 2005 <br /> IF EXISTS (SELECT * FROM sys.views WHERE object_id = '[dbo].[瑙嗗浘鍚峕'</p> <p><br /> /*<br /> sysObjects ( <br /> Name sysname,      --object 鍚嶇О <br /> id   int,          --object id <br /> xtype char(2),     -- object 綾誨瀷   <br /> type char(2),     -- Object 綾誨瀷錛堜笌xtype 浼間箮涓妯′竴鏍鳳紵 鏈夌偣閮侀椃…錛? <br /> uid   smallint,     -- object 鎵鏈夎呯殑ID <br /> ...                --鍏朵粬鐨勫瓧孌典笉甯哥敤鍒般?nbsp;  <br /> )</p> <p>sysobjects鐨剎type 浠h〃鐨勫璞$被鍨嬨傚彲浠ユ槸涓嬪垪瀵硅薄綾誨瀷涓殑涓縐嶏細(xì) <br /> C = CHECK 綰︽潫 <br /> D = 榛樿鍊兼垨 DEFAULT 綰︽潫 <br /> F = FOREIGN KEY 綰︽潫 <br /> L = 鏃ュ織 <br /> FN = 鏍囬噺鍑芥暟 <br /> IF = 鍐呭祵琛ㄥ嚱鏁?<br /> P = 瀛樺偍榪囩▼ <br /> PK = PRIMARY KEY 綰︽潫錛堢被鍨嬫槸 K錛?<br /> RF = 澶嶅埗絳涢夊瓨鍌ㄨ繃紼?<br /> S = 緋葷粺琛?<br /> TF = 琛ㄥ嚱鏁?<br /> TR = 瑙﹀彂鍣?<br /> U = 鐢ㄦ埛琛?<br /> UQ = UNIQUE 綰︽潫錛堢被鍨嬫槸 K錛?<br /> V = 瑙嗗浘 <br /> X = 鎵╁睍瀛樺偍榪囩▼ </p> <p><br /> object_id鍜宒ata_object_id閮芥槸琛ㄧず鏁版嵁搴撳璞$殑鍞竴鏍囧織銆?br /> <br /> object_id鏄暟鎹簱瀵硅薄鐨勯昏緫id錛宒ata_object_id鏄暟鎹簱瀵硅薄鐨勭墿鐞唅d銆?br /> <br /> 濡傛灉涓浜沷bject娌℃湁鐗╃悊灞炴х殑璇濋偅瀹冨氨涓嶅瓨鍦╠ata_object_id錛屼緥濡俻rocedure,function,package,data type,db link,mv瀹氫箟錛寁iew瀹氫箟錛屼復(fù)鏃惰〃錛屽垎鍖鴻〃瀹氫箟絳夌瓑榪欎簺object閮芥槸娌℃湁瀵瑰簲鐫鏌愪釜segment錛屽洜姝ゅ畠浠殑data_object_id 閮戒負(fù)絀恒?br /> <br /> 褰撲竴涓〃寤虹珛鐨勬椂鍊欙紝浠栫殑object_id 鍜? data_object_id鏄浉絳夌殑銆傚綋琛╩ove鍜宼runcate鍚巇ata_object_id浼?xì)鍙戠敓鍙樺寲銆備慨鏀硅〃緇撴瀯涓嶄細(xì)鏇存敼銆?br /> <br /> select object_id,data_object_id from user_objects where object_name=’T';<br /> OBJECT_ID DATA_OBJECT_ID<br /> 鈥斺斺? 鈥斺斺斺?#8211;<br /> 63053 63464<br /> <br /> SELECT HEADER_FILE,HEADER_BLOCK,BLOCKS FROM DBA_SEGMENTS WHERE SEGMENT_NAME=’T’ AND OWNER=’TEST’;<br /> HEADER_FILE HEADER_BLOCK BLOCKS<br /> 鈥斺斺?#8211; 鈥斺斺斺?鈥斺斺?<br /> 4 467 8</p> <p>*/</p> <p><br /> SELECT * FROM sysobjects WHERE xtype='U' AND id=OBJECT_ID('Booking')</p> <img src ="http://www.tkk7.com/sealyu/aggbug/322100.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/sealyu/" target="_blank">seal</a> 2010-05-28 02:40 <a href="http://www.tkk7.com/sealyu/archive/2010/05/28/322100.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>MySQLdb User's Guidehttp://www.tkk7.com/sealyu/archive/2010/05/17/321217.htmlsealsealMon, 17 May 2010 15:56:00 GMThttp://www.tkk7.com/sealyu/archive/2010/05/17/321217.htmlhttp://www.tkk7.com/sealyu/comments/321217.htmlhttp://www.tkk7.com/sealyu/archive/2010/05/17/321217.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/321217.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/321217.html

Contents

Introduction

MySQLdb is an thread-compatible interface to the popular MySQL database server that provides the Python database API.

Installation

The README file has complete installation instructions.

_mysql

If you want to write applications which are portable across databases, use MySQLdb, and avoid using this module directly. _mysql provides an interface which mostly implements the MySQL C API. For more information, see the MySQL documentation. The documentation for this module is intentionally weak because you probably should use the higher-level MySQLdb module. If you really need it, use the standard MySQL docs and transliterate as necessary.

MySQL C API translation

The MySQL C API has been wrapped in an object-oriented way. The only MySQL data structures which are implemented are the MYSQL (database connection handle) and MYSQL_RES (result handle) types. In general, any function which takes MYSQL *mysql as an argument is now a method of the connection object, and any function which takes MYSQL_RES *result as an argument is a method of the result object. Functions requiring none of the MySQL data structures are implemented as functions in the module. Functions requiring one of the other MySQL data structures are generally not implemented. Deprecated functions are not implemented. In all cases, the mysql_ prefix is dropped from the name. Most of the conn methods listed are also available as MySQLdb Connection object methods. Their use is non-portable.

MySQL C API function mapping

C API _mysql
mysql_affected_rows() conn.affected_rows()
mysql_autocommit() conn.autocommit()
mysql_character_set_name() conn.character_set_name()
mysql_close() conn.close()
mysql_commit() conn.commit()
mysql_connect() _mysql.connect()
mysql_data_seek() result.data_seek()
mysql_debug() _mysql.debug()
mysql_dump_debug_info conn.dump_debug_info()
mysql_escape_string() _mysql.escape_string()
mysql_fetch_row() result.fetch_row()
mysql_get_character_set_info() conn.get_character_set_info()
mysql_get_client_info() _mysql.get_client_info()
mysql_get_host_info() conn.get_host_info()
mysql_get_proto_info() conn.get_proto_info()
mysql_get_server_info() conn.get_server_info()
mysql_info() conn.info()
mysql_insert_id() conn.insert_id()
mysql_num_fields() result.num_fields()
mysql_num_rows() result.num_rows()
mysql_options() various options to _mysql.connect()
mysql_ping() conn.ping()
mysql_query() conn.query()
mysql_real_connect() _mysql.connect()
mysql_real_query() conn.query()
mysql_real_escape_string() conn.escape_string()
mysql_rollback() conn.rollback()
mysql_row_seek() result.row_seek()
mysql_row_tell() result.row_tell()
mysql_select_db() conn.select_db()
mysql_set_character_set() conn.set_character_set()
mysql_ssl_set() ssl option to _mysql.connect()
mysql_stat() conn.stat()
mysql_store_result() conn.store_result()
mysql_thread_id() conn.thread_id()
mysql_thread_safe_client() conn.thread_safe_client()
mysql_use_result() conn.use_result()
mysql_warning_count() conn.warning_count()
CLIENT_* MySQLdb.constants.CLIENT.*
CR_* MySQLdb.constants.CR.*
ER_* MySQLdb.constants.ER.*
FIELD_TYPE_* MySQLdb.constants.FIELD_TYPE.*
FLAG_* MySQLdb.constants.FLAG.*

Some _mysql examples

Okay, so you want to use _mysql anyway. Here are some examples.

The simplest possible database connection is:

import _mysql
db=_mysql.connect()

This creates a connection to the MySQL server running on the local machine using the standard UNIX socket (or named pipe on Windows), your login name (from the USER environment variable), no password, and does not USE a database. Chances are you need to supply more information.:

db=_mysql.connect("localhost","joebob","moonpie","thangs")

This creates a connection to the MySQL server running on the local machine via a UNIX socket (or named pipe), the user name "joebob", the password "moonpie", and selects the initial database "thangs".

We haven't even begun to touch upon all the parameters connect() can take. For this reason, I prefer to use keyword parameters:

db=_mysql.connect(host="localhost",user="joebob",
passwd="moonpie",db="thangs")

This does exactly what the last example did, but is arguably easier to read. But since the default host is "localhost", and if your login name really was "joebob", you could shorten it to this:

db=_mysql.connect(passwd="moonpie",db="thangs")

UNIX sockets and named pipes don't work over a network, so if you specify a host other than localhost, TCP will be used, and you can specify an odd port if you need to (the default port is 3306):

db=_mysql.connect(host="outhouse",port=3307,passwd="moonpie",db="thangs")

If you really had to, you could connect to the local host with TCP by specifying the full host name, or 127.0.0.1.

Generally speaking, putting passwords in your code is not such a good idea:

db=_mysql.connect(host="outhouse",db="thangs",read_default_file="~/.my.cnf")

This does what the previous example does, but gets the username and password and other parameters from ~/.my.cnf (UNIX-like systems). Read about option files for more details.

So now you have an open connection as db and want to do a query. Well, there are no cursors in MySQL, and no parameter substitution, so you have to pass a complete query string to db.query():

db.query("""SELECT spam, eggs, sausage FROM breakfast
WHERE price < 5""")

There's no return value from this, but exceptions can be raised. The exceptions are defined in a separate module, _mysql_exceptions, but _mysql exports them. Read DB API specification PEP-249 to find out what they are, or you can use the catch-all MySQLError.

At this point your query has been executed and you need to get the results. You have two options:

r=db.store_result()
# ...or...
r=db.use_result()

Both methods return a result object. What's the difference? store_result() returns the entire result set to the client immediately. If your result set is really large, this could be a problem. One way around this is to add a LIMIT clause to your query, to limit the number of rows returned. The other is to use use_result(), which keeps the result set in the server and sends it row-by-row when you fetch. This does, however, tie up server resources, and it ties up the connection: You cannot do any more queries until you have fetched all the rows. Generally I recommend using store_result() unless your result set is really huge and you can't use LIMIT for some reason.

Now, for actually getting real results:

>>> r.fetch_row()
(('3','2','0'),)

This might look a little odd. The first thing you should know is, fetch_row() takes some additional parameters. The first one is, how many rows (maxrows) should be returned. By default, it returns one row. It may return fewer rows than you asked for, but never more. If you set maxrows=0, it returns all rows of the result set. If you ever get an empty tuple back, you ran out of rows.

The second parameter (how) tells it how the row should be represented. By default, it is zero which means, return as a tuple. how=1 means, return it as a dictionary, where the keys are the column names, or table.column if there are two columns with the same name (say, from a join). how=2 means the same as how=1 except that the keys are always table.column; this is for compatibility with the old Mysqldb module.

OK, so why did we get a 1-tuple with a tuple inside? Because we implicitly asked for one row, since we didn't specify maxrows.

The other oddity is: Assuming these are numeric columns, why are they returned as strings? Because MySQL returns all data as strings and expects you to convert it yourself. This would be a real pain in the ass, but in fact, _mysql can do this for you. (And MySQLdb does do this for you.) To have automatic type conversion done, you need to create a type converter dictionary, and pass this to connect() as the conv keyword parameter.

The keys of conv should be MySQL column types, which in the C API are FIELD_TYPE_*. You can get these values like this:

from MySQLdb.constants import FIELD_TYPE

By default, any column type that can't be found in conv is returned as a string, which works for a lot of stuff. For our purposes, we probably want this:

my_conv = { FIELD_TYPE.LONG: int }

This means, if it's a FIELD_TYPE_LONG, call the builtin int() function on it. Note that FIELD_TYPE_LONG is an INTEGER column, which corresponds to a C long, which is also the type used for a normal Python integer. But beware: If it's really an UNSIGNED INTEGER column, this could cause overflows. For this reason, MySQLdb actually uses long() to do the conversion. But we'll ignore this potential problem for now.

Then if you use db=_mysql.connect(conv=my_conv...), the results will come back ((3, 2, 0),), which is what you would expect.

MySQLdb

MySQLdb is a thin Python wrapper around _mysql which makes it compatible with the Python DB API interface (version 2). In reality, a fair amount of the code which implements the API is in _mysql for the sake of efficiency.

The DB API specification PEP-249 should be your primary guide for using this module. Only deviations from the spec and other database-dependent things will be documented here.

Functions and attributes

Only a few top-level functions and attributes are defined within MySQLdb.

connect(parameters...)

Constructor for creating a connection to the database. Returns a Connection Object. Parameters are the same as for the MySQL C API. In addition, there are a few additional keywords that correspond to what you would pass mysql_options() before connecting. Note that some parameters must be specified as keyword arguments! The default value for each parameter is NULL or zero, as appropriate. Consult the MySQL documentation for more details. The important parameters are:

host
name of host to connect to. Default: use the local host via a UNIX socket (where applicable)
user
user to authenticate as. Default: current effective user.
passwd
password to authenticate with. Default: no password.
db
database to use. Default: no default database.
port
TCP port of MySQL server. Default: standard port (3306).
unix_socket
location of UNIX socket. Default: use default location or TCP for remote hosts.
conv
type conversion dictionary. Default: a copy of MySQLdb.converters.conversions
compress
Enable protocol compression. Default: no compression.
connect_timeout
Abort if connect is not completed within given number of seconds. Default: no timeout (?)
named_pipe
Use a named pipe (Windows). Default: don't.
init_command
Initial command to issue to server upon connection. Default: Nothing.
read_default_file
MySQL configuration file to read; see the MySQL documentation for mysql_options().
read_default_group
Default group to read; see the MySQL documentation for mysql_options().
cursorclass
cursor class that cursor() uses, unless overridden. Default: MySQLdb.cursors.Cursor. This must be a keyword parameter.
use_unicode

If True, CHAR and VARCHAR and TEXT columns are returned as Unicode strings, using the configured character set. It is best to set the default encoding in the server configuration, or client configuration (read with read_default_file). If you change the character set after connecting (MySQL-4.1 and later), you'll need to put the correct character set name in connection.charset.

If False, text-like columns are returned as normal strings, but you can always write Unicode strings.

This must be a keyword parameter.

charset

If present, the connection character set will be changed to this character set, if they are not equal. Support for changing the character set requires MySQL-4.1 and later server; if the server is too old, UnsupportedError will be raised. This option implies use_unicode=True, but you can override this with use_unicode=False, though you probably shouldn't.

If not present, the default character set is used.

This must be a keyword parameter.

sql_mode

If present, the session SQL mode will be set to the given string. For more information on sql_mode, see the MySQL documentation. Only available for 4.1 and newer servers.

If not present, the session SQL mode will be unchanged.

This must be a keyword parameter.

ssl
This parameter takes a dictionary or mapping, where the keys are parameter names used by the mysql_ssl_set MySQL C API call. If this is set, it initiates an SSL connection to the server; if there is no SSL support in the client, an exception is raised. This must be a keyword parameter.
apilevel
String constant stating the supported DB API level. '2.0'
threadsafety

Integer constant stating the level of thread safety the interface supports. This is set to 1, which means: Threads may share the module.

The MySQL protocol can not handle multiple threads using the same connection at once. Some earlier versions of MySQLdb utilized locking to achieve a threadsafety of 2. While this is not terribly hard to accomplish using the standard Cursor class (which uses mysql_store_result()), it is complicated by SSCursor (which uses mysql_use_result(); with the latter you must ensure all the rows have been read before another query can be executed. It is further complicated by the addition of transactions, since transactions start when a cursor execute a query, but end when COMMIT or ROLLBACK is executed by the Connection object. Two threads simply cannot share a connection while a transaction is in progress, in addition to not being able to share it during query execution. This excessively complicated the code to the point where it just isn't worth it.

The general upshot of this is: Don't share connections between threads. It's really not worth your effort or mine, and in the end, will probably hurt performance, since the MySQL server runs a separate thread for each connection. You can certainly do things like cache connections in a pool, and give those connections to one thread at a time. If you let two threads use a connection simultaneously, the MySQL client library will probably upchuck and die. You have been warned.

For threaded applications, try using a connection pool. This can be done using the Pool module.

charset
The character set used by the connection. In MySQL-4.1 and newer, it is possible (but not recommended) to change the connection's character set with an SQL statement. If you do this, you'll also need to change this attribute. Otherwise, you'll get encoding errors.
paramstyle

String constant stating the type of parameter marker formatting expected by the interface. Set to 'format' = ANSI C printf format codes, e.g. '...WHERE name=%s'. If a mapping object is used for conn.execute(), then the interface actually uses 'pyformat' = Python extended format codes, e.g. '...WHERE name=%(name)s'. However, the API does not presently allow the specification of more than one style in paramstyle.

Note that any literal percent signs in the query string passed to execute() must be escaped, i.e. %%.

Parameter placeholders can only be used to insert column values. They can not be used for other parts of SQL, such as table names, statements, etc.

conv

A dictionary or mapping which controls how types are converted from MySQL to Python and vice versa.

If the key is a MySQL type (from FIELD_TYPE.*), then the value can be either:

  • a callable object which takes a string argument (the MySQL value),' returning a Python value
  • a sequence of 2-tuples, where the first value is a combination of flags from MySQLdb.constants.FLAG, and the second value is a function as above. The sequence is tested until the flags on the field match those of the first value. If both values are None, then the default conversion is done. Presently this is only used to distinquish TEXT and BLOB columns.

If the key is a Python type or class, then the value is a callable Python object (usually a function) taking two arguments (value to convert, and the conversion dictionary) which converts values of this type to a SQL literal string value.

This is initialized with reasonable defaults for most types. When creating a Connection object, you can pass your own type converter dictionary as a keyword parameter. Otherwise, it uses a copy of MySQLdb.converters.conversions. Several non-standard types are returned as strings, which is how MySQL returns all columns. For more details, see the built-in module documentation.

Connection Objects

Connection objects are returned by the connect() function.

commit()
If the database and the tables support transactions, this commits the current transaction; otherwise this method successfully does nothing.
rollback()
If the database and tables support transactions, this rolls back (cancels) the current transaction; otherwise a NotSupportedError is raised.
cursor([cursorclass])
MySQL does not support cursors; however, cursors are easily emulated. You can supply an alternative cursor class as an optional parameter. If this is not present, it defaults to the value given when creating the connection object, or the standard Cursor class. Also see the additional supplied cursor classes in the usage section.

There are many more methods defined on the connection object which are MySQL-specific. For more information on them, consult the internal documentation using pydoc.

Cursor Objects

callproc(procname, args)

Calls stored procedure procname with the sequence of arguments in args. Returns the original arguments. Stored procedure support only works with MySQL-5.0 and newer.

Compatibility note: PEP-249 specifies that if there are OUT or INOUT parameters, the modified values are to be returned. This is not consistently possible with MySQL. Stored procedure arguments must be passed as server variables, and can only be returned with a SELECT statement. Since a stored procedure may return zero or more result sets, it is impossible for MySQLdb to determine if there are result sets to fetch before the modified parmeters are accessible.

The parameters are stored in the server as @_*procname*_*n*, where n is the position of the parameter. I.e., if you cursor.callproc('foo', (a, b, c)), the parameters will be accessible by a SELECT statement as @_foo_0, @_foo_1, and @_foo_2.

Compatibility note: It appears that the mere act of executing the CALL statement produces an empty result set, which appears after any result sets which might be generated by the stored procedure. Thus, you will always need to use nextset() to advance result sets.

close()
Closes the cursor. Future operations raise ProgrammingError. If you are using server-side cursors, it is very important to close the cursor when you are done with it and before creating a new one.
info()
Returns some information about the last query. Normally you don't need to check this. If there are any MySQL warnings, it will cause a Warning to be issued through the Python warning module. By default, Warning causes a message to appear on the console. However, it is possible to filter these out or cause Warning to be raised as exception. See the MySQL docs for mysql_info(), and the Python warning module. (Non-standard)
setinputsizes()
Does nothing, successfully.
setoutputsizes()
Does nothing, successfully.
nextset()

Advances the cursor to the next result set, discarding the remaining rows in the current result set. If there are no additional result sets, it returns None; otherwise it returns a true value.

Note that MySQL doesn't support multiple result sets until 4.1.

Some examples

The connect() method works nearly the same as with _mysql:

import MySQLdb
db=MySQLdb.connect(passwd="moonpie",db="thangs")

To perform a query, you first need a cursor, and then you can execute queries on it:

c=db.cursor()
max_price=5
c.execute("""SELECT spam, eggs, sausage FROM breakfast
WHERE price < %s""", (max_price,))

In this example, max_price=5 Why, then, use %s in the string? Because MySQLdb will convert it to a SQL literal value, which is the string '5'. When it's finished, the query will actually say, "...WHERE price < 5".

Why the tuple? Because the DB API requires you to pass in any parameters as a sequence. Due to the design of the parser, (max_price) is interpreted as using algebraic grouping and simply as max_price and not a tuple. Adding a comma, i.e. (max_price,) forces it to make a tuple.

And now, the results:

>>> c.fetchone()
(3L, 2L, 0L)

Quite unlike the _mysql example, this returns a single tuple, which is the row, and the values are properly converted by default... except... What's with the L's?

As mentioned earlier, while MySQL's INTEGER column translates perfectly into a Python integer, UNSIGNED INTEGER could overflow, so these values are converted to Python long integers instead.

If you wanted more rows, you could use c.fetchmany(n) or c.fetchall(). These do exactly what you think they do. On c.fetchmany(n), the n is optional and defaults to c.arraysize, which is normally 1. Both of these methods return a sequence of rows, or an empty sequence if there are no more rows. If you use a weird cursor class, the rows themselves might not be tuples.

Note that in contrast to the above, c.fetchone() returns None when there are no more rows to fetch.

The only other method you are very likely to use is when you have to do a multi-row insert:

c.executemany(
"""INSERT INTO breakfast (name, spam, eggs, sausage, price)
VALUES (%s, %s, %s, %s, %s)""",
[
("Spam and Sausage Lover's Plate", 5, 1, 8, 7.95 ),
("Not So Much Spam Plate", 3, 2, 0, 3.95 ),
("Don't Wany ANY SPAM! Plate", 0, 4, 3, 5.95 )
] )

Here we are inserting three rows of five values. Notice that there is a mix of types (strings, ints, floats) though we still only use %s. And also note that we only included format strings for one row. MySQLdb picks those out and duplicates them for each row.

Using and extending

In general, it is probably wise to not directly interact with the DB API except for small applicatons. Databases, even SQL databases, vary widely in capabilities and may have non-standard features. The DB API does a good job of providing a reasonably portable interface but some methods are non-portable. Specifically, the parameters accepted by connect() are completely implementation-dependent.

If you believe your application may need to run on several different databases, the author recommends the following approach, based on personal experience: Write a simplified API for your application which implements the specific queries and operations your application needs to perform. Implement this API as a base class which should be have few database dependencies, and then derive a subclass from this which implements the necessary dependencies. In this way, porting your application to a new database should be a relatively simple matter of creating a new subclass, assuming the new database is reasonably standard.

Because MySQLdb's Connection and Cursor objects are written in Python, you can easily derive your own subclasses. There are several Cursor classes in MySQLdb.cursors:

BaseCursor
The base class for Cursor objects. This does not raise Warnings.
CursorStoreResultMixIn
Causes the Cursor to use the mysql_store_result() function to get the query result. The entire result set is stored on the client side.
CursorUseResultMixIn
Causes the cursor to use the mysql_use_result() function to get the query result. The result set is stored on the server side and is transferred row by row using fetch operations.
CursorTupleRowsMixIn
Causes the cursor to return rows as a tuple of the column values.

CursorDictRowsMixIn

Causes the cursor to return rows as a dictionary, where the keys are column names and the values are column values. Note that if the column names are not unique, i.e., you are selecting from two tables that share column names, some of them will be rewritten as table.column. This can be avoided by using the SQL AS keyword. (This is yet-another reason not to use * in SQL queries, particularly where JOIN is involved.)
Cursor
The default cursor class. This class is composed of CursorWarningMixIn, CursorStoreResultMixIn, CursorTupleRowsMixIn, and BaseCursor, i.e. it raises Warning, uses mysql_store_result(), and returns rows as tuples.
DictCursor
Like Cursor except it returns rows as dictionaries.
SSCursor
A "server-side" cursor. Like Cursor but uses CursorUseResultMixIn. Use only if you are dealing with potentially large result sets.
SSDictCursor
Like SSCursor except it returns rows as dictionaries.

Embedded Server

Instead of connecting to a stand-alone server over the network, the embedded server support lets you run a full server right in your Python code or application server.

If you have built MySQLdb with embedded server support, there are two additional functions you will need to make use of:

server_init(args, groups)

Initialize embedded server. If this client is not linked against the embedded server library, this function does nothing.

args
sequence of command-line arguments
groups
sequence of groups to use in defaults files
server_end()
Shut down embedded server. If not using an embedded server, this does nothing.

See the MySQL documentation for more information on the embedded server.

Title: MySQLdb: a Python interface for MySQL
Author: Andy Dustman
Version: $Revision: 421 $


seal 2010-05-17 23:56 鍙戣〃璇勮
]]>
濡備綍鍦∕ac OS X緋葷粺涓惎鐢∕ySQL(杞?http://www.tkk7.com/sealyu/archive/2010/05/16/321082.htmlsealsealSun, 16 May 2010 01:38:00 GMThttp://www.tkk7.com/sealyu/archive/2010/05/16/321082.htmlhttp://www.tkk7.com/sealyu/comments/321082.htmlhttp://www.tkk7.com/sealyu/archive/2010/05/16/321082.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/321082.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/321082.html闅忕潃緗戠粶鏃ョ泭鍙戝睍榪樻湁os x鐢ㄦ埛鐨勫澶氾紝鏈夊彲鑳戒細(xì)闇瑕佸湪鑷繁鐨剎緋葷粺涓繍琛宮ysql+php鐜錛屾瘮濡傛灦璁劇綉绔欐垨鑰呮祴璇曚箣綾匯傜畝鍗曟楠ゅ涓嬶細(xì)

銆銆1銆佷笅杞組ySQL 5.x 鍙戣鐗?錛岃В鍘嬪茍瀹夎鏄犲儚涓殑涓や釜瀹夎鍖呮枃浠躲?/p>

銆銆a. mysql-5.x-osx10.6_x86_64.pkg 錛峬ysql 5.x 鏍囧噯鐗堝畨瑁?/p>

銆銆b. MySQLStartupItem.pkg錛峬ysql鍚姩欏圭洰錛屽彲浠ヤ笂浣犵殑鐢?shù)鑴戝湪鍚姩绯痪l熸椂鑷姩榪愯mysql鏈嶅姟銆傚畠瀹夎鍦?Library /StartupItems/MySQL/錛屽鏋滀綘涓嶆兂緋葷粺鍚姩鏃惰繍琛宮ysql鏈嶅姟錛岃涓嶈瀹夎銆傚鏋滀綘鍦ㄥ畨瑁呭悗鍙堜笉鎯充嬌鐢紝璇峰垹闄?Library /StartupItems/MySQL/榪欎釜鐩綍銆?/p>

銆銆鍚姩mysql錛?/p>

銆銆2銆佸鏋滀綘宸茬粡瀹夎浜?jiǎn)MySQLStartupItem.pkg錛岄噸鏂板惎鍔ㄧ數(shù)鑴戝嵆鍙?/p>

銆銆3銆佸鏋滀綘鏈夊畨瑁匨ySQLStartupItem.pkg鎴栬呬笉鎯沖惎鍔ㄧ數(shù)鑴戯紝榪愯錛氬簲鐢ㄧ▼搴忥紞瀹炵敤紼嬪簭錛嶇粓绔▼搴忥紝鍦ㄧ粓绔腑杈撳叆鍛戒護(hù)錛?/p>

銆銆sudo /Library/StartupItems/MySQL/MySQL start

銆銆鐒跺悗杈撳叆浣犵殑緋葷粺綆$悊鍛樺瘑鐮侊紝濡傛灉娌℃湁璁懼畾瀵嗙爜灝辯洿鎺ュ洖杞︺?/p>

銆銆鍏抽棴mysql鏈嶅姟錛?/p>

銆銆緇堢涓緭鍏ュ懡浠わ細(xì)sudo /Library/StartupItems/MySQL/MySQL stop

銆銆鐒跺悗杈撳叆浣犵殑緋葷粺綆$悊鍛樺瘑鐮侊紝濡傛灉娌℃湁璁懼畾瀵嗙爜灝辯洿鎺ュ洖杞︺?/p>

銆銆mysql root璐︽埛瀵嗙爜錛?/p>

銆銆mysql root瀵嗙爜鍒濆鍊兼槸絀恒傝繖鏍瘋櫧鐒舵病鏈夐棶棰樸備絾寰堜笉瀹夊叏銆傚緩璁綘鏇存敼root鐢ㄦ埛瀵嗙爜銆傛敞鎰忥細(xì)mysql root鐢ㄦ埛鍜岀郴緇熶腑鐨剅oot鐢ㄦ埛鏄笉涓鏍風(fēng)殑銆傛槸瀹屽叏涓や釜涓嶅悓鐨勭敤鎴楓?/p>

銆銆鏇存敼mysql root瀵嗙爜璇峰湪緇堢涓緭鍏ュ懡浠わ細(xì)

銆銆/usr/local/mysql/bin/mysqladmin -u root password 鏂板瘑鐮?/p>

銆銆鍚屾椂浣犱篃鍙互闅忔椂浣跨敤榪欐潯鍛戒護(hù)鏇存敼浣犵殑瀵嗙爜銆?/p>

銆銆4銆佷笅杞絰鐗坢ysql鏁版嵁搴撶鐞嗗伐鍏?/p>

銆銆榪欐槸涓涓繍琛屽湪mac os x緋葷粺涓殑mysql鏁版嵁搴撶鐞嗚蔣浠訛紝鏀寔鏈湴鍙?qiáng)杩溄E嬫暟鎹簱綆$悊銆傚茍涓旇繕鏄厤璐圭殑銆傝繖涓▼搴忕殑浼樼偣鏄畬鍏–GI鐣岄潰銆傚茍涓斿瘑鐮佹槸淇濆瓨鍦ㄦ湰鏈轟笂鐨勩傜浉瀵規(guī)瘮杈? 瀹夊叏銆?/p>

seal 2010-05-16 09:38 鍙戣〃璇勮
]]>
Software caused connection abort: recv failedhttp://www.tkk7.com/sealyu/archive/2010/04/15/318384.htmlsealsealWed, 14 Apr 2010 21:58:00 GMThttp://www.tkk7.com/sealyu/archive/2010/04/15/318384.htmlhttp://www.tkk7.com/sealyu/comments/318384.htmlhttp://www.tkk7.com/sealyu/archive/2010/04/15/318384.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/318384.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/318384.html
Solution: Uninstall ipv6 from Network management.

seal 2010-04-15 05:58 鍙戣〃璇勮
]]>
鏁版嵁搴撶殑鍔ㄦ佸瓧孌甸棶棰?/title><link>http://www.tkk7.com/sealyu/archive/2010/03/18/315797.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 18 Mar 2010 07:59:00 GMT</pubDate><guid>http://www.tkk7.com/sealyu/archive/2010/03/18/315797.html</guid><wfw:comment>http://www.tkk7.com/sealyu/comments/315797.html</wfw:comment><comments>http://www.tkk7.com/sealyu/archive/2010/03/18/315797.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/sealyu/comments/commentRss/315797.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/sealyu/services/trackbacks/315797.html</trackback:ping><description><![CDATA[<div>Sealyu  2010錛?錛?8</div> <div><br /> </div> 鍦ㄩ」鐩腑緇忓父浼?xì)纰板埌杩欐狅L(fēng)殑闂錛?鐢ㄦ埛瑕佹眰浠栦滑鐨勪駭鍝佺瓑瀹炰綋鐨勫睘鎬ф槸鍙畾鍒剁殑銆傛瘮濡傦細(xì)浜у搧鐨勬暟閲忋侀珮搴︺侀噸閲忕瓑灞炴э紝閮芥槸鍙互鍦ㄥ悗鍙板鍒犺鏌ョ殑銆傚湪榪欑鎯呭喌涓嬶紝浣跨敤java寤虹珛瀹炰綋灝遍亣鍒伴棶棰樹(shù)簡(jiǎn)錛屽洜涓烘暟鎹簱涓殑瀛楁鏄笉瀹氱殑銆?榪欑鎯呭喌鍦ㄤ竴浜涘崟鎹佽瘯鍗風(fēng)瓑鍦版柟鏄緢甯歌鐨勩? <div>1銆傜洰鍓嶄負(fù)姝紝娌℃湁鍙戠幇姣旇緝瀹岀編鐨勮В鍐蟲(chóng)柟娉曘?涔嬪墠欏圭洰涓亣鍒頒簡(jiǎn)涓嬈″姩鎬佺敓鎴愬崟鎹殑鎯呭喌錛屼富瑕佹槸閫氳繃浣跨敤鍑犲紶琛ㄦ潵瀛樺偍琛ㄧ殑緇撴瀯鍜屽瓧孌典俊鎭潵瀹炵幇銆?榪欑鎯呭喌涓嬶紝瀹炵幇鏌ヨ鍜屾姤琛ㄧ瓑鍔熻兘鏃跺氨浼?xì)鍙楀埌濯?jiǎng)鍝嶃?鍏蜂綋鏂規(guī)硶璺熶笅闈㈣繖浣嶇殑綾諱技錛? </div> <div> <div style="background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; padding-right: 5px; padding-bottom: 4px; padding-left: 4px; padding-top: 4px; width: 98%; word-break: break-all; "><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #000000; ">鎴戝湪緇欎竴瀹舵墦瀛楀叕鍙稿紑鍙戜竴涓皢琛ㄥ崟鐨勫唴瀹硅緭鍏ユ暟鎹簱鐨勭▼搴忋傝〃鍗曚腑鍙兘鏈夌畝鍗曠殑閫佽揣鏀舵嵁錛?0澶氫釜瀛楁錛夛紝鍙兘鏈夋皯鎰忚皟鏌ョ殑琛ㄦ牸錛?0澶氫釜瀛楁錛夛紝鍙兘鏈変繚闄╁叕鍙哥殑鐢ㄦ埛鐧昏琛紙100澶氫釜瀛楁錛夈傝鍘岀殑鏄繖鏍風(fēng)殑琛ㄥ崟鍙兘鍙堝嚑鐧劇銆?nbsp;  <br />       鎴戣璁$殑鏁版嵁搴撳涓嬶細(xì)   <br />       琛ㄥ崟鐨勭綾昏〃錛堣〃1錛夛細(xì)   <br />       Form_ID           //琛ㄥ崟鐨勭綾籌D   <br />       Form_Name       //琛ㄥ崟鐨勫悕縐?nbsp;  <br />     <br />       鏌愪竴涓〃鍗曠殑瀛楁琛紙琛?錛?nbsp;  <br />       Field_ID           //瀛楁鐨処D   <br />       Field_Name       //瀛楁鐨勫悕縐?nbsp;  <br />       Form_ID             //琛ㄥ崟鐨勭綾籌D錛屼笌琛?榪炴帴   <br />     <br />       鍏蜂綋鐨勬煇涓涓〃鍗曡〃錛堣〃3錛?nbsp;  <br />       Sheet_ID                     //鍏蜂綋鐨勬煇涓涓〃鍗曠殑ID   <br />       Client_ID                   //榪欎釜琛ㄥ崟鎵灞炵殑瀹㈡埛ID錛屼笌瀹㈡埛琛ㄨ繛鎺?nbsp;  <br />     <br />       鏌愪竴涓〃鍗曠殑瀛楁鍐呭琛紙琛?錛?nbsp;  <br />       Field_Value_ID         //瀛楁鐨勫唴瀹笽D   <br />       Field_Value               //瀛楁鐨勫唴瀹?nbsp;  <br />       Field_ID                     //瀛楁鐨処D錛屼笌琛?榪炴帴   <br />       Sheet_ID                     //鍏蜂綋鐨勬煇涓涓〃鍗曠殑ID錛屼笌琛?榪炴帴   <br />     <br />       姣忓鍔犱簡(jiǎn)涓縐嶆柊鐨勮〃鍗曟椂錛屽湪琛?鍜岃〃2涓坊鍔犲涓嬭褰曪細(xì)   <br />           琛?錛欶orm_ID           Form_Name   <br />                       A001                 閫佽揣鍗?nbsp;    <br />           琛?錛欶ield_ID         Field_Name       Form_ID             <br />                       f0001               鍙戣揣鏃ユ湡           A001     <br />                       f0002               鍙戣揣浜?nbsp;              A001     <br />                       f0003             鏀惰揣浜哄湴鍧         A001     <br />       榪欐牱錛屽綋鏌愪竴涓墦瀛楀叕鍙哥殑瀹㈡埛瑕佹眰杈撳叆榪欎釜“閫佽揣鍗?#8221;鏃訛紝姣忚緭鍏ヤ竴涓佽揣鍗曪紙鍗寵〃鍗曡〃3錛夛紝琛?鍜岃〃4娣誨姞濡備笅璁板綍錛氾紙姣忚緭鍏ヤ竴涓佽揣鍗曚箣綾葷殑琛ㄥ崟錛岃〃3鍜岃〃4閮芥坊鍔犳暟鎹級(jí)   <br />           琛?錛歋heet_ID         Client_ID   <br />                       S001                 0000001   <br />           琛?錛欶ield_Value_ID         Field_Value         Field_ID           Sheet_ID   <br />                           V0001                         2001.5.8               f0001                 S001     <br />                           V0002                             寮犱笁                     f0002               S001     <br />                           V0003                     澶ц繛甯?jìng)瑗垮矖鍖?nbsp;            f0003                 S001     <br />       鎵撳瓧鍏徃鏍規(guī)嵁鍏蜂綋鐨勬煇涓涓〃鍗曠殑ID錛圫heet_ID錛夛紝緇撳悎4涓〃鎻愬彇瀹㈡埛ID銆佽〃鍗曠綾誨悕縐般佸瓧孌靛悕縐板拰瀛楁鍐呭瀛樺叆鍙﹀鐨勬暟鎹簱銆?  </span></div> </div> <div>2銆傝繕鐪嬪埌榪囦竴縐嶆儏鍐碉紝鏄彧鐢ㄤ竴涓暱瀛楃涓插瓧孌碉紝鍒╃敤xml鐨勬牸寮忔潵瀛樺偍瀵瑰簲鐨勭粨鏋勶紝鍚屾椂鍦╦ava涓嬌鐢▁ml2db絳夊伐鍏鋒潵瑙f瀽銆傝繖縐嶆儏鍐典笅錛屾煡璇㈠拰鎶ヨ〃鍔熻兘鍚屾牱涓嶅ソ瀹炵幇銆傚悓鏃訛紝xml鑺傜偣鐨勫鍒犺鏌ヤ篃涓嶅お鏂逛究銆?/div> <div>3銆傛渶鍚庝竴縐嶆柟娉曪紝鏄洿鎺ヤ嬌鐢ㄦ暟鎹簱鏉ュ疄鐜般傚墠鍑犲ぉ鐪嬪埌DB2 9涓凡緇忔敮鎸佸XML鏍煎紡鏁版嵁鐨勫瓨鍌ㄥ拰鏌ヨ銆備嬌鐢╔QUERY鏉ラ厤濂楀疄鐜版煡璇€備笉榪囪繖縐嶆柟娉曞鏁版嵁搴撶殑渚濊禆鎬у己錛岀洰鍓嶄負(fù)姝紝榪樻病鍙戠幇鏈夊叾浠栨暟鎹簱鎻愪緵榪欑鍔熻兘銆?/div> <div><br /> </div> <img src ="http://www.tkk7.com/sealyu/aggbug/315797.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/sealyu/" target="_blank">seal</a> 2010-03-18 15:59 <a href="http://www.tkk7.com/sealyu/archive/2010/03/18/315797.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>XMLTABLE by example, Part 2: Common scenarios for using XMLTABLE with DB2http://www.tkk7.com/sealyu/archive/2010/02/24/313810.htmlsealsealWed, 24 Feb 2010 08:43:00 GMThttp://www.tkk7.com/sealyu/archive/2010/02/24/313810.htmlhttp://www.tkk7.com/sealyu/comments/313810.htmlhttp://www.tkk7.com/sealyu/archive/2010/02/24/313810.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/313810.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/313810.html闃呰鍏ㄦ枃

seal 2010-02-24 16:43 鍙戣〃璇勮
]]>
spring鐨凞riverManagerDataSource涓巃pache鐨凚asicDataSource錛堣漿錛?/title><link>http://www.tkk7.com/sealyu/archive/2010/02/02/311728.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Tue, 02 Feb 2010 14:04:00 GMT</pubDate><guid>http://www.tkk7.com/sealyu/archive/2010/02/02/311728.html</guid><wfw:comment>http://www.tkk7.com/sealyu/comments/311728.html</wfw:comment><comments>http://www.tkk7.com/sealyu/archive/2010/02/02/311728.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/sealyu/comments/commentRss/311728.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/sealyu/services/trackbacks/311728.html</trackback:ping><description><![CDATA[鎯呭喌鏄繖鏍風(fēng)殑銆傘傘? <br /> <br /> 2008-3-18 1:08:26 org.apache.tomcat.util.threads.ThreadPool logFull <br /> 涓ラ噸: All threads (150) are currently busy, waiting. Increase maxThreads (150) or check the servlet status <br /> <br /> 閲嶆柊鍚姩鏈嶅姟鍣ㄤ箣鍚庯紝闂?shù)緷鐒跺瓨鍦ㄣ? <br /> 鍒嗘瀽寰楀嚭浠ヤ笅鍙兘鎯呭喌 <br /> 1.榪炴帴鏁拌揪鍒頒簡(jiǎn)150鐨勬渶澶т笂闄? <br /> 2.鏈嶅姟鍣ㄧ鍝嶅簲鐢ㄦ埛璇鋒眰鐨勬椂闂磋繃闀垮鑷? <br /> 3.榪炴帴姹犵殑閰嶇疆鏁板嚭浜?jiǎn)闂? <br /> <br /> 鍒嗘瀽錛? <br /> 1.1涓敤鎴瘋闂槸OK鐨勶紝褰撶敤2涓敤鎴峰localhost榪涜璁塊棶錛屾墍浠ユ牴鏈笉鍙兘杈懼埌騫跺彂璁塊棶綰跨▼鐨?50鐨勪笂闄愶紝鎵浠ュ簲璇ヤ笉鏄暟鎹簱鐨勫師鍥?鎺掗櫎浜?jiǎn)绗竴縐嶅彲鑳? <br /> 2.涔嬪墠璁塊棶鐨凧SP宸茬粡緇忚繃澶氭璁塊棶錛屾墍浠ヤ笉鍙兘鏄涓嬈¤闂紪璇戯紝榪愯鑰屽鑷寸殑璇鋒眰鏃墮棿榪囬暱鐨勫師鍥狅紝絎簩鎯呭喌涔熷惁瀹氥? <br /> <br /> <strong>鍒版錛岀粡榪囧垎鏋愬彲浠ョ‘瀹氱殑鏄繛鎺ユ睜鍑轟簡(jiǎn)闂...</strong> <br /> <br /> 棣栧厛鏈?涓煡璇嗙偣闇瑕佸啀寮勭殑鏇存竻妤氫竴浜? <br /> <br /> <strong>DriverManager涓嶥ataSource  榪炴帴鏁版嵁搴撴湁浣曞尯鍒?</strong> <br />         DriverManager浼犵粺鐨刯dbc榪炴帴錛岄氳繃Class.forName("XXX"),鐨勫姙娉曟敞鍐屼箣鍚庯紝灝卞彲浠riverManager.getConnection()鑾峰緱榪炴帴浜?jiǎn)銆? <br /> <br />         DataSource鏄緩绔嬪湪JNDI鏈嶅姟鍩虹涓婄殑,闇瑕乤pplication server閰嶇疆datasource.棣栧厛闇瑕佹敞鍐屼竴涓狣ataSource(涓鑸湪/META-INF/context.xml涓?鐒跺悗鍦? web.xml鏂囦歡涓紩鐢ㄨ繖涓狣ataSource,灝卞彲浠ataSource.getConnection()鑾峰緱榪炴帴,鍏蜂綋鎿嶄綔鍙傝?tomcat 鐩綍閲岀殑JNDI Resources灝忚妭) <br /> <br /> <strong>DataSource 涓?DBCP pool(榪炴帴姹? 鐨勫尯鍒紵</strong> <br />         J2EE 鏈嶅姟鍣ㄥ惎鍔ㄦ椂浼?xì)寰忕珛涓瀹氭暟閲忕殑姹犺繛鎺ワ紝騫朵竴鐩寸淮鎸佷笉灝戜簬姝ゆ暟鐩殑姹犺繛鎺ャ傚鎴風(fēng)紼嬪簭闇瑕佽繛鎺ユ椂錛屾睜椹卞姩紼嬪簭浼?xì)杩斿洖涓涓湭浣跨敤鐨勬睜榪炴帴騫跺皢鍏惰〃璁頒負(fù)蹇欍傚鏋? 褰撳墠娌℃湁絀洪棽榪炴帴錛屾睜椹卞姩紼嬪簭灝辨柊寤轟竴瀹氭暟閲忕殑榪炴帴錛屾柊寤鴻繛鎺ョ殑鏁伴噺鏈夐厤緗弬鏁板喅瀹氥傚綋浣跨敤鐨勬睜榪炴帴璋冪敤瀹屾垚鍚庯紝姹犻┍鍔ㄧ▼搴忓皢姝よ繛鎺ヨ〃璁頒負(fù)絀洪棽錛屽叾浠栬皟鐢? 灝卞彲浠ヤ嬌鐢ㄨ繖涓繛鎺ャ? <br /> 鐩稿綋浜庢槸浼樺寲浜?jiǎn)DataSource鐨勪竴縐嶅伐鍏? <br /> <br /> 璺熸暟鎹簱榪炴帴鐨勯儴鍒嗘槸閫氳繃Spring鐨凞ataSource JDBC榪炴帴鐨?閰嶇疆鐨刋ML鍐呭濡備笅: <br /> <div> <div> <div>Xml浠g爜 <embed src="http://jueforever.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3Cbean%20id%3D%22propertyConfigurer%22%20%0Aclass%3D%22org.springframework.beans.factory.config.PropertyPlaceholderConfigurer%22%3E%0A%3C!--PropertyPlaceholderConfigurer%E7%B1%BB%E6%9D%A5%E8%AF%BB%E5%8F%96xxx.properties%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%BF%A1%E6%81%AF%2C%E4%BB%A5key%E5%92%8Cvalue%E7%9A%84%E5%BD%A2%E5%BC%8F--%3E%0A%3Cproperty%20name%3D%22locations%22%3E%0A%20%20%20%20%3Clist%3E%0A%20%20%20%20%20%20%20%20%3Cvalue%3E%0A%20%20%20%20%20%20%20%20%20%20%2FWEB-INF%2Fclasses%2Fconfig%2Fpkm%2Fenvironment%2Fjdbc.properties%0A%20%20%20%20%20%20%20%20%3C%2Fvalue%3E%0A%20%20%20%20%20%20%20%20%3Cvalue%3E%0A%20%20%20%20%20%20%20%20%20%20%3C!--%E5%A4%9A%E4%B8%AAxxx.properties%E6%96%87%E4%BB%B6--%3E%0A%20%20%20%20%20%20%20%20%3C%2Fvalue%3E%0A%20%20%20%20%3C%2Flist%3E%0A%3C%2Fproperty%3E%0A%3C%2Fbean%3E%0A%0A%3C!--%E4%BA%8B%E5%AE%9E%E4%B8%8A%E6%98%AF%E5%9B%A0%E4%B8%BADriverManagerDataSource%E5%BB%BA%E7%AB%8B%E8%BF%9E%E6%8E%A5%E6%98%AF%E5%8F%AA%E8%A6%81%E6%9C%89%E8%BF%9E%E6%8E%A5%E5%B0%B1%E6%96%B0%E5%BB%BA%E4%B8%80%E4%B8%AAconnection%2C%E6%A0%B9%E6%9C%AC%E6%B2%A1%E6%9C%89%E8%BF%9E%E6%8E%A5%E6%B1%A0%E7%9A%84%E4%BD%9C%E7%94%A8--%3E%0A%3C!--%E4%B8%A4%E7%A7%8D%E4%B8%8D%E5%90%8C%E7%9A%84DataSource--%3E%0A%3C!--%E5%8D%95%E7%BA%AF%E7%9A%84DataSource--%3E%0A%3Cbean%20id%3D%22pkmDataSource%22%20%0Aclass%3D%22org.springframework.jdbc.datasource.DriverManagerDataSource%22%3E%0A%20%20%20%20%3Cproperty%20name%3D%22driverClassName%22%3E%0A%20%20%20%20%20%20%20%20%3Cvalue%3E%24%7Bpkm.jdbc.driverClassName%7D%3C%2Fvalue%3E%0A%20%20%20%20%20%20%20%20%3C!--%24%7Bpkm.jdbc.driverClassName%7D%E6%98%AFjdbc.properties%E6%96%87%E4%BB%B6%20%E4%B8%AD%E7%9A%84key--%3E%0A%20%20%20%20%3C%2Fproperty%3E%0A%20%20%20%20%3Cproperty%20name%3D%22url%22%3E%0A%20%20%20%20%20%20%20%20%3Cvalue%3E%24%7Bpkm.jdbc.url%7D%3C%2Fvalue%3E%0A%20%20%20%20%3C%2Fproperty%3E%0A%20%20%20%20%3Cproperty%20name%3D%22username%22%3E%0A%20%20%20%20%20%20%20%20%3Cvalue%3E%24%7Bpkm.jdbc.username%7D%3C%2Fvalue%3E%0A%20%20%20%20%3C%2Fproperty%3E%0A%20%20%20%20%3Cproperty%20name%3D%22password%22%3E%0A%20%20%20%20%20%20%20%20%3Cvalue%3E%24%7Bpkm.jdbc.password%7D%3C%2Fvalue%3E%0A%20%20%20%20%3C%2Fproperty%3E%0A%3C%2Fbean%3E%0A%0A%3C!--%E8%BF%9E%E6%8E%A5%E6%B1%A0--%3E%0A%3Cbean%20id%3D%22pkmDataSource%22%20%0Aclass%3D%22org.apache.commons.dbcp.BasicDataSource%22%20destroy-method%3D%22close%22%20lazy-init%3D%22false%22%3E%0A%20%20%20%20%3Cproperty%20name%3D%22driverClassName%22%20value%3D%22%24%7Bpkm.jdbc.driverClassName%7D%22%2F%3E%0A%20%20%20%20%3Cproperty%20name%3D%22url%22%20value%3D%22%24%7Bpkm.jdbc.url%7D%22%2F%3E%0A%20%20%20%20%3Cproperty%20name%3D%22username%22%20value%3D%22%24%7Bpkm.jdbc.username%7D%22%2F%3E%0A%20%20%20%20%3Cproperty%20name%3D%22password%22%20value%3D%22%24%7Bpkm.jdbc.password%7D%22%2F%3E%0A%20%20%20%20%3Cproperty%20name%3D%22initialSize%22%20value%3D%225%22%2F%3E%0A%20%20%20%20%3Cproperty%20name%3D%22maxActive%22%20value%3D%2210%22%2F%3E%0A%20%20%20%20%3Cproperty%20name%3D%22maxWait%22%20value%3D%2260000%22%2F%3E%0A%20%20%20%20%3Cproperty%20name%3D%22poolPreparedStatements%22%20value%3D%22true%22%2F%3E%20%20%0A%3C%2Fbean%3E" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15"></div> </div> <ol start="1"> <li><bean id="propertyConfigurer"   </li> <li>class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  </li> <li><!--PropertyPlaceholderConfigurer綾繪潵璇誨彇xxx.properties閰嶇疆鏂囦歡淇℃伅,浠ey鍜寁alue鐨勫艦寮?->  </li> <li><property name="locations">  </li> <li>    <list>  </li> <li>        <value>  </li> <li>          /WEB-INF/classes/config/pkm/environment/jdbc.properties  </li> <li>        </value>  </li> <li>        <value>  </li> <li>          <!--澶氫釜xxx.properties鏂囦歡-->  </li> <li>        </value>  </li> <li>    </list>  </li> <li></property>  </li> <li></bean>  </li> <li>  </li> <li><!--浜嬪疄涓婃槸鍥犱負(fù)DriverManagerDataSource寤虹珛榪炴帴鏄彧瑕佹湁榪炴帴灝辨柊寤轟竴涓猚onnection,鏍規(guī)湰娌℃湁榪炴帴姹犵殑浣滅敤-->  </li> <li><!--涓ょ涓嶅悓鐨凞ataSource-->  </li> <li><!--鍗曠函鐨凞ataSource-->  </li> <li><bean id="pkmDataSource"   </li> <li>class="org.springframework.jdbc.datasource.DriverManagerDataSource">  </li> <li>    <property name="driverClassName">  </li> <li>        <value>${pkm.jdbc.driverClassName}</value>  </li> <li>        <!--${pkm.jdbc.driverClassName}鏄痡dbc.properties鏂囦歡 涓殑key-->  </li> <li>    </property>  </li> <li>    <property name="url">  </li> <li>        <value>${pkm.jdbc.url}</value>  </li> <li>    </property>  </li> <li>    <property name="username">  </li> <li>        <value>${pkm.jdbc.username}</value>  </li> <li>    </property>  </li> <li>    <property name="password">  </li> <li>        <value>${pkm.jdbc.password}</value>  </li> <li>    </property>  </li> <li></bean>  </li> <li>  </li> <li><!--榪炴帴姹?->  </li> <li><bean id="pkmDataSource"   </li> <li>class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" lazy-init="false">  </li> <li>    <property name="driverClassName" value="${pkm.jdbc.driverClassName}"/>  </li> <li>    <property name="url" value="${pkm.jdbc.url}"/>  </li> <li>    <property name="username" value="${pkm.jdbc.username}"/>  </li> <li>    <property name="password" value="${pkm.jdbc.password}"/>  </li> <li>    <property name="initialSize" value="5"/>  </li> <li>    <property name="maxActive" value="10"/>  </li> <li>    <property name="maxWait" value="60000"/>  </li> <li>    <property name="poolPreparedStatements" value="true"/>    </li> <li></bean>  </li> </ol> </div> <pre style="display: none;" name="code" class="xml"><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!--PropertyPlaceholderConfigurer綾繪潵璇誨彇xxx.properties閰嶇疆鏂囦歡淇℃伅,浠ey鍜寁alue鐨勫艦寮?-> <property name="locations"> <list> <value> /WEB-INF/classes/config/pkm/environment/jdbc.properties </value> <value> <!--澶氫釜xxx.properties鏂囦歡--> </value> </list> </property> </bean> <!--浜嬪疄涓婃槸鍥犱負(fù)DriverManagerDataSource寤虹珛榪炴帴鏄彧瑕佹湁榪炴帴灝辨柊寤轟竴涓猚onnection,鏍規(guī)湰娌℃湁榪炴帴姹犵殑浣滅敤--> <!--涓ょ涓嶅悓鐨凞ataSource--> <!--鍗曠函鐨凞ataSource--> <bean id="pkmDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${pkm.jdbc.driverClassName}</value> <!--${pkm.jdbc.driverClassName}鏄痡dbc.properties鏂囦歡 涓殑key--> </property> <property name="url"> <value>${pkm.jdbc.url}</value> </property> <property name="username"> <value>${pkm.jdbc.username}</value> </property> <property name="password"> <value>${pkm.jdbc.password}</value> </property> </bean> <!--榪炴帴姹?-> <bean id="pkmDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" lazy-init="false"> <property name="driverClassName" value="${pkm.jdbc.driverClassName}"/> <property name="url" value="${pkm.jdbc.url}"/> <property name="username" value="${pkm.jdbc.username}"/> <property name="password" value="${pkm.jdbc.password}"/> <property name="initialSize" value="5"/> <property name="maxActive" value="10"/> <property name="maxWait" value="60000"/> <property name="poolPreparedStatements" value="true"/> </bean></pre> <br /> <br />         鎵浠ラ棶棰樺氨鍑哄湪榪欓噷,褰撲嬌鐢˙asicDataSource鍚庨棶棰樹(shù)笉鍦ㄥ嚭鐜?鎵浠ユ槸榪炴帴鏁伴噺閫犳垚鐨?鍦ㄨ闂暟閲忓ぇ,騫跺彂鐨勬儏鍐典笅,姣棤鐤戦棶鏄閫夋嫨榪炴帴姹犵殑,鍥犱負(fù)鏈夎繛鎺ユ睜鐨勫姛鑳?鏃犺鏄晥鐜囪繕鏄湪璧勬簮鍒╃敤鐜囦笂閮戒紭?shù)簬DriverManagerDataSource <br /> <br />         浠庤屼粠鏍規(guī)湰涓婁簡(jiǎn)瑙d簡(jiǎn)spring鐨凞riverManagerDataSource涓巃pacheBasicDataSource涔嬮棿鐨勫尯鍒? <br /> <br /> 鏈夋椂鍊欏彲鑳介渶瑕侀厤緗甁ndi鏈嶅姟 <br /> <div> <div> <div>Xml浠g爜 <embed src="http://jueforever.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3Cbean%20id%3D%22pkmDataSource%22%20%0Aclass%3D%22org.springframework.jndi.JndiObjectFactoryBean%22%3E%0A%20%20%20%20%3Cproperty%20name%3D%22jndiName%22%20value%3D%22pkmDataSource%22%2F%3E%20%0A%3C%2Fbean%3E%0A%3C!--%0A%E8%BF%99%E6%A0%B7%E7%9A%84%E8%AF%9D%E9%83%A8%E7%BD%B2%E7%9A%84%E6%97%B6%E5%80%99%EF%BC%8C%E9%9C%80%E8%A6%81%E5%9C%A8%E5%AE%B9%E5%99%A8%E4%B8%AD%EF%BC%88tomcat%2Cweblogic%EF%BC%89%E9%85%8D%E7%BD%AEJDBC%20Connection%20Pool(DBCP)%E8%BF%9E%E6%8E%A5%E6%B1%A0%0A--%3E%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15"></div> </div> <ol start="1"> <li><bean id="pkmDataSource"   </li> <li>class="org.springframework.jndi.JndiObjectFactoryBean">  </li> <li>    <property name="jndiName" value="pkmDataSource"/>   </li> <li></bean>  </li> <li><!-- </li> <li>榪欐牱鐨勮瘽閮ㄧ講鐨勬椂鍊欙紝闇瑕佸湪瀹瑰櫒涓紙tomcat,weblogic錛夐厤緗甁DBC Connection Pool(DBCP)榪炴帴姹?nbsp;</li> <li>-->  </li> </ol> </div> <pre style="display: none;" name="code" class="xml"><bean id="pkmDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="pkmDataSource"/> </bean> <!-- 榪欐牱鐨勮瘽閮ㄧ講鐨勬椂鍊欙紝闇瑕佸湪瀹瑰櫒涓紙tomcat,weblogic錛夐厤緗甁DBC Connection Pool(DBCP)榪炴帴姹? --> </pre> <br /> <br /> <strong>榪欎笁縐嶈繛鎺ユ柟寮忓父甯鎬嬌鐢紝涔熷鏄撴販娣嗭紝閫夋嫨鍏朵腑鐨勬煇涓縐嶏紝灝遍渶瑕佺湅鍏蜂綋鐜鏉ラ厤緗簡(jiǎn)銆?/strong> <br /> <br /> <span style="font-size: medium;">PS錛歫dbc.properties鏂囦歡涓殑閰嶇疆濡備笅</span> <br /> <div> <div> <div>Properties浠g爜 <embed src="http://jueforever.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=pkm.jdbc.driverClassName%3Doracle.jdbc.OracleDriver%0Apkm.jdbc.url%3Djdbc%5C%3Aoracle%5C%3Athin%5C%3A%40109.52.20.31%5C%3A1521%5C%3Aorcl%3C!--%E6%8A%8A%E7%AC%A6%E5%8F%B7%E5%81%9A%E8%BD%AC%E8%AF%91--%3E%0Apkm.jdbc.name%3Dpkmuser%0Apkm.jdbc.password%3Ddbl0gin%0Apkm.jdbc.dataSource%3DpkmDataSource" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="14" height="15"></div> </div> <ol start="1"> <li>pkm.jdbc.driverClassName=oracle.jdbc.OracleDriver  </li> <li>pkm.jdbc.url=jdbc":oracle":thin":@109.52.20.31":1521":orcl<!--鎶婄鍙峰仛杞瘧-->  </li> <li>pkm.jdbc.name=pkmuser  </li> <li>pkm.jdbc.password=dbl0gin  </li> <li>pkm.jdbc.dataSource=pkmDataSource  </li> </ol> <br /> <h1> 鍏充簬Spring鏁村悎鍙戠幇鐨勪竴浜涢棶棰樸? <cite><a href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(saveit=window.open('http://wz.csdn.net/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'saveit','scrollbars=no,width=590,height=300,left=75,top=20,status=no,resizable=yes'));saveit.focus();" class="fav_csdnstylebykimi" title="鏀惰棌鍒版垜鐨勭綉鎽樹(shù)腑錛屽茍鍒嗕韓緇欐垜鐨勬湅鍙?>鏀惰棌</a> </cite> </h1> <div> <script type="text/javascript"> document.body.oncopy = function() { if (window.clipboardData) { setTimeout(function() { var text = clipboardData.getData("text"); if (text && text.length > 300) { text = text + ""r"n"n鏈枃鏉ヨ嚜CSDN鍗氬錛岃漿杞借鏍囨槑鍑哄錛? + location.href; clipboardData.setData("text", text); } }, 100); } } </script> <script type="text/javascript"> function StorePage() { d = document; t = d.selection ? (d.selection.type != 'None' ? d.selection.createRange().text : '') : (d.getSelection ? d.getSelection() : ''); void (keyit = window.open('http://www.365key.com/storeit.aspx?t=' + escape(d.title) + '&u=' + escape(d.location.href) + '&c=' + escape(t), 'keyit', 'scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes')); keyit.focus(); }</script> <p>Spring鎻愪緵浜?jiǎn)涓や釜杩欐狅L(fēng)殑鏁版嵁婧愶紙閮戒綅浜巓rg.springframework.jdbc.datasource紼嬪簭鍖呴噷錛夛細(xì)<br /> DriverManagerDataSource錛氬湪姣忎釜榪炴帴璇鋒眰鏃墮兘鏂板緩涓涓繛鎺ャ備笌DBCP鐨凚asicDataSource涓嶅悓錛孌riverManagerDataSource鎻愪緵鐨勮繛鎺ユ病鏈夎繘琛屾睜綆$悊銆?br /> SingleConnectionDataSource錛氬湪姣忎釜榪炴帴璇鋒眰鏃墮兘榪斿洖鍚屼竴涓繛鎺ャ傝櫧鐒跺畠涓嶅悓涓ユ牸鎰忎箟涓婄殑姹犵鐞嗘暟鎹簮錛屼絾鎴戜滑鍙互鎶婂畠鐪嬩綔鍙湁涓涓繛鎺ョ殑姹犮?br /> 瀵逛袱涓暟鎹簮鐨勯厤緗兘綾諱技浜庨厤緗瓺BCP鐨凚asicDataSource<br /> 鍖哄埆鍦ㄤ簬鐢變簬DriverManagerDataSource鍜孲ingleConnectionDataSource閮芥病鏈夋彁渚涜繛鎺ユ睜錛屾墍浠ュ湪姝ゆ病鏈夎緗睜閰嶇疆灞炴с?br /> 铏界劧榪欎袱涓暟鎹簮閮藉浜庡皬紼嬪簭鏉ヨ鏄緢涓嶉敊鐨勶紝鑰屼笖榪樺湪涓嶆柇鍙戝睍錛屼絾鎶婂畠浠敤浜庣敓浜х▼搴忚繕鏄渶瑕佽鐪熻冭檻鐨勩?/p> <p>SingleConnectionDataSource鍙嬌鐢ㄤ竴涓暟鎹簱榪炴帴錛屾墍浠ヤ笉閫傚悎鐢ㄤ簬澶氱嚎紼嬬▼搴忋傝? DriverMangerDataSource铏界劧鑳藉鏀寔澶氱嚎紼嬶紝浣嗗畠浼?xì)鍦ㄦ瘡娆q炴帴璇鋒眰鏃墮兘鏂板緩涓涓繛鎺ワ紝榪欐槸浠ユц兘涓轟唬浠風(fēng)殑銆傜敱浜庤繖浜涢檺鍒訛紝鎴戜滑寮虹儓 寤鴻搴旇浣跨敤鏁版嵁婧愭睜銆?/p> <p>鍦ㄩ氳繃鏁版嵁婧愪笌鏁版嵁搴撳緩绔嬭繛鎺ヤ箣鍚庯紝鎴戜滑灝辮瀹為檯璁塊棶鏁版嵁搴撲簡(jiǎn)錛岃屾渶鍩烘湰鐨勬柟寮忓氨鏄嬌鐢↗DBC錛岀幇鍦ㄦ垜浠氨鏉ョ湅涓鐪婼pring濡備綍璁╀嬌鐢ㄧ畝鍗曠殑JDBC鏇村姞綆渚褲?br />  Spring鍦ㄧ涓夋柟渚濊禆鍖呬腑鍖呭惈浜?jiǎn)涓や釜鏁版嵁婧愮殑瀹炵幇绫诲寘锛屽叾涓鏄疉pache鐨凞BCP錛屽叾浜屾槸 C3P0銆傚彲浠ュ湪Spring閰嶇疆鏂囦歡涓埄鐢ㄨ繖涓よ呬腑浠諱綍涓涓厤緗暟鎹簮銆?/p> <p>DBCP鏁版嵁婧?<br />      DBCP 綾誨寘浣嶄簬 /lib/jakarta-commons/commons-dbcp.jar錛孌BCP鏄竴涓緷璧?Jakarta commons- pool瀵硅薄姹犳満鍒剁殑鏁版嵁搴撹繛鎺ユ睜錛屾墍浠ュ湪綾昏礬寰勪笅榪樺繀欏誨寘鎷?lib/jakarta- commons/commons-pool.jar銆備笅闈㈡槸浣?鐢―BCP閰嶇疆MySql鏁版嵁婧愮殑閰嶇疆鐗囨柇錛?/p> <p>xml 浠g爜<br /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"        <br />          destroy-method="close">        <br />      <property name="driverClassName" value="com.mysql.jdbc.Driver" />       <br />      <property name="url" value="jdbc:mysql://localhost:3309/sampledb" />       <br />      <property name="username" value="root" />       <br />      <property name="password" value="1234" />       <br /> bean>   </p> <p>BasicDataSource鎻愪緵浜?jiǎn)close()鏂規(guī)硶鍏抽棴鏁版嵁婧愶紝鎵浠ュ繀欏昏瀹歞estroy-method=”close”灞炴э紝 浠ヤ究Spring瀹瑰櫒鍏抽棴鏃訛紝鏁版嵁婧愯兘澶熸甯稿叧闂傞櫎浠ヤ笂蹇呴』鐨勬暟鎹簮灞炴у錛岃繕鏈変竴浜涘父鐢ㄧ殑灞炴э細(xì) <br />      defaultAutoCommit錛氳緗粠鏁版嵁婧愪腑榪斿洖鐨勮繛鎺ユ槸鍚﹂噰鐢ㄨ嚜鍔ㄦ彁浜ゆ満鍒訛紝榛樿鍊間負(fù) true錛?<br />      defaultReadOnly錛氳緗暟鎹簮鏄惁浠呰兘鎵ц鍙鎿嶄綔錛?榛樿鍊間負(fù) false錛?<br />      maxActive錛氭渶澶ц繛鎺ユ暟鎹簱榪炴帴鏁幫紝璁劇疆涓?鏃訛紝琛ㄧず娌℃湁闄愬埗錛?<br />      maxIdle錛氭渶澶х瓑寰呰繛鎺ヤ腑鐨勬暟閲忥紝璁劇疆涓?鏃訛紝琛ㄧず娌℃湁闄愬埗錛?<br />      maxWait錛氭渶澶х瓑寰呯鏁幫紝鍗曚綅涓烘縐掞紝 瓚呰繃鏃墮棿浼?xì)鎶ュ嚭閿欒淇℃伅锛?<br />      validationQuery錛氱敤浜庨獙璇佽繛鎺ユ槸鍚︽垚鍔熺殑鏌ヨSQL璇彞錛孲QL璇彞蹇呴』鑷沖皯瑕佽繑鍥炰竴琛屾暟鎹紝 濡備綘鍙互綆鍗曞湴璁劇疆涓猴細(xì)“select count(*) from user”錛?<br />      removeAbandoned錛氭槸鍚﹁嚜鎴戜腑鏂紝榛樿鏄?false 錛?<br />      removeAbandonedTimeout錛氬嚑縐掑悗鏁版嵁榪炴帴浼?xì)鑷姩鏂紑錛屽湪removeAbandoned涓簍rue錛屾彁渚涜鍊鹼紱 <br />      logAbandoned錛氭槸鍚﹁褰曚腑鏂簨浠訛紝 榛樿涓?false錛?</p> <p>C3P0鏁版嵁婧?<br />      C3P0 鏄竴涓紑鏀炬簮浠g爜鐨凧DBC鏁版嵁婧愬疄鐜伴」鐩紝瀹冨湪lib鐩綍涓笌Hibernate涓璧峰彂甯冿紝瀹炵幇浜?jiǎn)JDBC3鍜孞DBC2鎵╁睍瑙勮寖璇存槑鐨? Connection 鍜孲tatement 姹犮侰3P0綾誨寘浣嶄簬/lib/c3p0/c3p0-0.9.0.4.jar銆備笅闈㈡槸浣跨敤C3P0閰嶇疆涓 涓?oracle鏁版嵁婧愶細(xì)</p> <p>xml 浠g爜<br /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"        <br />          destroy-method="close">       <br />      <property name="driverClass" value=" oracle.jdbc.driver.OracleDriver "/>       <br />      <property name="jdbcUrl" value=" jdbc:oracle:thin:@localhost:1521:ora9i "/>       <br />      <property name="user" value="admin"/>       <br />      <property name="password" value="1234"/>       <br /> bean>   </p> <p>ComboPooledDataSource鍜孊asicDataSource涓鏍鋒彁渚涗簡(jiǎn)涓涓敤浜庡叧闂暟鎹簮鐨刢lose()鏂規(guī)硶錛岃繖鏍鋒垜浠氨鍙互淇濊瘉Spring瀹瑰櫒鍏抽棴鏃舵暟鎹簮鑳藉鎴愬姛閲婃斁銆?<br />      C3P0鎷ユ湁姣擠BCP鏇翠赴瀵岀殑閰嶇疆灞炴э紝閫氳繃榪欎簺灞炴э紝鍙互瀵規(guī)暟鎹簮榪涜鍚勭鏈夋晥鐨勬帶鍒訛細(xì) <br />      acquireIncrement錛氬綋榪炴帴姹犱腑鐨勮繛鎺ョ敤瀹屾椂錛孋3P0涓嬈℃у垱寤烘柊榪炴帴鐨勬暟鐩紱 <br />      acquireRetryAttempts錛氬畾涔夊湪浠庢暟鎹簱鑾峰彇鏂拌繛鎺ュけ璐ュ悗閲嶅灝濊瘯鑾峰彇鐨勬鏁幫紝榛樿涓?0錛?<br />      acquireRetryDelay錛氫袱嬈¤繛鎺ヤ腑闂撮殧鏃墮棿錛屽崟浣嶆縐掞紝榛樿涓?000錛?<br />      autoCommitOnClose錛氳繛鎺ュ叧闂椂榛樿灝嗘墍鏈夋湭鎻愪氦鐨勬搷浣滃洖婊氥傞粯璁や負(fù)false錛?<br />      automaticTestTable錛? C3P0灝嗗緩涓寮犲悕涓篢est鐨勭┖琛紝騫朵嬌鐢ㄥ叾鑷甫鐨勬煡璇㈣鍙ヨ繘琛屾祴璇曘傚鏋滃畾涔変簡(jiǎn)榪欎釜鍙傛暟錛岄偅涔堝睘鎬referredTestQuery灝嗚蹇界暐銆? 浣?涓嶈兘鍦ㄨ繖寮燭est琛ㄤ笂榪涜浠諱綍鎿嶄綔錛屽畠?yōu)畣涓负C3P0嫻嬭瘯鎵鐢紝榛樿涓簄ull錛?<br />      breakAfterAcquireFailure錛?鑾峰彇榪炴帴澶辮觸灝嗕細(xì)寮曡搗鎵鏈夌瓑寰呰幏鍙栬繛鎺ョ殑綰跨▼鎶涘嚭寮傚父銆備絾鏄暟鎹簮浠嶆湁鏁堜繚鐣欙紝騫跺湪涓嬫璋?nbsp;   鐢╣etConnection()鐨勬椂鍊欑戶(hù)緇皾璇曡幏鍙栬繛 鎺ャ傚鏋滆涓簍rue錛岄偅涔堝湪灝濊瘯鑾峰彇榪炴帴澶辮觸鍚庤鏁版嵁婧愬皢鐢蟲(chóng)槑宸叉柇寮騫舵案涔呭叧闂傞粯璁や負(fù) false錛?<br />      checkoutTimeout錛氬綋榪炴帴姹犵敤瀹屾椂瀹㈡埛绔皟鐢╣etConnection()鍚庣瓑寰呰幏鍙栨柊榪炴帴鐨勬椂闂達(dá)紝瓚呮椂鍚庡皢鎶涘嚭SQLException錛屽璁句負(fù)0鍒欐棤闄愭湡絳夊緟銆傚崟浣嶆縐掞紝榛樿涓?錛?<br />      connectionTesterClassName錛? 閫氳繃瀹炵幇ConnectionTester鎴朡ueryConnectionTester鐨勭被鏉ユ祴璇曡繛鎺ワ紝綾誨悕闇璁劇疆涓哄叏闄愬畾鍚嶃傞粯璁や負(fù) com.mchange.v2.C3P0.impl.DefaultConnectionTester錛?<br />      idleConnectionTestPeriod錛氶殧澶氬皯縐掓鏌ユ墍鏈夎繛鎺ユ睜涓殑絀洪棽榪炴帴錛岄粯璁や負(fù)0琛ㄧず涓嶆鏌ワ紱 <br />      initialPoolSize錛氬垵濮嬪寲鏃跺垱寤虹殑榪炴帴鏁幫紝搴斿湪minPoolSize涓巑axPoolSize涔嬮棿鍙栧箋傞粯璁や負(fù)3錛?<br />      maxIdleTime錛氭渶澶х┖闂叉椂闂達(dá)紝瓚呰繃絀洪棽鏃墮棿鐨勮繛鎺ュ皢琚涪寮冦備負(fù)0鎴栬礋鏁板垯姘鎬笉涓㈠純銆傞粯璁や負(fù)0錛?<br />      maxPoolSize錛氳繛鎺ユ睜涓繚鐣欑殑鏈澶ц繛鎺ユ暟銆傞粯璁や負(fù)15錛?<br />      maxStatements錛? JDBC鐨勬爣鍑嗗弬鏁幫紝鐢ㄤ互鎺у埗鏁版嵁婧愬唴鍔犺澆鐨凱reparedStatement鏁伴噺銆備絾鐢變簬棰勭紦瀛樼殑Statement灞? 浜庡崟涓狢onnection 鑰屼笉鏄暣涓繛鎺ユ睜銆傛墍浠ヨ緗繖涓弬鏁伴渶瑕佽冭檻鍒板鏂歸潰鐨勫洜绱狅紝濡傛灉maxStatements涓? maxStatementsPerConnection 鍧囦負(fù)0錛屽垯緙撳瓨琚叧闂傞粯璁や負(fù)0錛?<br />      maxStatementsPerConnection錛氳繛鎺ユ睜鍐呭崟涓繛鎺ユ墍鎷ユ湁鐨勬渶澶х紦瀛楽tatement鏁般傞粯璁や負(fù)0錛?<br />      numHelperThreads錛欳3P0鏄紓姝ユ搷浣滅殑錛岀紦鎱㈢殑JDBC鎿嶄綔閫氳繃甯姪榪涚▼瀹屾垚銆傛墿灞曡繖浜涙搷浣滃彲浠ユ湁鏁堢殑鎻愬崌鎬ц兘錛岄氳繃澶氱嚎紼嬪疄鐜板涓搷浣滃悓鏃惰鎵ц銆傞粯璁や負(fù)3錛?<br />      preferredTestQuery錛氬畾涔夋墍鏈夎繛鎺ユ祴璇曢兘鎵ц鐨勬祴璇曡鍙ャ傚湪浣跨敤榪炴帴嫻嬭瘯鐨勬儏鍐典笅榪欎釜鍙傛暟鑳芥樉钁楁彁楂樻祴璇曢熷害銆傛祴璇曠殑琛ㄥ繀欏誨湪鍒濆鏁版嵁婧愮殑鏃跺欏氨瀛樺湪銆傞粯璁や負(fù)null錛?<br />      propertyCycle錛?鐢ㄦ埛淇敼緋葷粺閰嶇疆鍙傛暟鎵ц鍓嶆渶澶氱瓑寰呯殑縐掓暟銆傞粯璁や負(fù)300錛?<br />      testConnectionOnCheckout錛? 鍥犳ц兘娑堣楀ぇ璇峰彧鍦ㄩ渶瑕佺殑鏃跺欎嬌鐢ㄥ畠銆傚鏋滆涓簍rue閭d箞鍦ㄦ瘡涓猚onnection鎻愪氦鐨勬椂鍊欓兘 灝嗘牎楠屽叾鏈夋晥鎬с傚緩璁嬌鐢? idleConnectionTestPeriod鎴朼utomaticTestTable <br /> 絳夋柟娉曟潵鎻愬崌榪炴帴嫻嬭瘯鐨勬ц兘銆傞粯璁や負(fù)false錛?<br />      testConnectionOnCheckin錛氬鏋滆涓簍rue閭d箞鍦ㄥ彇寰楄繛鎺ョ殑鍚屾椂灝嗘牎楠岃繛鎺ョ殑鏈夋晥鎬с傞粯璁や負(fù)false銆?</p> <p>璇婚厤緗枃浠剁殑鏂瑰紡寮曠敤灞炴э細(xì) </p> <p><bean id="propertyConfigurer"      <br />          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">       <br />      <property name="location" value="/WEB-INF/jdbc.properties"/>       <br /> bean>       <br /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"        <br />          destroy-method="close">       <br />      <property name="driverClassName" value="${jdbc.driverClassName}" />       <br />      <property name="url" value="${jdbc.url}" />       <br />      <property name="username" value="${jdbc.username}" />       <br />      <property name="password" value="${jdbc.password}" />       <br /> bean>    </p> <p>     鍦╦dbc.properties灞炴ф枃浠朵腑瀹氫箟灞炴у鹼細(xì) <br />      jdbc.driverClassName= com.mysql.jdbc.Driver <br />      jdbc.url= jdbc:mysql://localhost:3309/sampledb <br />      jdbc.username=root <br />      jdbc.password=1234 <br />      鎻愮ず 緇忓父鏈夊紑鍙戣呭湪${xxx}鐨勫墠鍚庝笉灝忓績(jī)閿叆涓浜涚┖鏍鹼紝榪欎簺絀烘牸瀛楃灝嗗拰鍙橀噺鍚堝茍鍚庝綔涓哄睘鎬х殑鍊箋傚錛?鐨勫睘鎬ч厤緗」錛屽湪鍓嶅悗閮芥湁絀烘牸錛岃瑙f瀽鍚庯紝username鐨勫間負(fù)“ 1234 ”錛岃繖灝嗛犳垚鏈緇堢殑閿欒錛屽洜姝ら渶瑕佺壒鍒皬蹇?jī)銆?/p> <p>鑾峰彇JNDI鏁版嵁婧?<br />      濡傛灉搴旂敤閰嶇疆鍦ㄩ珮鎬ц兘鐨勫簲鐢ㄦ湇鍔″櫒錛堝WebLogic鎴朩ebsphere絳夛級(jí)涓婏紝鎴戜滑鍙兘鏇村笇鏈涗嬌鐢ㄥ簲鐢ㄦ湇鍔″櫒鏈韓鎻愪緵鐨勬暟鎹簮銆傚簲鐢ㄦ湇鍔″櫒鐨勬暟鎹簮 浣跨敤JNDI寮鏀捐皟鐢ㄨ呬嬌鐢紝Spring涓烘涓撻棬鎻愪緵寮曠敤JNDI璧勬簮鐨凧ndiObjectFactoryBean綾匯備笅闈㈡槸涓涓畝鍗曠殑閰嶇疆錛?/p> <p>xml 浠g爜<br /> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">       <br />      <property name="jndiName" value="java:comp/env/jdbc/bbt"/>       <br /> bean>   </p> <p>閫氳繃jndiName鎸囧畾寮曠敤鐨凧NDI鏁版嵁婧愬悕縐般?<br />      Spring 2.0涓鴻幏鍙朖2EE璧勬簮鎻愪緵浜?jiǎn)涓涓猨ee鍛藉悕絀洪棿錛岄氳繃jee鍛藉悕絀洪棿錛屽彲浠ユ湁鏁堝湴綆鍖朖2EE璧勬簮鐨勫紩鐢ㄣ備笅闈㈡槸浣跨敤jee鍛藉悕絀洪棿寮曠敤JNDI鏁版嵁婧愮殑閰嶇疆錛?</p> <p>xml 浠g爜<br /> <beans xmlns=http://www.springframework.org/schema/beans     <br /> xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance     <br /> xmlns:jee=http://www.springframework.org/schema/jee     <br /> xsi:schemaLocation="<a >http://www.springframework.org/schema/beans</a>      <br /> <a >http://www.springframework.org/schema/beans/spring-beans-2.0.xsd</a>      <br /> <a >http://www.springframework.org/schema/jee</a>     <br /> <a >       <br /> <jee:jndi-lookup id="dataSource" jndi-name=" java:comp/env/jdbc/bbt"/>       <br /> beans>   </p> <p>Spring鐨勬暟鎹簮瀹炵幇綾?<br />      Spring 鏈韓涔熸彁渚涗簡(jiǎn)涓涓畝鍗曠殑鏁版嵁婧愬疄鐜扮被DriverManagerDataSource 錛屽畠浣嶄簬 org.springframework.jdbc.datasource鍖呬腑銆傝繖涓被瀹炵幇浜?jiǎn)javax.sql.DataSource鎺ュ彛錛屼絾 瀹冨茍娌? 鏈夋彁渚涙睜鍖栬繛鎺ョ殑鏈哄埗錛屾瘡嬈¤皟鐢╣etConnection()鑾峰彇鏂拌繛鎺ユ椂錛屽彧鏄畝鍗曞湴鍒涘緩涓涓柊鐨勮繛鎺ャ傚洜姝わ紝榪欎釜鏁版嵁婧愮被姣旇緝閫傚悎鍦ㄥ崟鍏冩祴璇? 鎴栫畝 鍗曠殑鐙珛搴旂敤涓嬌鐢紝鍥犱負(fù)瀹冧笉闇瑕侀澶栫殑渚濊禆綾匯?<br />       涓嬮潰錛屾垜浠潵鐪嬩竴涓婦riverManagerDataSource鐨勭畝鍗曚嬌鐢細(xì)褰撶劧錛屾垜浠篃鍙互閫氳繃閰嶇疆鐨勬柟寮忕洿鎺ヤ嬌鐢―riverManagerDataSource銆?/p> <p>java 浠g爜<br /> DriverManagerDataSource ds = new DriverManagerDataSource ();       <br /> ds.setDriverClassName("com.mysql.jdbc.Driver");       <br /> ds.setUrl("jdbc:mysql://localhost:3309/sampledb");       <br /> ds.setUsername("root");       <br /> ds.setPassword("1234");       <br /> Connection actualCon = ds.getConnection();   </p> <p>灝忕粨 </p> <p>     涓嶇閲囩敤浣曠鎸佷箙鍖栨妧鏈紝閮介渶瑕佸畾涔夋暟鎹簮銆係pring闄勫甫浜?jiǎn)涓や釜鏁版嵁婧愮殑瀹炵幇绫诲寘锛屼綘鍙互鑷閫夋嫨榪涜瀹氫箟銆傚湪瀹為檯閮ㄧ講鏃訛紝鎴戜滑鍙兘浼?xì)鐩存帴閲囩敤搴旂敤鏈? 鍔″櫒鏈韓鎻愪緵鐨勬暟鎹簮錛岃繖鏃訛紝鍒欏彲浠ラ氳繃JndiObjectFactoryBean鎴杍ee鍛藉悕絀洪棿寮曠敤JNDI涓殑鏁版嵁婧愩?</p> <p>DBCP涓嶤3PO閰嶇疆鐨勫尯鍒細(xì)</p> <p>C3PO 錛?/p> <p>xml 浠g爜</p> <p><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">   <br />      <property name="driverClass">   <br />          <value>oracle.jdbc.driver.OracleDrivervalue>   <br />      property>   <br />      <property name="jdbcUrl">              <br />          <value>jdbc:oracle:thin:@10.10.10.6:1521:DataBaseNamevalue>   <br />       property>   <br />      <property name="user">   <br />          <value>testAdminvalue>   <br />      property>   <br />      <property name="password">   <br />          <value>123456value>   <br />      property>   <br /> bean>   </p> <p>DBCP錛?/p> <p>xml 浠g爜<br /> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   <br />      <property name="driverClassName">   <br />          <value>oracle.jdbc.driver.OracleDrivervalue>   <br />      property>   <br />      <property name="url">              <br />          <value>jdbc:oracle:thin:@10.10.10.6:1521:DataBaseNamevalue>   <br />       property>   <br />      <property name="username">   <br />          <value>testAdminvalue>   <br />      property>   <br />      <property name="password">   <br />          <value>123456value>   <br />      property>   <br /> bean></p> </div> <br /> </div> <img src ="http://www.tkk7.com/sealyu/aggbug/311728.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/sealyu/" target="_blank">seal</a> 2010-02-02 22:04 <a href="http://www.tkk7.com/sealyu/archive/2010/02/02/311728.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>SQL 涓?with as 鐢ㄦ硶(杞?http://www.tkk7.com/sealyu/archive/2009/11/12/302068.htmlsealsealThu, 12 Nov 2009 03:12:00 GMThttp://www.tkk7.com/sealyu/archive/2009/11/12/302068.htmlhttp://www.tkk7.com/sealyu/comments/302068.htmlhttp://www.tkk7.com/sealyu/archive/2009/11/12/302068.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/302068.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/302068.htmlwith
sql1 as (select to_char(a) s_name from test_tempa),
R ]F5^5"KFW0n B0sql2 as (select to_char(b) s_name from test_tempb where not exists (select s_name from sql1 where rownum=1))
select * from sql1ITPUB涓漢絀洪棿2g N*` O3y2eB Q6}
union all
select * from sql2
union all
select 'no records' from dual
       where not exists (select s_name from sql1 where rownum=1)
       and not exists (select s_name from sql2 where rownum=1);

鍐嶄婦涓畝鍗曠殑渚嬪瓙

with a as (select * from test)

select * from a;

鍏跺疄灝辨槸鎶婁竴澶у爢閲嶅鐢ㄥ埌鐨凷QL璇彞鏀懼湪with as 閲岄潰錛屽彇涓涓埆鍚嶏紝鍚庨潰鐨勬煡璇㈠氨鍙互鐢ㄥ畠

榪欐牱瀵逛簬澶ф壒閲忕殑SQL璇彞璧峰埌涓涓紭鍖栫殑浣滅敤錛岃屼笖娓呮鏄庝簡(jiǎn)


榪欐槸鎼滅儲(chǔ)鍒扮殑鑻辨枃鏂囨。璧勬枡(璇村緱姣旇緝鍏?浣嗘槸鏈漢鑻辨枃鐗硅彍,榪樻病鍏蜂綋浜?jiǎn)瑙e?甯屾湜鍚勯珮鎵嬪叿浣撹皥璋堣繖涓獁ith
as 鐨勫ソ澶?

About Oracle WITH clause
Starting in Oracle9i release 2 we see an incorporation of the SQL-99 “WITH clause”, a tool for materializing subqueries to save Oracle from having to re-compute them multiple times.

The SQL “WITH clause” is very similar to the use of Global temporary tables (GTT), a technique that is often used to improve query speed for complex subqueries. Here are some important notes about the Oracle “WITH clause”:

   • The SQL “WITH clause” only works on Oracle 9i release 2 and beyond.
   • Formally, the “WITH clause” is called subquery factoring
   • The SQL “WITH clause” is used when a subquery is executed multiple times
   • Also useful for recursive queries (SQL-99, but not Oracle SQL)

To keep it simple, the following example only references the aggregations once, where the SQL “WITH clause” is normally used when an aggregation is referenced multiple times in a query.
We can also use the SQL-99 “WITH clause” instead of temporary tables. The Oracle SQL “WITH clause” will compute the aggregation once, give it a name, and allow us to reference it (maybe multiple times), later in the query.

The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:

WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);


Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH  clause”:

WITH
sum_sales AS
  select /*+ materialize */
    sum(quantity) all_sales from stores
number_stores AS
  select /*+ materialize */
    count(*) nbr_stores from stores
sales_by_store AS
  select /*+ materialize */
  store_name, sum(quantity) store_sales from
  store natural join sales
SELECT
   store_name
FROM
   store,
   sum_sales,
   number_stores,
   sales_by_store
where
   store_sales > (all_sales / nbr_stores)
;


Note the use of the Oracle undocumented “materialize” hint in the “WITH clause”. The Oracle materialize hint is used to ensure that the Oracle cost-based optimizer materializes the temporary tables that are created inside the “WITH” clause. This is not necessary in Oracle10g, but it helps ensure that the tables are only created one time.

It should be noted that the “WITH clause” does not yet fully-functional within Oracle SQL and it does not yet support the use of “WITH clause” replacement for “CONNECT BY” when performing recursive queries.

To see how the “WITH clause” is used in ANSI SQL-99 syntax, here is an excerpt from Jonathan Gennick’s great work “Understanding the WITH Clause” showing the use of the SQL-99 “WITH clause” to traverse a recursive bill-of-materials hierarchy

The SQL-99 “WITH clause” is very confusing at first because the SQL statement does not begin with the word SELECT. Instead, we use the “WITH clause” to start our SQL query, defining the aggregations, which can then be named in the main query as if they were “real” tables:

WITH
subquery_name
AS
(the aggregation SQL statement)
SELECT
(query naming subquery_name);


Retuning to our oversimplified example, let’s replace the temporary tables with the SQL “WITH” clause”:

鍙︿竴涓緥瀛?
with tempDeptName(deptName) as
(
      select
             dept_name as deptName
     from
             bas_dept as dept,tpp_materialmuster as muster
     where
             dept.DEPT_ID = muster.NEEDUNIT
           
     union all
     
     select
             corp_name as deptName
     from
              bas_corp as corp,tpp_materialmuster as muster
     where
             corp.corp_id = muster.NEEDUNIT
           
),
tempProjInfo(projName, projCode) as
(
      select
             etfprojName as projName,
            etfprojCode as projCode
     from
             tbi_etfproj as etf, tpp_materialMuster as muster
     where
             etf.etfprojid = muster.projid
           
     union all
     
     select
             etmprojName as projName,
            etmprojCode as projCode
     from
              tbi_etmproj as etm, tpp_materialMuster as muster
     where
               etm.etmprojId = muster.projid
)

select
                   deptname,
                   projname,
                   projcode
      from     tpp_materialmuster as muster,tempDeptName,tempProjInfo


seal 2009-11-12 11:12 鍙戣〃璇勮
]]>
How to convert from string to datetime in sql server?http://www.tkk7.com/sealyu/archive/2009/11/05/301216.htmlsealsealThu, 05 Nov 2009 01:59:00 GMThttp://www.tkk7.com/sealyu/archive/2009/11/05/301216.htmlhttp://www.tkk7.com/sealyu/comments/301216.htmlhttp://www.tkk7.com/sealyu/archive/2009/11/05/301216.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/301216.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/301216.html闃呰鍏ㄦ枃

seal 2009-11-05 09:59 鍙戣〃璇勮
]]>
鏍囨敞鏋氫婦綾誨瀷@Enumerated錛堣漿錛?/title><link>http://www.tkk7.com/sealyu/archive/2009/06/30/284749.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Tue, 30 Jun 2009 05:18:00 GMT</pubDate><guid>http://www.tkk7.com/sealyu/archive/2009/06/30/284749.html</guid><wfw:comment>http://www.tkk7.com/sealyu/comments/284749.html</wfw:comment><comments>http://www.tkk7.com/sealyu/archive/2009/06/30/284749.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/sealyu/comments/commentRss/284749.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/sealyu/services/trackbacks/284749.html</trackback:ping><description><![CDATA[<p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹?huà)浣?">瀹炰綋</span><font face="Times New Roman">Entity</font><span style="font-family: 瀹?huà)浣?">涓氳繃</span><font face="Times New Roman">@Enumerated</font><span style="font-family: 瀹?huà)浣?">鏍囨敞鏋氫婦綾誨瀷錛屼緥濡傚皢</span><font face="Times New Roman">CustomerEO</font><span style="font-family: 瀹?huà)浣?">瀹炰綋涓鍔犱竴涓?/span><font face="Times New Roman">CustomerType</font><span style="font-family: 瀹?huà)浣?">綾誨瀷鐨勬灇涓懼瀷灞炴э紝鏍囨敞瀹炰綋鍚庣殑浠g爜濡備笅鎵紺恒?/span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">@Entity</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">@Table(name = "customer")</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">public class CustomerEO implements java.io.Serializable {</font></p> <p style="margin: 0cm 0cm 0pt auto; text-indent: 21pt;"><span style="font-family: 榛戜綋;"><font style="background-color: #e0e0e0;">……</font></span></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         private CustomerType type;</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;"> </font></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">         @Enumerated(EnumType.STRING)</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         public CustomerType getType() {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">                   return type;</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         }</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;"> </font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         public void setType(CustomerType type) {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">                   this.type = type;</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         }</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;"> </font></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">         public enum CustomerType {</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">                   COMPETITOR, INVESTOR, PARTNER, VENDER</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">         }</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;"> </font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">}</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹?huà)浣?">鍦ㄥ疄浣撲腑铏界劧鏍囨敞鎴愭灇涓劇被鍨嬶紝浣嗗綋瀹炰綋鎸佷箙鍖栧悗錛岃〃涓墍瀵瑰簲鐨勫間粛鏃ф槸鍩烘湰鐨勬暟鎹被鍨嬶紝浠ヤ笂浠g爜鍒涘緩琛ㄧ殑</span><font face="Times New Roman">SQL</font><span style="font-family: 瀹?huà)浣?">璇彞鏄細(xì)</span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">CREATE TABLE customer (</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         id int(20) NOT NULL auto_increment,</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         name varchar(255),</font></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">         type varchar(255),</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         PRIMARY KEY (id)</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">)</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><span style="font-family: 瀹?huà)浣?"><font size="3">浣跨敤鏋氫婦綾誨瀷鍚庯紝鍦ㄥ垱寤哄疄浣撴椂渚垮彲浠ョ洿鎺ュ紩鐢ㄦ灇涓劇被鍨嬶紝渚嬪浠ヤ笅浠g爜鎵紺恒?/font></span></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">CustomerEO customer = new CustomerEO();</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">customer.setName("Janet2");</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">customer.setType(<strong>CustomerType.PARTNER</strong>);</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">entityManager.persist(customer);</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹?huà)浣?">鍦ㄤ嬌鐢?/span><font face="Times New Roman">@Enumerated</font><span style="font-family: 瀹?huà)浣?">娉ㄩ噴鏃訛紝闇瑕佹敞鎰忎互涓嬪嚑涓棶棰橈細(xì)</span></font></p> <p style="margin: 0cm 0cm 0pt 21pt;"><span style="font-family: Wingdings;"><font size="3">l</font><span style="font-family: "Times New Roman"; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">         </span></span><font size="3"><span style="font-family: 瀹?huà)浣?">鍥犱負(fù)鏋氫婦綾誨瀷鐨勬湁鍚嶇О鍜屽間袱涓睘鎬э紝鎵浠ュ湪鎸佷箙鍖栨椂鍙互閫夋嫨鎸佷箙鍖栧悕縐版垨鏄寔涔呭寲鍊箋傞氳繃</span><font face="Times New Roman">EnumType</font><span style="font-family: 瀹?huà)浣?">鏉ュ畾涔夛紝瀹冩湁涓や釜鍊煎涓嬫墍紺恒?/span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">public enum EnumType {</font></p> <p style="margin: 0cm 0cm 0pt auto; text-indent: 21pt;"><font style="background-color: #e0e0e0;">ORDINAL,</font></p> <p style="margin: 0cm 0cm 0pt auto; text-indent: 21pt;"><font style="background-color: #e0e0e0;">STRING</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">}</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><font face="Times New Roman">ORDINAL</font><span style="font-family: 瀹?huà)浣?">琛ㄧず鎸佷箙鍖栫殑涓烘灇涓劇被鍨嬬殑鍊鹼紝</span><font face="Times New Roman">STRING</font><span style="font-family: 瀹?huà)浣?">琛ㄧず鎸佷箙鍖栫殑涓烘灇涓劇被鍨嬬殑鍚嶇О銆傞粯璁や負(fù)</span><font face="Times New Roman">ORDINAL</font><span style="font-family: 瀹?huà)浣?">錛屾寔涔呭寲鍊箋備緥濡備互涓婄ず渚嬩腑鏍囨敞鐨勪負(fù)</span><font face="Times New Roman">STRING</font><span style="font-family: 瀹?huà)浣?">錛岃繖鏍鋒寔涔呭寲瀹炰綋鍚庯紝鏁版嵁搴撲腑淇濆瓨鐨勬槸鏋氫婦綾誨瀷鐨勫悕縐幫紝濡傚浘</span><span style="font-family: 瀹?huà)浣?">鎵紺恒?/span></font></p> <p style="margin: 0cm 0cm 12pt;" align="center"><span style="font-family: 瀹?huà)浣?"><img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/EJB_JPA/5.5.jpg" /></span></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹?huà)浣?">鑻ユ鏃舵敼鎴?/span><font face="Times New Roman">ORDINAL</font><span style="font-family: 瀹?huà)浣?">錛屼唬鐮佸涓嬶細(xì)</span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">         @Enumerated(EnumType.ORDINAL)</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         public CustomerType getType() {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">                   return type;</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         }</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹?huà)浣?">鍒欏悓鏍鋒寔涔呭寲鐨勫疄浣撳悗錛屾暟鎹簱淇濆瓨鐨勭粨鏋滃鍥?/span><span style="font-family: 瀹?huà)浣?">鎵紺恒?/span></font></p> <p style="margin: 0cm 0cm 12pt;" align="center"><span style="font-family: 瀹?huà)浣?"><img alt="" src="http://p.blog.csdn.net/images/p_blog_csdn_net/EJB_JPA/5.6.jpg" /></span></p> <p style="margin: 0cm 0cm 0pt 21pt;"><span style="font-family: Wingdings;"><font size="3">l</font><span style="font-family: "Times New Roman"; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">         </span></span><font size="3"><span style="font-family: 瀹?huà)浣?">濡備綍閫夋嫨</span><font face="Times New Roman">STRING</font><span style="font-family: 瀹?huà)浣?">鍜?/span><font face="Times New Roman">ORDINAL</font><span style="font-family: 瀹?huà)浣?">錛?/span></font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹?huà)浣?">濡傛灉浣跨敤</span><font face="Times New Roman">STRING</font><span style="font-family: 瀹?huà)浣?">淇濆瓨錛岃櫧鐒朵粠鏁版嵁搴撲腑鏌ヨ鏁版嵁鏃墮潪甯哥洿瑙傦紝鑳藉娓呮鐨勭湅鍑?guó)櫙ゾc誨瀷浠h〃鎰忎箟錛屼絾榪欐牱涔熶細(xì)甯︽潵鍏朵粬鐨勯棶棰樸傝嫢姝ゆ椂鏋氫婦綾誨瀷鐨勫畾涔夋敼鍙橈紝渚嬪涓婁緥涓殑鏋氫婦綾誨瀷鍚嶇О鏀逛負(fù)錛?/span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         public enum CustomerType {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">                   CUST_COMPETITOR, INVESTOR, PARTNER, VENDER</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         }</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹?huà)浣?">鍒欐鏃舵暟鎹簱涓繚瀛樼殑“</span><font face="Times New Roman">COMPETITOR</font><span style="font-family: 瀹?huà)浣?">”鐨勫煎皢涓嶈兘杞寲涓烘灇涓劇被鍨?/span><font face="Times New Roman">CustomerType</font><span style="font-family: 瀹?huà)浣?">涓殑“</span><font face="Times New Roman">CUST_COMPETITOR</font><span style="font-family: 瀹?huà)浣?">”鐨勫箋備絾鑻ヤ嬌鐢?/span><font face="Times New Roman">ORDINAL</font><span style="font-family: 瀹?huà)浣?">鍒欎笉浼?xì)甯︽潵杩櫩U嶉棶棰樸傛墍浠ュ緩璁嬌鐢?/span><font face="Times New Roman">ORDINAL</font><span style="font-family: 瀹?huà)浣?">綾誨瀷鏉ユ寔涔呭寲鏋氫婦綾誨瀷銆?/span></font></p> <p style="margin: 0cm 0cm 0pt 21pt;"><span style="font-family: Wingdings;"><font size="3">l</font><span style="font-family: "Times New Roman"; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal;">         </span></span><font size="3"><span style="font-family: 瀹?huà)浣?">鏋氫婦綾誨瀷鐨勫畾涔変綅緗紝瀹炰綋澶栭儴</span><font face="Times New Roman">VS</font><span style="font-family: 瀹?huà)浣?">瀹炰綋鍐呴儴銆?/span></font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹?huà)浣?">涓婁緥涓?/span><font face="Times New Roman">CustomerType</font><span style="font-family: 瀹?huà)浣?">鏋氫婦綾誨瀷瀹氫箟鍦?/span><font face="Times New Roman">CustomerEO</font><span style="font-family: 瀹?huà)浣?">瀹炰綋鍐呴儴錛岃繖鏄洜涓哄彧鏈?/span><font face="Times New Roman">CustomerEO</font><span style="font-family: 瀹?huà)浣?">榪欎釜瀹炰綋浼?xì)鋴社?/span><font face="Times New Roman">CustomerType</font><span style="font-family: 瀹?huà)浣?">綾誨瀷錛屽叾浠栫殑瀹炰綋涓嶄細(xì)浣跨敤璇ョ被鍨嬨傝綾誨瀷涓庤繖涓疄浣撳叧緋葷揣瀵嗚仈緋匯?/span></font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹?huà)浣?">浣嗚嫢姝ゆ椂澶氫釜瀹炰綋鍏敤涓涓灇涓劇被鍨嬫椂錛屽垯鍙互灝嗘灇涓劇被鍨嬪崟鐙畾涔夛紝瀹氫箟鍦ㄥ疄浣撶殑澶栭儴銆傛湁榪欐牱涓涓灇涓劇被鍨?/span><font face="Times New Roman">BusinessLine</font><span style="font-family: 瀹?huà)浣?">錛屽畠瀹氫箟鍦ㄥ疄浣撳閮紝浠g爜濡備笅錛?/span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">public enum BusinessLine {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         REAL_ESTATE,FINANCE, NON_PROFIT</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">}</font></p> <p style="margin: 0cm 0cm 0pt; text-indent: 21pt;"><font size="3"><span style="font-family: 瀹?huà)浣?">渚嬪</span><font face="Times New Roman">CustomerEO</font><span style="font-family: 瀹?huà)浣?">瀹炰綋澧炲姞涓涓?/span><font face="Times New Roman">BusinessLine</font><span style="font-family: 瀹?huà)浣?">鐨勬灇涓劇被鍨嬶紝浠g爜濡備笅鎵紺恒?/span></font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">@Entity</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">@Table(name = "customer")</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">public class CustomerEO implements java.io.Serializable {</font></p> <p style="margin: 0cm 0cm 0pt auto; text-indent: 21pt;"><span style="font-family: 榛戜綋;"><font style="background-color: #e0e0e0;">……</font></span></p> <p style="margin: 0cm 0cm 0pt auto;"><strong><font style="background-color: #e0e0e0;">         private BusinessLine businessLine;</font></strong></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;"> </font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         @Enumerated(EnumType.STRING)</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         public BusinessLine getBusinessLine() {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">                   return businessLine;</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         }</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;"> </font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         public void setBusinessLine(BusinessLine businessLine) {</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">                   this.businessLine = businessLine;</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">         }</font></p> <p style="margin: 0cm 0cm 0pt auto;"><font style="background-color: #e0e0e0;">}</font></p> <img src ="http://www.tkk7.com/sealyu/aggbug/284749.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/sealyu/" target="_blank">seal</a> 2009-06-30 13:18 <a href="http://www.tkk7.com/sealyu/archive/2009/06/30/284749.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title> JAVA榪炴帴姹?杞?http://www.tkk7.com/sealyu/archive/2009/03/13/259574.htmlsealsealFri, 13 Mar 2009 08:58:00 GMThttp://www.tkk7.com/sealyu/archive/2009/03/13/259574.htmlhttp://www.tkk7.com/sealyu/comments/259574.htmlhttp://www.tkk7.com/sealyu/archive/2009/03/13/259574.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/259574.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/259574.htmlpackage com.xinnuo.jdbc;

import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;

 /*
  * 璇ョ被鍙兘鍒涘緩涓涓疄渚嬶紝鍏跺畠瀵硅薄鑳藉璋冪敤鍏墮潤(rùn)鎬佹柟娉曪紙涔熺О涓虹被鏂規(guī)硶錛夎幏寰楄鍞竴瀹炰緥鐨勫紩鐢ㄣ?br />   * DBConnectionManager綾葷殑寤烘瀯鍑芥暟鏄鏈夌殑錛岃繖鏄負(fù)浜?jiǎn)閬垮厤鍏跺畠瀵硅薄鍒涘圾櫙ゾc葷殑瀹炰緥.
  * DBConnectionManager綾葷殑瀹㈡埛紼嬪簭鍙互璋冪敤getInstance()鏂規(guī)硶鑾峰緱瀵硅綾誨敮涓瀹炰緥鐨勫紩鐢ㄣ?br />   * 綾葷殑鍞竴瀹炰緥鍦╣etInstance()鏂規(guī)硶絎竴嬈¤璋冪敤鏈熼棿鍒涘緩錛屾鍚庡叾寮曠敤灝變竴鐩翠繚瀛樺湪闈?rùn)鎬佸彉閲?br />   * instance涓傛瘡嬈¤皟鐢╣etInstance()閮藉鍔犱竴涓狣BConnectionManager鐨勫鎴風(fēng)▼搴忚鏁般?br />   * 鍗籌紝璇ヨ鏁頒唬琛ㄥ紩鐢―BConnectionManager鍞竴瀹炰緥鐨勫鎴風(fēng)▼搴忔繪暟錛屽畠?yōu)畣琚敤浜庢帶鍒稑q炴帴姹犵殑
  * 鍏抽棴鎿嶄綔銆?璇ョ被瀹炰緥鐨勫垵濮嬪寲宸ヤ綔縐佹湁鏂規(guī)硶init()瀹屾垚銆傚叾涓?getResourceAsStream()鏂規(guī)硶
  * 鐢ㄤ簬瀹氫綅騫舵墦寮澶栭儴鏂囦歡銆傚閮ㄦ枃浠剁殑瀹氫綅鏂規(guī)硶渚濊禆浜庣被瑁呰澆鍣ㄧ殑瀹炵幇銆傛爣鍑嗙殑鏈湴綾昏杞藉櫒鏌ユ壘鎿?br />   * 浣滄繪槸寮濮嬩簬綾繪枃浠舵墍鍦ㄨ礬寰勶紝涔熻兘澶熸悳绱LASSPATH涓0鏄庣殑璺緞銆俤b.properties鏄竴涓睘鎬?br />   * 鏂囦歡錛屽畠鍖呭惈瀹氫箟榪炴帴姹犵殑閿?鍊煎銆傚彲渚涘畾涔夌殑鍏敤灞炴у涓嬶細(xì)
  *   drivers 浠ョ┖鏍煎垎闅旂殑JDBC椹卞姩紼嬪簭綾誨垪琛?"
  *   logfile 鏃ュ織鏂囦歡鐨勭粷瀵硅礬寰?
  *  鍏跺畠鐨勫睘鎬у拰鐗瑰畾榪炴帴姹犵浉鍏籌紝鍏跺睘鎬у悕瀛楀墠搴斿姞涓婅繛鎺ユ睜鍚嶅瓧錛?
  *  < poolname>.url 鏁版嵁搴撶殑 JDBC URL
  *  < poolname>.maxconn 鍏佽寤虹珛鐨勬渶澶ц繛鎺ユ暟錛?琛ㄧず娌℃湁闄愬埗
  *  < poolname>.user 鐢ㄤ簬璇ヨ繛鎺ユ睜鐨勬暟鎹簱甯愬彿
  *  < poolname>.password 鐩稿簲鐨勫瘑鐮?"
  * 鍏朵腑url灞炴ф槸蹇呴渶鐨勶紝鑰屽叾瀹冨睘鎬у垯鏄彲閫夌殑銆傛暟鎹簱甯愬彿鍜屽瘑鐮佸繀欏誨悎娉曘傜敤浜嶹indows騫沖彴鐨?br />   * db.properties鏂囦歡紺轟緥濡備笅錛?br />   *  drivers=com.microsoft.jdbc.sqlserver.SQLServerDriver
  *  logfile=D:""log.txt
  *  access.maxconn=20
  *  access.url=jdbc:microsoft:sqlserver://localhost:1433;databasename=web
  *  access.user=sa
  *  access.password=sa 
  * 娉ㄦ剰鍦╓indows璺緞涓殑鍙嶆枩鏉犲繀欏昏緭鍏?涓紝榪欐槸鐢變簬灞炴ф枃浠朵腑鐨勫弽鏂滄潬鍚屾椂涔熸槸涓涓漿涔夊瓧絎︺?br />   * init()鏂規(guī)硶鍦ㄥ垱寤哄睘鎬у璞″茍璇誨彇db.properties鏂囦歡涔嬪悗錛屽氨寮濮嬫鏌ogfile灞炴с傚鏋滃睘
  * 鎬ф枃浠朵腑娌℃湁鎸囧畾鏃ュ織鏂囦歡錛屽垯榛樿涓哄綋鍓嶇洰褰曚笅鐨凞BConnectionManager.log鏂囦歡銆傚鏃ュ織鏂?br />   * 浠舵棤娉曚嬌鐢紝鍒欏悜System.err杈撳嚭鏃ュ織璁板綍銆傝杞藉拰娉ㄥ唽鎵鏈夊湪drivers灞炴т腑鎸囧畾鐨凧DBC椹卞姩
  * 紼嬪簭loadDrivers()鏂規(guī)硶瀹炵幇銆傝鏂規(guī)硶鍏堢敤StringTokenizer灝哾rivers灞炴у煎垎鍓蹭負(fù)瀵瑰簲浜庨┍
  * 鍔ㄧ▼搴忓悕縐扮殑瀛楃涓詫紝鐒跺悗渚濇瑁呰澆榪欎簺綾誨茍鍒涘緩鍏跺疄渚嬶紝鏈鍚庡湪DriverManager涓敞鍐岃瀹炰緥騫舵妸
  * 瀹冨姞鍏ュ埌涓涓鏈夌殑鍚戦噺drivers銆傚悜閲廳rivers灝嗙敤浜庡叧闂湇鍔℃椂浠嶥riverManager鍙栨秷鎵鏈?br />   * JDBC 椹卞姩紼嬪簭鐨勬敞鍐屻俰nit()鏂規(guī)硶鐨勬渶鍚庝竴涓換鍔℃槸璋冪敤縐佹湁鏂規(guī)硶createPools()鍒涘緩榪炴帴姹犲
  * 璞°俢reatePools()鏂規(guī)硶鍏堝垱寤烘墍鏈夊睘鎬у悕瀛楃殑鏋氫婦瀵硅薄錛堝嵆Enumeration瀵硅薄錛岃瀵硅薄鍙互鎯寵薄
  * 涓轟竴涓厓绱犵郴鍒楋紝閫愭璋冪敤鍏秐extElement()鏂規(guī)硶灝嗛『搴忚繑鍥炲悇鍏冪礌錛夛紝鐒跺悗鍦ㄥ叾涓悳绱㈠悕瀛椾互“.url”
  * 緇撳熬鐨勫睘鎬с傚浜庢瘡涓涓鍚堟潯浠剁殑灞炴э紝鍏堟彁鍙栧叾榪炴帴姹犲悕瀛楅儴鍒嗭紝榪涜岃鍙栨墍鏈夊睘浜庤榪炴帴姹犵殑灞炴э紝
  * 鏈鍚庡垱寤鴻繛鎺ユ睜瀵硅薄騫舵妸瀹冧繚瀛樺湪瀹炰緥鍙橀噺pools涓傛暎鍒楄〃錛圚ashtable綾?錛塸ools瀹炵幇榪炴帴姹犲悕瀛?br />   * 鍒拌繛鎺ユ睜瀵硅薄涔嬮棿鐨勬槧灝勶紝姝ゅ浠ヨ繛鎺ユ睜鍚嶅瓧涓洪敭錛岃繛鎺ユ睜瀵硅薄涓哄箋?涓轟究浜庡鎴風(fēng)▼搴忎粠鎸囧畾榪炴帴姹犺幏
  * 寰楀彲鐢ㄨ繛鎺ユ垨灝嗚繛鎺ヨ繑鍥炵粰榪炴帴姹狅紝綾籇BConnectionManager鎻愪緵浜?jiǎn)鏂规硶getConnection()鍜?br />   * freeConnection()銆傛墍鏈夎繖浜涙柟娉曢兘瑕佹眰鍦ㄥ弬鏁頒腑鎸囧畾榪炴帴姹犲悕瀛楋紝鍏蜂綋鐨勮繛鎺ヨ幏鍙栨垨榪斿洖鎿嶄綔鍒欒皟
  * 鐢ㄥ搴旂殑榪炴帴姹犲璞″畬鎴愩備負(fù)瀹炵幇榪炴帴姹犵殑瀹夊叏鍏抽棴錛孌BConnectionManager鎻愪緵浜?jiǎn)鏂规硶r(shí)elease()銆?br />   * 鍦ㄤ笂闈㈡垜浠凡緇忔彁鍒幫紝鎵鏈塂BConnectionManager鐨勫鎴風(fēng)▼搴忛兘搴旇璋冪敤闈?rùn)鎬佹柟娉昰etInstance()
  * 浠ヨ幏寰楄綆$悊鍣ㄧ殑寮曠敤錛屾璋冪敤灝嗗鍔犲鎴風(fēng)▼搴忚鏁般傚鎴風(fēng)▼搴忓湪鍏抽棴鏃惰皟鐢╮elease()鍙互閫掑噺璇ヨ鏁般?br />   * 褰撴渶鍚庝竴涓鎴風(fēng)▼搴忚皟鐢╮elease()錛岄掑噺鍚庣殑寮曠敤璁℃暟涓?錛屽氨鍙互璋冪敤鍚勪釜榪炴帴姹犵殑release()鏂規(guī)硶
  * 鍏抽棴鎵鏈夎繛鎺ヤ簡(jiǎn)銆傜鐞嗙被release()鏂規(guī)硶鏈鍚庣殑浠誨姟鏄挙閿鎵鏈塉DBC椹卞姩紼嬪簭鐨勬敞鍐屻?br />   */

/**
 * 綆$悊綾籇BConnectionManager鏀寔瀵逛竴涓垨澶氫釜鐢卞睘鎬ф枃浠跺畾涔夌殑鏁版嵁搴撹繛鎺?br />  * 姹犵殑璁塊棶.瀹㈡埛紼嬪簭鍙互璋冪敤getInstance()鏂規(guī)硶璁塊棶鏈被鐨勫敮涓瀹炰緥.
 */
public class DBConnectionManager {
 static private DBConnectionManager instance; // 鍞竴瀹炰緥

 static private int clients;

 private Vector drivers = new Vector();

 private PrintWriter log;

 private Hashtable pools = new Hashtable();

 /**
  * 榪斿洖鍞竴瀹炰緥.濡傛灉鏄涓嬈¤皟鐢ㄦ鏂規(guī)硶,鍒欏垱寤哄疄渚?br />   * @return DBConnectionManager 鍞竴瀹炰緥
  */
 static synchronized public DBConnectionManager getInstance() {
  if (instance == null) {
   instance = new DBConnectionManager();
  }
  clients++;
  return instance;
 }

 /**
  * 寤烘瀯鍑芥暟縐佹湁浠ラ槻姝㈠叾瀹冨璞″垱寤烘湰綾誨疄渚?br />   */
 private DBConnectionManager() {
  init();
 }

 /**
  * * 灝嗚繛鎺ュ璞¤繑鍥炵粰鐢卞悕瀛楁寚瀹氱殑榪炴帴姹?br />   * @param name鍦ㄥ睘鎬ф枃浠朵腑瀹氫箟鐨勮繛鎺ユ睜鍚嶅瓧
  * @param con榪炴帴瀵硅薄""""r
  */
 public void freeConnection(String name, Connection con) {
  DBConnectionPool pool = (DBConnectionPool) pools.get(name);
  if (pool != null) {
   pool.freeConnection(con);
  }
 }

 /**
  * 鑾峰緱涓涓彲鐢ㄧ殑(絀洪棽鐨?榪炴帴.濡傛灉娌℃湁鍙敤榪炴帴,涓斿凡鏈夎繛鎺ユ暟灝忎簬鏈澶ц繛鎺ユ暟 053 * 闄愬埗,鍒欏垱寤哄茍榪斿洖鏂拌繛
  * @param name鍦ㄥ睘鎬ф枃浠朵腑瀹氫箟鐨勮繛鎺ユ睜鍚嶅瓧 056 *
  * @return Connection 鍙敤榪炴帴鎴杗ull 057
  */
 public Connection getConnection(String name) {
  DBConnectionPool pool = (DBConnectionPool) pools.get(name);
  if (pool != null) {
   return pool.getConnection();
  }
  return null;
 }

 /**
  * 鑾峰緱涓涓彲鐢ㄨ繛鎺?鑻ユ病鏈夊彲鐢ㄨ繛鎺?涓斿凡鏈夎繛鎺ユ暟灝忎簬鏈澶ц繛鎺ユ暟闄愬埗,
  * 鍒欏垱寤哄茍榪斿洖鏂拌繛鎺?鍚﹀垯,鍦ㄦ寚瀹氱殑鏃墮棿鍐呯瓑寰呭叾瀹冪嚎紼嬮噴鏀捐繛鎺?
  * @param name 榪炴帴姹犲悕瀛?071 *
  * @param time浠ユ縐掕鐨勭瓑寰呮椂闂?a>""""r
  * @return Connection 鍙敤榪炴帴鎴杗ull
  */
 public Connection getConnection(String name, long time) {
  DBConnectionPool pool = (DBConnectionPool) pools.get(name);
  if (pool != null) {
   return pool.getConnection(time);
  }
  return null;
 }

 /**
  * 鍏抽棴鎵鏈夎繛鎺?鎾ら攢椹卞姩紼嬪簭鐨勬敞鍐?a>""""r
  */
 public synchronized void release() {
  // 絳夊緟鐩村埌鏈鍚庝竴涓鎴風(fēng)▼搴忚皟鐢?br />   if (--clients != 0) {
   return;
  }

  Enumeration allPools = pools.elements();
  while (allPools.hasMoreElements()) {
   DBConnectionPool pool = (DBConnectionPool) allPools.nextElement();
   pool.release();
  }
  Enumeration allDrivers = drivers.elements();
  while (allDrivers.hasMoreElements()) {
   Driver driver = (Driver) allDrivers.nextElement();
   try {
    DriverManager.deregisterDriver(driver);
    log("鎾ら攢JDBC椹卞姩紼嬪簭 " + driver.getClass().getName() + "鐨勬敞鍐?);
   } catch (SQLException e) {
    log(e, "鏃犳硶鎾ら攢涓嬪垪JDBC椹卞姩紼嬪簭鐨勬敞鍐? " + driver.getClass().getName());
   }
  }
 }

 /**
  * 鏍規(guī)嵁鎸囧畾灞炴у垱寤鴻繛鎺ユ睜瀹炰緥.
  * @param props 榪炴帴姹犲睘鎬?113
  */
 private void createPools(Properties props) {
  Enumeration propNames = props.propertyNames();
  while (propNames.hasMoreElements()) {
   String name = (String) propNames.nextElement();
   if (name.endsWith(".url")) {
    String poolName = name.substring(0, name.lastIndexOf("."));
    String url = props.getProperty(poolName + ".url");
    if (url == null) {
     log("娌℃湁涓鴻繛鎺ユ睜" + poolName + "鎸囧畾URL");
     continue;
    }
    String user = props.getProperty(poolName + ".user");
    String password = props.getProperty(poolName + ".password");
    String maxconn = props.getProperty(poolName + ".maxconn", "0");
    int max;
    try {
     max = Integer.valueOf(maxconn).intValue();
    } catch (NumberFormatException e) {
     log("閿欒鐨勬渶澶ц繛鎺ユ暟闄愬埗: " + maxconn + " .榪炴帴姹? " + poolName);
     max = 0;
    }
    DBConnectionPool pool = new DBConnectionPool(poolName, url,
      user, password, max);
    pools.put(poolName, pool);
    log("鎴愬姛鍒涘緩榪炴帴姹? + poolName);
   }
  }
 }

 /**
  * 璇誨彇灞炴у畬鎴愬垵濮嬪寲
  */
 private void init() {
  InputStream is = getClass().getResourceAsStream("/db.properties");
  Properties dbProps = new Properties();
  try {
   dbProps.load(is);
  } catch (Exception e) {
   System.err.println("涓嶈兘璇誨彇灞炴ф枃浠? "+"璇風(fēng)‘淇漝b.properties鍦–LASSPATH鎸囧畾鐨勮礬寰勪腑");
   return;
  }
  String logFile = dbProps.getProperty("logfile","DBConnectionManager.log");
  try {
   log = new PrintWriter(new FileWriter(logFile, true), true);
  } catch (IOException e) {
   System.err.println("鏃犳硶鎵撳紑鏃ュ織鏂囦歡: " + logFile);
   log = new PrintWriter(System.err);
  }
  loadDrivers(dbProps);
  createPools(dbProps);
 }

 /**
  * 瑁呰澆鍜屾敞鍐屾墍鏈塉DBC椹卞姩紼嬪簭
  * @param props灞炴?br />   */
 private void loadDrivers(Properties props) {
  String driverClasses = props.getProperty("drivers");
  StringTokenizer st = new StringTokenizer(driverClasses);
  while (st.hasMoreElements()) {
   String driverClassName = st.nextToken().trim();
   try {
    Driver driver = (Driver) Class.forName(driverClassName).newInstance();
    DriverManager.registerDriver(driver);
    drivers.addElement(driver);
    log("鎴愬姛娉ㄥ唽JDBC椹卞姩紼嬪簭" + driverClassName);
   } catch (Exception e) {
    e.printStackTrace();
    log("鏃犳硶娉ㄥ唽JDBC椹卞姩紼嬪簭: " + driverClassName + ", 閿欒: " + e);
   }
  }
 }

 /**
  * 灝嗘枃鏈俊鎭啓鍏ユ棩蹇楁枃浠?br />   */
 private void log(String msg) {
  log.println(new Date() + ": " + msg);
 }

 /**
  * 灝嗘枃鏈俊鎭笌寮傚父鍐欏叆鏃ュ織鏂囦歡
  */
 private void log(Throwable e, String msg) {
  log.println(new Date() + ": " + msg);
  e.printStackTrace(log);
 }

 // ///////////////////////////////////////////////////////////////////////////////////////////////////////
 // ///////////////////////////////////////////////////////////////////////////////////////////////////////
 /*
  * DBConnectionPool瀹炵幇錛屽畠琛ㄧず鎸囧悜鏌愪釜鏁版嵁搴撶殑榪炴帴姹犮傛暟鎹簱鐢盝DBC URL鏍囪瘑銆備竴涓狫DBCURL鐢變笁閮ㄥ垎緇勬垚錛氬崗璁爣璇嗭紙鎬繪槸jdbc錛夛紝
  * 椹卞姩紼嬪簭鏍囪瘑錛堝odbc銆乮db銆乷racle絳夛級(jí)錛屾暟鎹簱鏍囪瘑錛堝叾鏍煎紡渚濊禆浜庨┍鍔ㄧ▼搴忥級(jí)銆備緥濡傦紝jdbc:odbc:demo錛屽嵆鏄竴涓寚鍚慸emo鏁版嵁
  * 搴撶殑JDBCURL錛岃屼笖璁塊棶璇ユ暟鎹簱瑕佷嬌鐢↗DBC-ODBC椹卞姩紼嬪簭銆傛瘡涓繛鎺ユ睜閮芥湁涓涓緵瀹㈡埛紼嬪簭浣跨敤鐨勫悕瀛椾互鍙?qiáng)鍙夌殑鐢ㄦ埛甯愬彿銆佸瘑鐮併佹渶
  * 澶ц繛鎺ユ暟闄愬埗銆傚鏋淲eb搴旂敤紼嬪簭鎵鏀寔鐨勬煇浜涙暟鎹簱鎿嶄綔鍙互琚墍鏈夌敤鎴鋒墽琛岋紝鑰屽叾瀹冧竴浜涙搷浣滃簲鐢辯壒鍒鍙殑鐢ㄦ埛鎵ц錛屽垯鍙互涓轟袱綾繪搷浣?br />   * 鍒嗗埆瀹氫箟榪炴帴姹狅紝涓や釜榪炴帴姹犱嬌鐢ㄧ浉鍚岀殑JDBC URL錛屼絾浣跨敤涓嶅悓鐨勫笎鍙峰拰瀵嗙爜銆傜被DBConnectionPool鐨勫緩鏋勫嚱鏁伴渶瑕佷笂榪版墍鏈夋暟鎹綔涓哄叾
  * 鍙傛暟銆傚鎴風(fēng)▼搴忓彲浠ヤ嬌鐢―BConnectionPool
  * 綾繪彁渚涚殑涓や釜鏂規(guī)硶鑾峰彇鍙敤榪炴帴銆備袱鑰呯殑鍏卞悓涔嬪鍦ㄤ簬錛氬榪炴帴姹犱腑瀛樺湪鍙敤榪炴帴錛屽垯鐩存帴榪斿洖錛屽惁鍒欏垱寤烘柊鐨勮繛鎺ュ茍榪斿洖銆傚鏋滄病鏈夊彲鐢ㄨ繛鎺?br />   * 涓斿凡鏈夎繛鎺ユ繪暟絳変簬鏈澶ч檺鍒舵暟錛岀涓涓柟娉曞皢鐩存帴榪斿洖null錛岃岀浜屼釜鏂規(guī)硶灝嗙瓑寰呯洿鍒版湁鍙敤榪炴帴涓烘銆傛墍鏈夌殑鍙敤榪炴帴瀵硅薄鍧囩櫥璁板湪鍚嶄負(fù)
  * freeConnections鐨勫悜閲忥紙Vector錛変腑銆傚鏋滃悜閲忎腑鏈夊浜庝竴涓殑榪炴帴錛実etConnection()鎬繪槸閫夊彇絎竴涓傚悓鏃訛紝鐢變簬鏂扮殑鍙敤榪炴帴鎬?br />   * 鏄粠灝鵑儴鍔犲叆鍚戦噺錛屼粠鑰屼嬌寰楁暟鎹簱榪炴帴鐢變簬闀挎椂闂撮棽緗岃鍏抽棴鐨勯闄╁噺浣庡埌鏈灝忕▼搴︺?絎竴涓猤etConnection()鍦ㄨ繑鍥炲彲鐢ㄨ繛鎺ョ粰瀹㈡埛
  * 紼嬪簭涔嬪墠錛岃皟鐢ㄤ簡(jiǎn)isClosed()鏂規(guī)硶楠岃瘉榪炴帴浠嶆棫鏈夋晥銆傚鏋滆榪炴帴琚叧闂垨瑙﹀彂寮傚父錛実etConnection()閫掑綊鍦拌皟鐢ㄨ嚜宸變互灝濊瘯鑾峰彇鍙﹀鐨?br />   * 鍙敤榪炴帴銆傚鏋滃湪鍚戦噺freeConnections涓笉瀛樺湪浠諱綍鍙敤榪炴帴錛実etConnection()鏂規(guī)硶媯(gè)鏌ユ槸鍚﹀凡緇忔寚瀹氭渶澶ц繛鎺ユ暟闄愬埗銆傚宸茬粡鎸囧畾錛?br />   * 鍒欐鏌ュ綋鍓嶈繛鎺ユ暟鏄惁宸茬粡鍒拌揪鏋侀檺銆傛澶刴axConn涓?琛ㄧず娌℃湁闄愬埗銆傚鏋滄病鏈夋寚瀹氭渶澶ц繛鎺ユ暟闄愬埗鎴栧綋鍓嶈繛鎺ユ暟灝忎簬璇ュ鹼紝璇ユ柟娉曞皾璇曞垱寤?br />   * 鏂扮殑榪炴帴銆傚鍒涘緩鎴愬姛錛屽垯澧炲姞宸蹭嬌鐢ㄨ繛鎺ョ殑璁℃暟騫惰繑鍥烇紝鍚﹀垯榪斿洖絀哄箋傚垱寤烘柊榪炴帴鐢眓ewConnection()鏂規(guī)硶瀹炵幇銆?br />   * 鍒涘緩榪囩▼涓庢槸鍚﹀凡緇忔寚瀹氭暟鎹簱甯愬彿銆佸瘑鐮佹湁鍏熾侸DBC鐨凞riverManager綾繪彁渚涘涓猤etConnection()鏂規(guī)硶錛岃繖浜涙柟娉曡鐢ㄥ埌JDBC URL
  * 涓庡叾瀹冧竴浜涘弬鏁幫紝濡傜敤鎴峰笎鍙峰拰瀵嗙爜絳夈侱riverManager灝嗕嬌鐢ㄦ寚瀹氱殑JDBC URL紜畾閫傚悎浜庣洰鏍囨暟鎹簱鐨勯┍鍔ㄧ▼搴忓強(qiáng)寤虹珛榪炴帴銆?br />   * 絎簩涓猤etConnection()鏂規(guī)硶闇瑕佷竴涓互姣涓哄崟浣嶇殑鏃墮棿鍙傛暟錛岃鍙傛暟琛ㄧず瀹㈡埛紼嬪簭鑳藉絳夊緟鐨勬渶闀挎椂闂淬傚緩绔嬭繛鎺ョ殑鍏蜂綋鎿?br />   * 浣滀粛鏃х敱絎竴涓猤etConnection()鏂規(guī)硶瀹炵幇銆傝鏂規(guī)硶鎵ц鏃跺厛灝唖tartTime鍒濆鍖栦負(fù)褰撳墠鏃墮棿銆傚湪while寰幆涓皾璇曡幏寰椾竴涓繛鎺ャ傚鏋滃け
  * 璐ワ紝鍒欎互緇欏畾鐨勬椂闂村間負(fù)鍙傛暟璋冪敤wait()銆倃ait()鐨勮繑鍥炲彲鑳芥槸鐢變簬鍏跺畠綰跨▼璋冪敤notify()鎴杗otifyAll()錛屼篃鍙兘鏄敱浜庨瀹氭椂闂村凡鍒般?br />   * 涓烘壘鍑簑ait()榪斿洖鐨勭湡姝e師鍥狅紝紼嬪簭鐢ㄥ綋鍓嶆椂闂村噺寮濮嬫椂闂達(dá)紙startTime錛夛紝濡傚樊鍊煎ぇ浜庨瀹氭椂闂村垯榪斿洖絀哄鹼紝鍚﹀垯鍐嶆璋冪敤getConnection()銆?br />   * 鎶婄┖闂茬殑榪炴帴鐧昏鍒拌繛鎺ユ睜鐢眆reeConnection()鏂規(guī)硶瀹炵幇錛屽畠鐨勫弬鏁頒負(fù)榪斿洖緇欒繛鎺ユ睜鐨勮繛鎺ュ璞°傝瀵硅薄琚姞鍏ュ埌freeConnections
  * 鍚戦噺鐨勬湯灝撅紝鐒跺悗鍑忓皯宸蹭嬌鐢ㄨ繛鎺ヨ鏁般傝皟鐢╪otifyAll()鏄負(fù)浜?jiǎn)閫氱煡鍏跺畠姝e湪絳夊緟鍙敤榪炴帴鐨勭嚎紼嬨?璁稿Servlet寮曟搸涓哄疄鐜板畨鍏ㄥ叧闂彁渚?br />   * 澶氱鏂規(guī)硶銆傛暟鎹簱榪炴帴姹犻渶瑕佺煡閬撹浜嬩歡浠ヤ繚璇佹墍鏈夎繛鎺ヨ兘澶熸甯稿叧闂侱BConnectionManager綾昏礋鍗忚皟鏁翠釜鍏抽棴榪囩▼錛屼絾鍏抽棴榪炴帴姹犱腑鎵鏈夎繛
  * 鎺ョ殑浠誨姟鍒欑敱DBConnectionPool綾昏礋璐c俽elease()鏂規(guī)硶渚汥BConnectionManager璋冪敤銆傝鏂規(guī)硶閬嶅巻freeConnections鍚戦噺騫跺叧闂墍鏈夎繛鎺ワ紝
  * 鐒跺悗浠庡悜閲忎腑鍒犻櫎榪欎簺榪炴帴銆?br />   */

 /**
  * 姝ゅ唴閮ㄧ被瀹氫箟浜?jiǎn)涓涓繛鎺ユ睜.瀹冭兘澶熸牴鎹姹傚垱寤烘柊榪炴帴,鐩村埌棰勫畾鐨勬渶""""r
  */
 class DBConnectionPool {
  private int checkedOut;

  private Vector freeConnections = new Vector();

  private int maxConn;

  private String name;

  private String password;

  private String URL;

  private String user;

  /**
   * 鍒涘緩鏂扮殑榪炴帴姹?br />    * @param name榪炴帴姹犲悕瀛?br />    * @param URL鏁版嵁搴撶殑JDBC URL
   * @param user鏁版嵁搴撳笎鍙?鎴?null
   * @param password瀵嗙爜,鎴?null
   * @param maxConn姝よ繛鎺ユ睜鍏佽寤虹珛鐨勬渶澶ц繛鎺ユ暟
   */
  public DBConnectionPool(String name, String URL, String user,
    String password, int maxConn) {
   this.name = name;
   this.URL = URL;
   this.user = user;
   this.password = password;
   this.maxConn = maxConn;
  }

  /**
   * 灝嗕笉鍐嶄嬌鐢ㄧ殑榪炴帴榪斿洖緇欒繛鎺ユ睜
   * @param con瀹㈡埛紼嬪簭閲婃斁鐨勮繛鎺?br />    */
  public synchronized void freeConnection(Connection con) {
   // 灝嗘寚瀹氳繛鎺ュ姞鍏ュ埌鍚戦噺鏈熬
   freeConnections.addElement(con);
   checkedOut--;
   notifyAll();
  }

  /**
   * 浠庤繛鎺ユ睜鑾峰緱涓涓彲鐢ㄨ繛鎺?濡傛病鏈夌┖闂茬殑榪炴帴涓斿綋鍓嶈繛鎺ユ暟灝忎簬鏈澶ц繛鎺?鏁伴檺鍒?鍒欏垱寤烘柊榪炴帴.
   * 濡傚師鏉ョ櫥璁頒負(fù)鍙敤鐨勮繛鎺ヤ笉鍐嶆湁鏁?鍒欎粠鍚戦噺鍒犻櫎涔?
   * 鐒跺悗閫掑綊璋冪敤鑷繁浠ュ皾璇曟柊鐨勫彲鐢ㄨ繛鎺?
   */
  public synchronized Connection getConnection() {
   Connection con = null;
   if (freeConnections.size() > 0) {// 鑾峰彇鍚戦噺涓涓涓彲鐢ㄨ繛鎺?br />     con = (Connection) freeConnections.firstElement();
    freeConnections.removeElementAt(0);
    try {
     if (con.isClosed()) {
      log("浠庤繛鎺ユ睜" + name + "鍒犻櫎涓涓棤鏁堣繛鎺?);
      // 閫掑綊璋冪敤鑷繁,灝濊瘯鍐嶆鑾峰彇鍙敤榪炴帴
      con = getConnection();
     }
    } catch (SQLException e) {
     log("浠庤繛鎺ユ睜" + name + "鍒犻櫎涓涓棤鏁堣繛鎺?);
     // 閫掑綊璋冪敤鑷繁,灝濊瘯鍐嶆鑾峰彇鍙敤榪炴帴
     con = getConnection();
    }
   } else if (maxConn == 0 || checkedOut < maxConn) {
    con = newConnection();
   }
   if (con != null) {
    checkedOut++;
   }
   return con;
  }

  /**
   * 浠庤繛鎺ユ睜鑾峰彇鍙敤榪炴帴.鍙互鎸囧畾瀹㈡埛紼嬪簭鑳藉絳夊緟鐨勬渶闀挎椂闂?鍙傝鍓嶄竴涓猤etConnection()鏂規(guī)硶.
   * @param timeout浠ユ縐掕鐨勭瓑寰呮椂闂撮檺鍒?br />    */
  public synchronized Connection getConnection(long timeout) {
   long startTime = new Date().getTime();
   Connection con;
   while ((con = getConnection()) == null) {
    try {
     wait(timeout);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
    if ((new Date().getTime() - startTime) >= timeout) {// wait()榪斿洖鐨勫師鍥犳槸瓚呮椂
     return null;
    }
   }
   return con;
  }

  /**
   * 鍏抽棴鎵鏈夎繛鎺?br />    */
  public synchronized void release() {
   Enumeration allConnections = freeConnections.elements();
   while (allConnections.hasMoreElements()) {
    Connection con = (Connection) allConnections.nextElement();
    try {
     con.close();
     log("鍏抽棴榪炴帴姹? + name + "涓殑涓涓繛鎺?);
    } catch (SQLException e) {
     log(e, "鏃犳硶鍏抽棴榪炴帴姹? + name + "涓殑榪炴帴");
     e.printStackTrace();
    }
   }
   freeConnections.removeAllElements();
  }

  /**
   * 鍒涘緩鏂扮殑榪炴帴
   */
  private Connection newConnection() {
   Connection con = null;
   try {
    if (user==null||"".equals(user)) {
     con = DriverManager.getConnection(URL);
    } else {
     con = DriverManager.getConnection(URL, user, password);
    }
    log("榪炴帴姹? + name + "鍒涘緩涓涓柊鐨勮繛鎺?);
   } catch (SQLException e) {
    log(e, "鏃犳硶鍒涘緩涓嬪垪URL鐨勮繛鎺? " + URL);
    return null;
   }
   return con;
  }
 }

 /////////////////////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////////////////////////////////
}

鍦╯qlserver2000錛宼omcat5.5楠岃瘉閫氳繃

鏂規(guī)硶璋冪敤錛?/p>

package com.xinnuo.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConn {
 private DBConnectionManager connMgr = null;
 private Connection conn = null;
 private Statement stat = null;
 private ResultSet rs = null;
 public DBConn() {
  connMgr = DBConnectionManager.getInstance();
 }
 public ResultSet executeQuery(String strSQL) throws SQLException {
  this.conn = connMgr.getConnection("access");
  this.stat = this.conn.createStatement();
  this.rs = this.stat.executeQuery(strSQL);
  return this.rs;
 }
 public void execute(String strSQL) throws SQLException {
  this.conn = connMgr.getConnection("access");
  this.stat = this.conn.createStatement();
  this.stat.execute(strSQL);
 }
 public void executeUpdate(String strSQL) throws SQLException {
  this.conn = connMgr.getConnection("access");
  this.stat = this.conn.createStatement();
  this.stat.executeUpdate(strSQL);
 }
 public Connection getConnection(){
  this.conn = connMgr.getConnection("access");
  return this.conn;
 }
 public void free(){
  try {
   if (this.rs != null) {
    this.rs.close();
   }
   if (this.stat != null) {
    this.stat.close();
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
  connMgr.freeConnection("access", this.conn);
 }
}

seal 2009-03-13 16:58 鍙戣〃璇勮
]]>
SQL Server / MySQL 琛ㄥ悕闀垮害闄愬埗http://www.tkk7.com/sealyu/archive/2009/03/04/257793.htmlsealsealWed, 04 Mar 2009 07:12:00 GMThttp://www.tkk7.com/sealyu/archive/2009/03/04/257793.htmlhttp://www.tkk7.com/sealyu/comments/257793.htmlhttp://www.tkk7.com/sealyu/archive/2009/03/04/257793.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/257793.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/257793.html MySQL鏁版嵁搴撳悕鍜岃〃鍚嶉暱搴﹂檺鍒朵負(fù)錛?4瀛楃銆?br />


seal 2009-03-04 15:12 鍙戣〃璇勮
]]>
Ubuntu涓婼Quirrel榪炴帴Microsoft SQL Server錛堝緢濂界殑瀹㈡埛绔級(jí)http://www.tkk7.com/sealyu/archive/2009/01/14/251217.htmlsealsealWed, 14 Jan 2009 02:02:00 GMThttp://www.tkk7.com/sealyu/archive/2009/01/14/251217.htmlhttp://www.tkk7.com/sealyu/comments/251217.htmlhttp://www.tkk7.com/sealyu/archive/2009/01/14/251217.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/251217.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/251217.html 欏轟究鎶婂畨瑁呴厤緗甋Quirrel鐨勮繃紼嬩腑閬囧埌鍒扮殑闂璁板綍涓涓嬶紝浠ュ厤蹇樿錛?br /> 1銆傛垜瀹夎鐨勭増鏈槸SQuirrel2.6.8錛?鍦ㄥ畨瑁呭畬鎴愬悗鍚姩錛屾樉紺哄嚭鏉ョ殑鍙湁涓涓┖紿楀彛錛岄櫎浜?jiǎn)鏍囬鏍忔病鋼Q浣曚笢瑗匡紝Google浜?jiǎn)涓涓嬶紝鏈変漢璇存槸jdk鐗堟湰鐨勯棶棰橈紝鐢變簬欏圭洰闇瑕侊紝蹇呴』浣跨敤jdk5錛岀湅鏉ュ彧鏈夊畨瑁呬袱涓猨dk浜?jiǎn)銆?鍙互榪欎箞浣滐紝鍘熸湁jdk鍙?qiáng)鐜鍙橀噺淇濇寔涓嶅彉锛屽啀瀹夎涓涓猨dk6錛岀劧鍚庢墜鍔ㄥ垱寤轟竴涓猯auncher錛屽惎鍔ㄥ弬鏁拌緗負(fù)錛?usr/lib/jvm/java-6-sun/bin/java -jar /home/sealyu/tools/SQuirreL/squirrel-sql.jar 銆?br /> 2銆備笉鐭ラ亾鏄惁鐢變簬鐗堟湰鐨勫師鍥狅紝娌℃壘鍒頒嬌鐢ㄦ枃妗o紝SQuirrel瀹樻柟緗戠珯涔熸病鎵懼埌銆傚悗鏉ヨ嚜宸辨懜绱簡(jiǎn)涓涓嬶紝鎯寵榪炴帴鍒癕icrosoft SQL Server錛堣繖閲屾垜鐢ㄧ殑鐗堟湰鏄?005錛夛紝涓嬭澆浜?jiǎn)鏈鏂扮殑jtds椹卞姩錛屾垜鐨勬槸jtds1.2.2.jar銆傚皢涔嬫斁鍒癝Quirrel_home/lib涓嬮潰錛岃繖鏍峰氨鑳借嚜鍔ㄦ嫻嬪埌椹卞姩錛屾墦寮SQuirrel鍚庯紝鍦╦TDS Microsoft SQL 鍙抽敭錛岄夋嫨Modify Driver, 鍦?Extra Class Path 涓夊嚭浣犵殑jtds鏋跺寘錛孡ist Drivers錛岃繖鏍蜂綘灝辮兘鍦ㄤ笅闈㈢殑涓嬫媺妗嗕腑鐪嬪埌椹卞姩鐨勪富綾匯傜偣鍑葷‘瀹氭寜閽紝榪欐椂鍊欏乏杈規(guī)爲(wèi)褰㈣彍鍗曢噷闈㈠搴旂殑鏉$洰鍓嶆柟灝辯敱綰㈣壊鐨勫皬鍙夊彉?shù)负缁胯壊鐨勫鍙蜂簡(jiǎn)銆?br /> 3銆傛柊寤轟竴涓狝lias錛岃緭鍏ヤ綘瑕佽繛鎺ユ暟鎹簱鐨勪俊鎭紝connect鍗沖彲浣跨敤銆?br />



seal 2009-01-14 10:02 鍙戣〃璇勮
]]>
SQL2005寮鍙戠増瀹夎涓滄ц兘鐩戣鍣ㄨ鏁板櫒瑕佹眰錛堥敊璇級(jí)鈥濆拰鈥渃om+鐩綍闂璀﹀憡澶勭悊鈥濋棶棰樼殑瑙e喅(杞?http://www.tkk7.com/sealyu/archive/2008/12/12/245888.htmlsealsealFri, 12 Dec 2008 03:29:00 GMThttp://www.tkk7.com/sealyu/archive/2008/12/12/245888.htmlhttp://www.tkk7.com/sealyu/comments/245888.htmlhttp://www.tkk7.com/sealyu/archive/2008/12/12/245888.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/245888.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/245888.html


鍦?“寮濮?#8221; -->  “榪愯”涓緭鍏?regedit錛屽紑鍚敞鍐岃〃緙栬緫鍣ㄣ?br />
瀹氫綅鍒?br /> [HKEY_LOCAL_MACHINE"SOFTWARE"Microsoft"Windows NT"CurrentVersion"Perflib
澶勶紝鍦ㄥ彸杈圭殑鏍?wèi)迮炵洰褰曚笅鍙互鐪嬪埌Perflib鐩綍涓嬫湁004鍜?09涓や釜瀛愮洰褰曘?br />
鍦⊿ql Server 2005 鐨勫畨瑁呭府鍔╂枃浠朵腑璇寸殑鏄渶瑕佹煡鐪?09鐩綍鐨勬敞鍐岃〃欏癸紝鑰屾垜浠ぇ閮ㄥ垎浜轟嬌鐢ㄧ殑鏄畝浣撲腑鏂囩殑鎿嶄綔緋葷粺錛屾墍浠ヤ笉鑳芥寜甯姪涓鐨勶紝鑰屾槸闇瑕佹敞鎰?04鐩綍涓殑鍐呭銆?br />
鎵撳紑004 鐩綍涓殑鍐呭錛屽彲浠ョ湅鍒板涓嬪浘錛?br />


鎴戜滑鍒嗗埆鍙屽嚮 “Counter” 欏?鍜?“Help” 欏癸紝瀵熺湅鍏朵腑鐨勬渶鍚庣殑鏁板瓧錛屽涓嬪浘錛?br />
Counter 欏圭殑鍐呭錛?br />



Helper 欏瑰唴瀹?br />


榪欐椂鍊欙紝鎴戜滑鐭ラ亾錛孋ounter 欏圭殑鏁板瓧鏄?556錛孒elper欏圭殑鍐呭鏄?557銆?br />
鐒跺悗錛屾搷浣滄敞鍐岃〃緙栬緫鍣ㄧ殑宸﹁竟鐨勭洰褰曟爲(wèi)錛屽畾浣嶅埌Perflib鐩綍涓嬶紝騫舵敞鎰忔鏌ュ彸杈圭獥鍙g殑 “Laster Counter”欏瑰拰“Laster Help”欏圭殑鍊鹼紝騫舵妸Laster Counter鐨勫兼敼鎴愬垰鎵嶈褰曚笅鐨凜ounter鍊?556錛屾妸Laster Help 鐨勫兼敼鎴愬垰鎵嶈涓嬬殑 Help鐨勫?557銆?br />
瑕佹敞鎰忕殑鏄紝淇敼鏁板瓧鐨勬椂鍊欙紝杈撳叆鐨勬椂鍊欏繀欏婚夊垯鍩烘暟鏄?“鍗佽繘鍒?#8221;錛屽惁鍒欐暟瀛楀皢涓嶅尮閰嶏紝Sql Server 2005 媯(gè)鏌ュ皢鍐嶆澶辮觸銆?br />
鐒跺悗鍏抽棴娉ㄥ唽琛ㄧ紪杈戝櫒錛屽紑濮嬪畨瑁?Sql Server 2005 錛岀粷瀵瑰彲浠ュ畨瑁呮垚鍔熴?

seal 2008-12-12 11:29 鍙戣〃璇勮
]]>
RedHat AS4 涓嬪畨瑁匓ugzilla鍚庝笉鑳借繛鎺ySQL闂鐨勮В鍐蟲(chóng)柟娉?/title><link>http://www.tkk7.com/sealyu/archive/2008/07/20/216263.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Sun, 20 Jul 2008 15:44:00 GMT</pubDate><guid>http://www.tkk7.com/sealyu/archive/2008/07/20/216263.html</guid><wfw:comment>http://www.tkk7.com/sealyu/comments/216263.html</wfw:comment><comments>http://www.tkk7.com/sealyu/archive/2008/07/20/216263.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/sealyu/comments/commentRss/216263.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/sealyu/services/trackbacks/216263.html</trackback:ping><description><![CDATA[鍦≧edHat AS4涓婂畨瑁匓ugzilla鏃訛紝<br /> 鍦ㄥ畨瑁呭畬瀵瑰簲鐨刾erl妯″潡鍚庯紝鍦╨ocalconfig鏂囦歡涓厤緗甿ysql鐨勫搴斾俊鎭紝<br /> 緇х畫(huà)榪愯checksetup.pl,鍑虹幇濡備笅閿欒錛?br /> Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)<br /> <br /> 浣嗘槸Mysql宸茬粡瀹夎騫朵笖鑳芥甯歌繍琛岋紝鍚庢潵鍙戠幇Bugzilla 榛樿媯(gè)鏌ysql鏃舵槸浣跨敤/var/lib/mysql/mysql.sock榪涜榪炴帴錛?br /> 鑰屾垜浣跨敤婧愮爜緙栬瘧瀹夎鐨凪ysql鏄嬌鐢?tmp/mysql.sock 榪涜榪炴帴錛屾墍浠ugzilla媯(gè)鏌ysql璁劇疆鏃朵細(xì)鍑虹幇榪炴帴閿欒銆?br /> 瑙e喅鏂規(guī)硶寰堢畝鍗曪細(xì)<br /> ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock<br /> 榪欐牱姣忔Bugzilla榪炴帴鏃跺氨浼?xì)鋴社?tmp/mysql.sock榪涜榪炴帴錛屽畨瑁呮垚鍔熴?br /> <br /> <img src ="http://www.tkk7.com/sealyu/aggbug/216263.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/sealyu/" target="_blank">seal</a> 2008-07-20 23:44 <a href="http://www.tkk7.com/sealyu/archive/2008/07/20/216263.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鍦≧edhat AS4涓婃簮鐮佺紪璇戝畨瑁匨ySQLhttp://www.tkk7.com/sealyu/archive/2008/07/20/216256.htmlsealsealSun, 20 Jul 2008 14:32:00 GMThttp://www.tkk7.com/sealyu/archive/2008/07/20/216256.htmlhttp://www.tkk7.com/sealyu/comments/216256.htmlhttp://www.tkk7.com/sealyu/archive/2008/07/20/216256.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/216256.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/216256.htmlby Seal 錛圚aibo Yu錛?008-7-20


1>灝唌ysql瀹夎鏂囦歡(鏈漢浣跨敤mysql5.0.51a-linux-i686-glibc23.tar.gz)瑙e帇鍒板搴旂洰褰曪紝鍦ㄦ涓?/home/seal/mysql

2>榪涘叆婧愮爜鐩綍緙栬瘧瀹夎

CODE:

#cd /home/seal/mysql
#./configure --prefix=/usr/local/mysql --with-charset=gbk  |娉?閰嶇疆Mysql瀹夎璺緞騫朵笖鏀寔涓枃
#make  |娉?緙栬瘧
#make install  |娉?緙栬瘧瀹夎

3>鏇挎崲/etc/my.cnf鏂囦歡,榪涘叆婧愮爜鍖?鎵ц鍛戒護(hù)

CODE:

#cd /home/seal/mysql
#cp support-files/my-medium.cnf /etc/my.cnf

4>寤虹珛MySQL浣跨敤鑰呭拰緹ょ粍:

CODE:

#groupadd mysql
#useradd -g mysql mysql

5>瀹屾垚浠ヤ笂鎿嶄綔浠ュ悗榪涜鍒濆鍖栨暟鎹簱,榪涘叆宸茬粡瀹夎濂界殑mysql鐩綍

CODE:

#cd /usr/local/mysql
#bin/mysql_install_db --user=mysql  |娉?--user=mysql 鍒濆鍖栬〃騫朵笖瑙勫畾鐢╩ysql鐢ㄦ埛
6>璁劇疆緇檓ysql鍜宺oot鐢ㄦ埛璁懼畾璁塊棶鏉冮檺 鎴戜滑鍏堣繘鍏ysql鐩綍

CODE:

#cd /usr/local/mysql
#chown -R root /usr/local/mysql   |娉?璁懼畾root鑳借闂?usr/local/mysq
#chown -R mysql /usr/local/mysql/var   |娉?璁懼畾mysql鐢ㄦ埛鑳借闂?usr/local/mysql/var
#chgrp -R mysql /usr/local/mysql    |娉?璁懼畾mysql緇勮兘澶熻闂?usr/local/mysq
7>鍚姩mysql,榪涘叆宸茬粡瀹夎濂界殑鐩綍

CODE:

#cd /usr/local/mysql
#bin/mysqld_safe --user=mysql &

8>
淇敼mysql鏁版嵁搴撹秴綰х敤鎴穜oot鐨勭己鐪佸瘑鐮?
/usr/local/mysql/bin/mysqladmin -u root password 'mysql'

鍏抽棴mysql鏈嶅姟鍣?
cd /usr/local/mysql/bin
./mysqladmin -u root -p    shutdown 

9>璁懼畾寮鏈哄氨鍚姩mysql,榪涘叆婧愮爜鐩綍涓?/p>

# cd /home/seal/mysql
# cp support-files/mysql.server /etc/init.d/mysql

# chmod +x /etc/init.d/mysql
# chkconfig --level 345 mysql on
# service mysql restart
Shutting down MySQL.                                       [  紜畾  ]
Starting MySQL                                                [  紜畾  ]
[root@localhost mysql]#
10>璁劇疆Mysql榪滅▼璁塊棶錛?br /> 鍦ㄥ惎鍔╩ysql鍚庯細(xì)
mysql>GRANT ALL PRIVILEGES ON *.* TO ‘myuser’@'%’ IDENTIFIED BY ‘mypassword’ WI
TH GRANT OPTION;
mysql>Flush Privileges;

瀹夎瀹屾瘯銆?br />


seal 2008-07-20 22:32 鍙戣〃璇勮
]]>
mysql寮鍚繙紼嬭繛鎺ョ殑鏂規(guī)硶http://www.tkk7.com/sealyu/archive/2008/07/15/215088.htmlsealsealTue, 15 Jul 2008 14:36:00 GMThttp://www.tkk7.com/sealyu/archive/2008/07/15/215088.htmlhttp://www.tkk7.com/sealyu/comments/215088.htmlhttp://www.tkk7.com/sealyu/archive/2008/07/15/215088.html#Feedback3http://www.tkk7.com/sealyu/comments/commentRss/215088.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/215088.html瑙e喅MySQL涓嶅厑璁鎬粠榪滅▼璁塊棶鐨勬柟娉?/span>

瑙e喅鏂規(guī)硶錛?/span>
1
銆?/span>鏀硅〃娉曘傚彲鑳芥槸浣犵殑甯愬彿涓嶅厑璁鎬粠榪滅▼鐧婚檰錛屽彧鑳藉湪localhost銆傝繖涓椂鍊欏彧瑕佸湪localhost鐨勯偅鍙扮數(shù)鑴戯紝鐧誨叆mysql鍚庯紝鏇存敼 “mysql” 鏁版嵁搴撻噷鐨?/span> “user” 琛ㄩ噷鐨?/span> “host” 欏癸紝浠?/span>“localhost”鏀圭О“%”

mysql -u root -pvmwaremysql>use mysql;
mysql>update user set host = ‘%’ where user = ‘root’;
mysql>select host, user from user;

2. 鎺堟潈娉曘備緥濡傦紝浣犳兂myuser浣跨敤mypassword浠庝換浣曚富鏈鴻繛鎺ュ埌mysql鏈嶅姟鍣ㄧ殑璇濄?/span>

GRANT ALL PRIVILEGES ON *.* TO ‘myuser’@'%’ IDENTIFIED BY ‘mypassword’ WI
TH GRANT OPTION;

濡傛灉浣犳兂鍏佽鐢ㄦ埛myuser浠?/span>ip涓?/span>192.168.1.6鐨勪富鏈鴻繛鎺ュ埌mysql鏈嶅姟鍣紝騫朵嬌鐢?/span>mypassword浣滀負(fù)瀵嗙爜

GRANT ALL PRIVILEGES ON *.* TO ‘myuser’@'192.168.1.3′ IDENTIFIED BY
‘mypassword’ WITH GRANT OPTION;

鎴戠敤鐨勭涓涓柟娉?/span>,鍒氬紑濮嬪彂鐜頒笉琛?/span>,鍦ㄧ綉涓婃煡浜?jiǎn)涓涓?/span>,灝戞墽琛屼竴涓鍙?/span> mysql>FLUSH   PRIVILEGES
浣夸慨鏀圭敓鏁?/span>.灝卞彲浠ヤ簡(jiǎn)

seal 2008-07-15 22:36 鍙戣〃璇勮
]]>
SQL Server鏃ユ湡澶勭悊datetime鍜宒ate涔嬮棿鐨勭浉浜掕漿鎹?/title><link>http://www.tkk7.com/sealyu/archive/2008/07/03/212308.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Thu, 03 Jul 2008 05:38:00 GMT</pubDate><guid>http://www.tkk7.com/sealyu/archive/2008/07/03/212308.html</guid><wfw:comment>http://www.tkk7.com/sealyu/comments/212308.html</wfw:comment><comments>http://www.tkk7.com/sealyu/archive/2008/07/03/212308.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/sealyu/comments/commentRss/212308.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/sealyu/services/trackbacks/212308.html</trackback:ping><description><![CDATA[<p><font size="1"><span>鏃ユ湡鏄暟鎹鐞嗕腑緇忓父浣跨敤鍒扮殑淇℃伅涔嬩竴銆傜敓鏃ャ佹暟鎹鐞嗘椂闂淬佽鍒掔殑棰勮瀹屾垚鏃墮棿錛屾寜騫淬佸銆佹湀鐨勭粺 璁★紝榪欎簺閮藉睘浜庢棩鏈熷鐞嗙殑鑼冪暣銆傜敱浜庢棩鏈熶腑鍖呭惈浜?jiǎn)骞淬佸銆佹湀銆佹棩絳変紬澶氫俊鎭紝涓嶅悓鐨勫浗瀹跺鏃ユ湡鏍煎紡銆佹棩鏈熸枃瀛楁弿榪板強(qiáng)鏄熸湡鏈変笉鍚岀殑瑙勫畾錛屽洜姝や駭鐢熶簡(jiǎn)鏃ユ湡澶勭悊 鐨勫鏉傛с傛湰绔犱富瑕佽璁哄湪</span>SQL Server鏁版嵁搴撲腑瀵規(guī)棩鏈熺殑鍚勭澶勭悊鏂規(guī)硶銆?/font></p> <h2 align="center"><font face="瀹?huà)浣? MS Song" size="1">鏃ユ湡綾誨瀷姒傝堪</font></h2> <p><font size="1">SQL Server涓殑鏃ユ湡綾誨瀷鍖呮嫭datetime鍜宻malldatetime錛屼粎鑳藉鐞嗗彲浠ヨ瘑鍒負(fù)1753騫達(dá)綖9999騫撮棿鐨勬棩鏈熺殑鍊鹼紝娌℃湁鍗曠嫭鐨勬棩鏈熷瀷鎴栨椂闂村瀷銆?/font></p> <h4><font face="瀹?huà)浣? MS Song"><font size="1">1錛巇atetime</font></font></h4> <p><font size="1">datetime綾誨瀷澶勭悊浠?753騫?鏈?鏃ワ綖9999騫?2鏈?1鏃ョ殑鏃ユ湡鍜屾椂闂存暟鎹紝綺劇‘搴︿負(fù)鐧懼垎涔嬩笁縐掋傚嵆錛氬浜?.000锝?.001銆?.009鐨勬棩鏈熷鹼紝璋冩暣涓?.000錛涘浜?.002锝?.004鐨勬棩鏈熷鹼紝璋冩暣涓?.003錛涘浜?.005锝?.008鐨勬棩鏈熷鹼紝璋冩暣涓?.007銆?/font></p> <p><font size="1">渚嬪錛屼笅闈㈢殑浠g爜鍦ㄨ緭鍏ユ椂錛屽叾鏃墮棿綺劇‘搴︿負(fù)鐧懼垎涔嬩竴縐掞紝浣嗙粡鏁版嵁搴撲繚瀛樺悗鍐嶆樉紺哄嚭鏉ワ紝鍏剁粨鏋滃氨宸茬粡鍋氫簡(jiǎn)澶勭悊銆?/font></p> <p><font face="Courier New" size="1">DECLARE @t TABLE(date char(21))</font></p> <p><font face="Courier New" size="1">INSERT @t SELECT '1900-1-1 00:00:00.000'</font></p> <p><font face="Courier New" size="1">...</font></p> <p><font face="Courier New" size="1">INSERT @t SELECT '1900-1-1 00:00:00.009'</font></p> <p><font size="1"><font face="Courier New">SELECT date,</font>杞崲鍚庣殑鏃ユ湡<font face="Courier New">=CAST(date as datetime) FROM @t</font></font></p> <p><font size="1"><font face="Courier New">/*--</font>緇撴灉</font></p> <p><font size="1"><font face="Courier New">date                     </font>杞崲鍚庣殑鏃ユ湡</font></p> <p><font face="Courier New" size="1">---------------------------------- ----------------------------</font></p> <p><font face="Courier New" size="1">1900-1-1 00:00:00.000    1900-01-01 00:00:00.000<br /> ...<br /> <font face="Courier New">1900-1-1 00:00:00.000    1900-01-01 00:00:00.010</font><br /> </font></p> <font size="1"><font face="Courier New">--*/</font> </font> <p><font size="1">datetime鐨勫瓨鍌ㄩ暱搴︿負(fù)8瀛楄妭錛屾棩鏈熷拰鏃墮棿鍚勭敤4涓瓧鑺傚瓨鍌紝絎竴涓?瀛楄妭瀛樺偍鑷?900騫?鏈?鏃ヤ箣鍓嶆垨涔嬪悗鐨勫ぉ鏁幫紙浠?900騫?鏈?鏃ヤ負(fù)鍒嗙晫鐐癸紝鍦?900騫?鏈?鏃ヤ箣鍓嶇殑鏃ユ湡鐨勫ぉ鏁板皬浜?錛屽湪1900騫?鏈?鏃ヤ箣鍚庣殑鏃ユ湡鐨勫ぉ鏁板ぇ浜?錛夈傚彟澶栦竴涓?瀛楄妭瀛樺偍浠ュ崍澶滐紙00:00:00.000錛夊悗姣鏁版墍浠h〃鐨勬瘡澶╃殑鏃墮棿銆?/font></p> <p><font size="1">渚嬪錛屼笅闈㈢殑浠g爜婕旂ず浜?jiǎn)datetime鍙橀噺涓紝浠呭寘鍚崟綰殑鏃ユ湡鍜屽崟綰殑鏃墮棿鏃訛紝鏃ユ湡瀛樺偍鐨勫崄鍏繘鍒跺瓨鍌ㄨ〃紺虹粨鏋溿?/font></p> <p align="left"><font face="Courier New" size="1">DECLARE @dt datetime</font></p> <p align="left"><font size="1"><font face="Courier New">--</font>鍗曠函鐨勬棩鏈?/font></p> <p align="left"><font face="Courier New" size="1">SET @dt='1900-1-2'</font></p> <p align="left"><font face="Courier New" size="1">SELECT CAST(@dt as binary(8))</font></p> <p align="left"><font size="1"><font face="Courier New">--</font>緇撴灉<font face="Courier New">: 0x0000000100000000</font></font></p> <p align="left"><font size="1"><font face="Courier New">--</font>鍗曠函鐨勬椂闂?/font></p> <p align="left"><font face="Courier New" size="1">SET @dt='00:00:01'</font></p> <p align="left"><font face="Courier New" size="1">SELECT CAST(@dt as binary(8))</font></p> <p align="left"><font size="1"><font face="Courier New">--</font>緇撴灉<font face="Courier New">: 0x000000000000012C</font></font></p> <h4><font face="瀹?huà)浣? MS Song"><font size="1">2錛巗malldatetime</font></font></h4> <p><font size="1">smalldatetime綾誨瀷澶勭悊浠?900騫?鏈?鏃ワ綖2079騫?鏈? 鏃ョ殑鏃ユ湡鍜屾椂闂存暟鎹紝綺劇‘鍒板垎閽熴?9.998縐掓垨鏇翠綆鐨剆malldatetime鍊煎悜涓嬭垗鍏ヤ負(fù)鏈鎺ヨ繎鐨勫垎閽燂紝29.999縐掓垨鏇撮珮鐨剆malldatetime鍊煎悜涓婅垗鍏ヤ負(fù)鏈鎺ヨ繎鐨勫垎閽熴?/font></p> <p><font size="1">smalldatetime鐨勫瓨鍌ㄩ暱搴︿負(fù)4瀛楄妭錛岀涓涓?瀛楄妭瀛樺偍鑷?900騫?鏈?鏃ヤ箣鍚庣殑澶╂暟銆傚彟澶栦竴涓?瀛楄妭瀛樺偍鍗堝錛?0:00:00.000錛夊悗鐨勫垎閽熸暟銆?/font></p> <p><font size="1">渚嬪錛屼笅闈㈢殑浠g爜婕旂ず浜?jiǎn)smalldatetime鍙橀噺涓紝浠呭寘鍚崟綰殑鏃ユ湡鍜屽崟綰殑鏃墮棿鏃訛紝鏃ユ湡瀛樺偍鐨勫崄鍏繘鍒跺瓨鍌ㄨ〃紺虹粨鏋溿?/font></p> <p><font face="Courier New" size="1">DECLARE @dt smalldatetime</font></p> <p><font size="1"><font face="Courier New">--</font>鍗曠函鐨勬棩鏈?/font></p> <p><font face="Courier New" size="1">SET @dt='1900-1-2'</font></p> <p><font face="Courier New" size="1">SELECT CAST(@dt as binary(4))</font></p> <p><font size="1"><font face="Courier New">--</font>緇撴灉<font face="Courier New">: 0x00010000</font></font></p> <p><font size="1"><font face="Courier New">--</font>鍗曠函鐨勬椂闂?/font></p> <p><font face="Courier New" size="1">SET @dt='00:10'</font></p> <p><font size="1"><font face="Courier New">SELECT CAST(@dt as binary(4))<br /> </font><font face="Courier New">--</font>緇撴灉<font face="Courier New">: 0x0000000A</font></font></p> <h2><font face="瀹?huà)浣? MS Song" size="1">鏃ユ湡澶勭悊鍑芥暟</font></h2> <p><font size="1">鏃ユ湡鐢卞勾銆佹湀銆佹棩銆佹椂絳夊涓儴鍒嗙粍鎴愶紝瀹冪殑澶勭悊鐩稿澶嶆潅錛屽洜姝わ紝SQL Server鎻愪緵浜?jiǎn)澶ч噺鐨勬棩鏈熷鐞嗗嚱鏁板Q岀敤浠ュ畬鎴愬悇縐嶆棩鏈熸暟鎹殑澶勭悊銆傛帉鎻″ソ榪欎簺鍑芥暟錛屽瀹屾垚鏁版嵁搴撶殑鍚勭鏃ユ湡澶勭悊闈炲父蹇呰錛屾湰鑺傚皢浠嬬粛鍑犱釜甯哥敤鐨勬棩鏈熷鐞嗗嚱鏁般傛湡澧炲噺鍑芥暟鍙互瀵規(guī)棩鏈熸寚瀹氶儴鍒嗙殑鍊艱繘琛屽鍑忥紝騫惰繑鍥炲鐞嗗悗鐨勬棩鏈熷鹼紝SQL Server鎻愪緵鐨勬棩鏈熷鍑忓嚱鏁頒負(fù)DATEADD銆?/font></p> <p><br /> <font size="1">DATEADD</font><font size="1">鐨勫叿浣撹娉曞涓嬶細(xì)<font face="Courier New">DATEADD ( datepart , number, date )</font></font></p> <p><font size="1">鍏朵腑鍖呮嫭浠ヤ笅鍙傛暟銆?/font></p> <p><font size="1">¡ datepart錛氭槸瑙勫畾搴斿悜鏃ユ湡鐨勫摢涓閮ㄥ垎榪斿洖鏂板肩殑鍙傛暟銆傝〃2-1鍒楀嚭浜?jiǎn)SQL Server鏀寔鐨勬棩鏈熼儴鍒嗐佺緝鍐欏強(qiáng)鍚箟銆?/font></p> <p><font size="1"><font face="瀹?huà)浣? MS Song">                    DATEADD</font>銆?font face="瀹?huà)浣? MS Song">DATEDIFF</font>鏀寔鐨勬棩鏈熼儴鍒嗐佺緝鍐欏強(qiáng)鍚箟</font></p> <table border="1" cellpadding="0" cellspacing="0" width="561"> <tbody> <tr> <td valign="top" width="187"> <p><font size="1">鏃ユ湡閮ㄥ垎</font></p> </td> <td valign="top" width="187"> <p><font size="1">緙?font face="瀹?huà)浣? MS Song">    </font>鍐?/font></p> </td> <td valign="top" width="187"> <p><font size="1">鍚?font face="瀹?huà)浣? MS Song">    </font>涔?/font></p> </td> </tr> <tr> <td valign="top" width="187"> <p><font size="1">Year</font></p> </td> <td valign="top" width="187"> <p><font size="1">yy , yyyy</font></p> </td> <td valign="top" width="187"> <p><font size="1">騫翠喚</font></p> </td> </tr> <tr> <td valign="top" width="187"> <p><font size="1">Quarter</font></p> </td> <td valign="top" width="187"> <p><font size="1">qq , q</font></p> </td> <td valign="top" width="187"> <p><font size="1">瀛e害</font></p> </td> </tr> <tr> <td valign="top" width="187"> <p><font size="1">Month</font></p> </td> <td valign="top" width="187"> <p><font size="1">mm , m</font></p> </td> <td valign="top" width="187"> <p><font size="1">鏈堜喚</font></p> </td> </tr> <tr> <td valign="top" width="187"> <p><font size="1">Dayofyear</font></p> </td> <td valign="top" width="187"> <p><font size="1">dy,y</font></p> </td> <td rowspan="2" valign="top" width="187"> <p><font size="1">鏃?/font></p> </td> </tr> <tr> <td valign="top" width="187"> <p><font size="1">Day</font></p> </td> <td valign="top" width="187"> <p><font size="1">dd , d</font></p> </td> </tr> <tr> <td valign="top" width="187"> <p><font size="1">Week</font></p> </td> <td valign="top" width="187"> <p><font size="1">wk , ww</font></p> </td> <td valign="top" width="187"> <p><font size="1">鏄熸湡</font></p> </td> </tr> <tr> <td valign="top" width="187"> <p><font size="1">Hour</font></p> </td> <td valign="top" width="187"> <p><font size="1">Hh</font></p> </td> <td valign="top" width="187"> <p><font size="1">灝忔椂</font></p> </td> </tr> <tr> <td valign="top" width="187"> <p><font size="1">Minute</font></p> </td> <td valign="top" width="187"> <p><font size="1">mi , n</font></p> </td> <td valign="top" width="187"> <p><font size="1">鍒嗛挓</font></p> </td> </tr> <tr> <td valign="top" width="187"> <p><font size="1">Second</font></p> </td> <td valign="top" width="187"> <p><font size="1">ss , s</font></p> </td> <td valign="top" width="187"> <p><font size="1">縐?/font></p> </td> </tr> <tr> <td valign="top" width="187"> <p><font size="1">Millisecond</font></p> </td> <td valign="top" width="187"> <p><font size="1">Ms</font></p> </td> <td valign="top" width="187"> <p><font size="1">姣</font></p> </td> </tr> </tbody> </table> <p><font size="1">¡ number錛氭槸鐢ㄦ潵澧炲姞datepart鐨勫箋傛鏁拌〃紺哄鍔狅紝璐熸暟琛ㄧず鍑忓皯錛屽鏋滄寚瀹氱殑鏄潪鏁存暟鍊鹼紝鍒欏拷鐣ユ鍊肩殑灝忔暟閮ㄥ垎錛屼笉鍋氬洓鑸嶄簲鍏ュ鐞嗐備緥濡傦紝DATEADD錛圖ay,1.7,date錛夛紝琛ㄧずdate澧炲姞1澶┿?/font></p> <p><font size="1">¡ date錛氭槸榪斿洖datetime鎴杝malldatetime鍊兼垨鏃ユ湡鏍煎紡瀛楃涓茬殑琛ㄨ揪寮忋?/font></p> <p><font size="1">濡傛灉date鏄痵malldatetime錛屽垯榪斿洖smalldatetime錛屽惁鍒欒繑鍥瀌atetime銆俤ate涓簊malldatetime錛孌atepart涓篠econd錛坰s,s錛夋垨Millisecond錛坢s錛夋椂錛岃繑鍥炲煎皢鏍規(guī)嵁鏃ユ湡澧炲噺鐨勭粨鏋滆皟鏁村埌鍒嗛挓錛沝ate涓篸atetime錛孌atepart涓篗illisecond錛坢s錛夋椂錛岃繑鍥炲煎皢鏍規(guī)嵁鏃ユ湡澧炲噺鐨勭粨鏋滆皟鏁翠負(fù)鐧懼垎涔嬩笁縐掋傝皟鏁磋鍒欏彲浠ュ弬鑰?.1鑺傜殑鐩稿叧璇存槑銆?/font></p> <p><font size="1">date鍏佽鐩存帴涓巒umber榪涜澧炲噺璁$畻錛屽嵆瀵逛簬DATEADD錛圖ay,number,date錛夛紝絳夊悓浜巇ate+number銆?/font></p> <h3><font face="瀹?huà)浣? MS Song"><font size="1"> 鏃ユ湡淇℃伅鑾峰彇鍑芥暟</font></font></h3> <p><font size="1">鏃ユ湡淇℃伅鑾峰彇鍑芥暟鐢ㄤ簬鑾峰彇鏃ユ湡鎸囧畾閮ㄥ垎鐨勭浉鍏充俊鎭紝甯哥敤鐨勬棩鏈熶俊鎭幏鍙栧嚱鏁板琛?-2鎵紺恒?/font></p> <p><font size="1"><font face="瀹?huà)浣? MS Song">                                      </font>甯哥敤鐨勬棩鏈熶俊鎭幏鍙栧嚱鏁?/font></p> <table border="1" cellpadding="0" cellspacing="0" width="561"> <tbody> <tr> <td valign="top" width="140"> <p><font size="1">鍔熻兘璇存槑</font></p> </td> <td valign="top" width="168"> <p><font size="1">璇?font face="瀹?huà)浣? MS Song">    </font>娉?/font></p> </td> <td valign="top" width="253"> <p><font size="1">鍙傛暟鍙?qiáng)杩斿洖鍊兼暟鎹被鍨嬭鏄?/font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">榪斿洖浠h〃鎸囧畾鏃ユ湡鐨勬寚瀹氭棩鏈熼儴鍒嗙殑瀛楃涓?/font></p> </td> <td valign="top" width="168"> <p><font size="1">DATENAME(datepart,date)</font></p> </td> <td rowspan="2" width="253"> <p><font size="1">datepart鏄寚瀹氬簲榪斿洖鐨勬棩鏈熼儴鍒嗙殑鍙傛暟錛屽叾瀹氫箟濡傝〃2-3鎵紺恒俤ate鏄繑鍥瀌atetime鎴杝malldatetime鍊兼垨鏃ユ湡鏍煎紡瀛楃涓茬殑琛ㄨ揪寮忋侱ATENAME鍑芥暟榪斿洖nvarchar錛孌ATEPART鍑芥暟榪斿洖int</font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">榪斿洖浠h〃鎸囧畾鏃ユ湡鐨勬寚瀹氭棩鏈熼儴鍒嗙殑鏁存暟</font></p> </td> <td valign="top" width="168"> <p><font size="1">DATEPART(datepart,date)</font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">榪斿洖琛ㄧず鎸囧畾鏃ユ湡涓殑騫翠喚鐨勬暣鏁?/font></p> </td> <td valign="top" width="168"> <p><font size="1">YEAR(date)</font></p> </td> <td valign="top" width="253"> <p><font size="1">榪斿洖int</font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">榪斿洖琛ㄧず鎸囧畾鏃ユ湡涓殑鏈堜喚鐨勬暣鏁?/font></p> </td> <td valign="top" width="168"> <p><font size="1">MONTH(date)</font></p> </td> <td valign="top" width="253"> <p><font size="1">榪斿洖int</font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">榪斿洖琛ㄧず鎸囧畾鏃ユ湡涓殑澶╃殑鏁存暟</font></p> </td> <td valign="top" width="168"> <p><font size="1">DAY(date)</font></p> </td> <td valign="top" width="253"> <p><font size="1">榪斿洖int</font></p> </td> </tr> </tbody> </table> <p><font size="1"><font face="瀹?huà)浣? MS Song">                  DATENAME</font>銆?font face="瀹?huà)浣? MS Song">DATEPART</font>鏀寔鐨勬棩鏈熼儴鍒嗐佺緝鍐欏強(qiáng)鍚箟</font></p> <table border="1" cellpadding="0" cellspacing="0" width="561"> <tbody> <tr> <td valign="top" width="140"> <p><font size="1">鏃ユ湡閮ㄥ垎</font></p> </td> <td valign="top" width="168"> <p><font size="1">緙?font face="瀹?huà)浣? MS Song">    </font>鍐?/font></p> </td> <td valign="top" width="253"> <p><font size="1">鍚?font face="瀹?huà)浣? MS Song">    </font>涔?/font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">Year</font></p> </td> <td valign="top" width="168"> <p><font size="1">yy , yyyy</font></p> </td> <td valign="top" width="253"> <p><font size="1">騫翠喚</font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">Quarter</font></p> </td> <td valign="top" width="168"> <p><font size="1">qq , q</font></p> </td> <td valign="top" width="253"> <p><font size="1">瀛e害</font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">Month</font></p> </td> <td valign="top" width="168"> <p><font size="1">mm , m</font></p> </td> <td valign="top" width="253"> <p><font size="1">鏈堜喚</font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">Dayofyear</font></p> </td> <td valign="top" width="168"> <p><font size="1">dy , y</font></p> </td> <td rowspan="2" valign="top" width="253"> <p><font size="1">鏃?/font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">Day</font></p> </td> <td valign="top" width="168"> <p><font size="1">dd , d</font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">Week</font></p> </td> <td valign="top" width="168"> <p><font size="1">wk , ww</font></p> </td> <td valign="top" width="253"> <p><font size="1">鑷勾鍒濆紑濮嬬殑絎嚑涓槦鏈?/font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">Weekday</font></p> </td> <td valign="top" width="168"> <p><font size="1">Dw</font></p> </td> <td valign="top" width="253"> <p><font size="1">鏄熸湡鍑狅紙渚嬪鏄熸湡涓銆佹槦鏈熶簩錛?/font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">Hour</font></p> </td> <td valign="top" width="168"> <p><font size="1">Hh</font></p> </td> <td valign="top" width="253"> <p><font size="1">灝忔椂</font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">Minute</font></p> </td> <td valign="top" width="168"> <p><font size="1">mi , n</font></p> </td> <td valign="top" width="253"> <p><font size="1">鍒嗛挓</font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">Second</font></p> </td> <td valign="top" width="168"> <p><font size="1">ss , s</font></p> </td> <td valign="top" width="253"> <p><font size="1">縐掋俤ate涓簊malldatetime鏃訛紝濮嬬粓榪斿洖0</font></p> </td> </tr> <tr> <td valign="top" width="140"> <p><font size="1">Millisecond</font></p> </td> <td valign="top" width="168"> <p><font size="1">Ms</font></p> </td> <td valign="top" width="253"> <p><font size="1">姣銆俤ate涓簊malldatetime鏃訛紝濮嬬粓榪斿洖0錛屼負(fù)datetime鏃訛紝榪斿洖鐧句喚涔嬩笁縐?/font></p> </td> </tr> </tbody> </table> <p><font size="1">DATEPART錛圵eek,date錛夎繑鍥炵殑鏄熸湡璁$畻鏂瑰紡錛屾槸鎸夌収鏄熸湡鏃ヤ負(fù)涓鍛ㄧ殑絎竴澶╋紝榪欑偣涓庝腑鍥戒漢鐨勬棩鏈熷鐞嗕範(fàn)鎯笉鍚岋紝鍦ㄤ嬌鐢ㄦ椂瑕佹敞鎰忚繖涓鐐廣侱ATENAME鍑芥暟榪斿洖鎸囧畾鏃ユ湡鐨勬寚瀹氭棩鏈熼儴鍒嗙殑瀛楃涓詫紝鍏惰繑鍥炵殑鍏蜂綋瀛楃涓插鹼紝涓嶴ET DATEFIRST鍙?qiáng)SET DATELANGUAGE閫夐」鐨勮緗湁鍏熾備嬌鐢―ATEPART錛圵eekday,date錛夋椂錛屽叾榪斿洖鐨勫間笌SET DATEFIRST閫夐」鐨勮緗湁鍏籌紝鍏蜂綋鐨勫皢鍦?.3鑺備腑璇存槑銆?/font></p> <h3><font face="瀹?huà)浣? MS Song"><font size="1"> 鏃ユ湡宸艱綆楀嚱鏁?/font></font></h3> <p><font size="1">鏃ユ湡宸艱綆楀嚱鏁扮敤浜庤綆椾袱涓粰瀹氭棩鏈熸寚瀹氶儴鍒嗙殑杈圭晫鏁幫紝SQL Server鎻愪緵鐨勬棩鏈熷樊鍊艱綆楀嚱鏁頒負(fù)DATEDIFF銆?/font></p> <p><font size="1">DATEDIFF鐨勫叿浣撹娉曞涓嬶細(xì)</font></p> <p><font face="Courier New" size="1">DATEDIFF ( datepart , startdate , enddate ) </font></p> <p><font size="1">鍏朵腑鍖呮嫭浠ヤ笅鍙傛暟銆?/font></p> <p><font size="1">¡ datepart錛氳瀹氫簡(jiǎn)搴斿湪鏃ユ湡鐨勫摢涓閮ㄥ垎璁$畻宸錛屽叾瀹氫箟濡傝〃2-1鎵紺恒?/font></p> <p><font size="1">¡ startdate錛氳瀹氫簡(jiǎn)璁$畻鐨勫紑濮嬫棩鏈熴?/font></p> <p><font size="1">¡ enddate錛氳瀹氫簡(jiǎn)璁$畻鐨勭粓姝㈡棩鏈熴?/font></p> <p><font size="1">榪斿洖綾誨瀷錛歩nteger</font></p> <p><font size="1">璁$畻鐨勫紑濮嬫棩鏈熷拰緇堟鏃ユ湡錛屽彲浠ユ槸鏃ユ湡鎴栨棩鏈熸牸寮忕殑瀛楃涓層傝綆楃殑鏂規(guī)硶鏄粠enddate鍑忓幓startdate銆傚鏋渟tartdate姣攅nddate鏅氾紝榪斿洖璐熷箋傚綋緇撴灉瓚呭嚭鏁存暟鍊艱寖鍥達(dá)紝DATEDIFF灝變駭鐢熼敊璇傚浜庢縐掞紝鏈澶ф暟鏄?4澶?0灝忔椂31鍒嗛挓23.647縐掋傚浜庣錛屾渶澶ф暟鏄?8騫淬?/font></p> <p><font size="1">璁$畻璺ㄥ垎閽熴佺鍜屾縐掕繖浜涜竟鐣岀殑鏂規(guī)硶錛屼嬌寰桪ATEDIFF緇欏嚭鐨勭粨鏋滃湪鍏ㄩ儴鏁版嵁綾誨瀷涓槸涓鑷寸殑銆傜粨鏋滄槸甯︽璐熷彿鐨勬暣鏁板鹼紝鍏剁瓑浜庤法絎竴涓拰絎簩涓棩鏈熼棿鐨刣atepart杈圭晫鏁般備緥濡傦紝鍦?005騫?鏈?鏃ュ拰2005騫?鏈?1鏃ヤ箣闂寸殑鏈堜喚鏁版槸1銆?/font></p> <h3><font face="瀹?huà)浣? MS Song"><font size="1"> 鍏朵粬鏃ユ湡澶勭悊鐩稿叧鍑芥暟</font></font></h3> <p><font size="1">鍏朵粬甯哥敤鐨勬棩鏈熷鐞嗙浉鍏沖嚱鏁板寘鎷互涓嬪嚑涓?/font></p> <h4><font face="瀹?huà)浣? MS Song"><font size="1">1錛嶨ETDATE</font></font></h4> <p><font size="1">GETDATE鎸夌収datetime鍊艱繑鍥炲綋鍓嶇郴緇熸棩鏈熷拰鏃墮棿銆?/font></p> <p><font size="1">GETDATE鐨勮娉曞涓嬶細(xì)</font></p> <p><font face="Courier New" size="1">GETDATE()</font></p> <p><font size="1">榪斿洖綾誨瀷錛歞atetime</font></p> <h4><font face="瀹?huà)浣? MS Song"><font size="1">2錛嶪SDATE</font></font></h4> <p><font size="1">ISDATE紜畾杈撳叆鐨勮〃杈懼紡鏄惁鏈夋晥鏃ユ湡銆?/font></p> <p><font size="1">鍦ㄨ緭鍏ユ棩鏈熻〃杈懼紡鏃訛紝鏃ユ湡閮芥槸浠ユ棩鏈熸牸寮忕殑瀛楃涓叉彁渚涚殑錛岀敱浜庝笉鍚岀殑鍖哄煙鏈変笉鍚岀殑鏃ユ湡鏍煎紡錛屾墍浠ュ茍涓嶈兘淇濊瘉杈撳叆鐨勬棩鏈熻〃杈懼紡鑳藉琚玈QL Server璇嗗埆錛岃繖縐嶆儏鍐典笅錛屽氨闇瑕佺敤ISDATE鏉ュ垽鏂棩鏈熻〃杈懼紡鑳藉惁姝g‘鍦拌SQL Server璇嗗埆浜?jiǎn)銆?/font></p> <p><font size="1">ISDATE鐨勮娉曞涓嬶細(xì)</font></p> <p><font face="Courier New" size="1">ISDATE(expression)</font></p> <p><font size="1">榪斿洖綾誨瀷錛歩nt</font></p> <h4><font face="瀹?huà)浣? MS Song"><font size="1">3錛嶤ONVERT</font></font></h4> <p><font size="1">CONVERT灝嗘煇縐嶆暟鎹被鍨嬬殑琛ㄨ揪寮忔樉寮忚漿鎹負(fù)鍙︿竴縐嶆暟鎹被鍨嬨?/font></p> <p><font size="1">涓ユ牸鏉ヨ錛孋ONVERT涓嶅睘浜庢棩鏈熷鐞嗗嚱鏁幫紝鍙槸瀹冭緇忓父鐢ㄤ簬鏃ユ湡澶勭悊涓紝鎵浠ヨ繖閲屾妸瀹冨垪鍏ヤ簡(jiǎn)鍏朵粬鏃ユ湡澶勭悊鍑芥暟錛屼笅闈㈡槸CONVERT鐨勭敤娉曟弿榪幫紙鍙噸鐐硅鏄庡湪鏃ユ湡澶勭悊涓殑搴旂敤錛夈?/font></p> <p><font size="1">CONVERT鐨勫叿浣撹娉曞涓嬶細(xì)</font></p> <p><font face="Courier New" size="1">CONVERT ( data_type [ ( length ) ] , expression [ , style ] )</font></p> <p><font size="1">鍏朵腑鍖呮嫭浠ヤ笅鍙傛暟銆?/font></p> <p><font size="1">¡ expression錛氭槸瑕佽漿鎹㈡暟鎹被鍨嬬殑鏈夋晥SQL Server琛ㄨ揪寮忋?/font></p> <p><font size="1">¡ data_type錛氭槸expression杞崲鍚庣殑鏁版嵁綾誨瀷錛宭ength鏄浜庢湁綺懼害瀹氫箟闇瑕佺殑data_type鐨勭簿搴﹀畾涔夛紝瀵逛簬娌℃湁綺懼害瀹氫箟闇瑕佺殑data_type錛岃鍙傛暟鍙互鐪佺暐銆?/font></p> <p><font size="1">¡ style錛氬畾涔夋暟鎹被鍨嬭漿鎹㈡椂鐨勬牸寮忥紝瀵逛簬鏃ユ湡綾誨瀷鐨勮漿鎹紝瀹冪殑瀹氫箟濡傝〃2-4鎵紺恒?/font></p> <p><font size="1">琛?font face="瀹?huà)浣? MS Song">2-4                                      style</font>鍦ㄦ棩鏈熻漿鎹腑鐨勮鏄?/font></p> <table border="1" cellpadding="0" cellspacing="0" width="561"> <tbody> <tr> <td valign="top" width="105"> <p><font size="1">涓嶅甫涓栫邯鏁頒綅</font></p> </td> <td valign="top" width="84"> <p><font size="1">甯︿笘綰暟浣?/font></p> </td> <td valign="top" width="141"> <p><font size="1">鏍?font face="瀹?huà)浣? MS Song">    </font>鍑?/font></p> </td> <td valign="top" width="230"> <p><font size="1">杈撳叆<font face="瀹?huà)浣? MS Song">/</font>杈撳嚭</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">鈥?/font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">0鎴?00</font></p> </td> <td valign="top" width="141"> <p><font size="1">榛樿鍊?/font></p> </td> <td valign="top" width="230"> <p><font size="1">mon dd yyyy hh:miAM(鎴?PM)</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">1</font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">101</font></p> </td> <td valign="top" width="141"> <p><font size="1">緹庡浗</font></p> </td> <td valign="top" width="230"> <p><font size="1">mm/dd/yyyy</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">2</font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">102</font></p> </td> <td valign="top" width="141"> <p><font size="1">ANSI</font></p> </td> <td valign="top" width="230"> <p><font size="1">yy.mm.dd</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">3</font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">103</font></p> </td> <td valign="top" width="141"> <p><font size="1">鑻卞浗/娉曞浗</font></p> </td> <td valign="top" width="230"> <p><font size="1">dd/mm/yy</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">4</font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">104</font></p> </td> <td valign="top" width="141"> <p><font size="1">寰峰浗</font></p> </td> <td valign="top" width="230"> <p><font size="1">dd.mm.yy</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">5</font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">105</font></p> </td> <td valign="top" width="141"> <p><font size="1">鎰忓ぇ鍒?/font></p> </td> <td valign="top" width="230"> <p><font size="1">dd-mm-yy</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">6</font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">106</font></p> </td> <td valign="top" width="141"> <p><font size="1">鈥?/font></p> </td> <td valign="top" width="230"> <p><font size="1">dd mon yy</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">7</font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">107</font></p> </td> <td valign="top" width="141"> <p><font size="1">鈥?/font></p> </td> <td valign="top" width="230"> <p><font size="1">mon dd, yy</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">8</font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">108</font></p> </td> <td valign="top" width="141"> <p><font size="1">鈥?/font></p> </td> <td valign="top" width="230"> <p><font size="1">hh:mm:ss</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">鈥?/font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">9鎴?09</font></p> </td> <td valign="top" width="141"> <p><font size="1">榛樿鍊?姣</font></p> </td> <td valign="top" width="230"> <p><font size="1">mon dd yyyy hh:mi:ss:mmmAM(鎴朠M)</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">10</font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">110</font></p> </td> <td valign="top" width="141"> <p><font size="1">緹庡浗</font></p> </td> <td valign="top" width="230"> <p><font size="1">mm-dd-yy</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">11</font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">111</font></p> </td> <td valign="top" width="141"> <p><font size="1">鏃ユ湰</font></p> </td> <td valign="top" width="230"> <p><font size="1">yy/mm/dd</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">12</font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">112</font></p> </td> <td valign="top" width="141"> <p><font size="1">ISO</font></p> </td> <td valign="top" width="230"> <p><font size="1">yymmdd</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">鈥?/font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">13鎴?13</font></p> </td> <td valign="top" width="141"> <p><font size="1">嬈ф床榛樿鍊?姣</font></p> </td> <td valign="top" width="230"> <p><font size="1">dd mon yyyy hh:mm:ss:mmm(24h)</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">14</font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">114</font></p> </td> <td valign="top" width="141"> <p><font size="1">鈥?/font></p> </td> <td valign="top" width="230"> <p><font size="1">hh:mi:ss:mmm(24h)</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">鈥?/font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">20鎴?20</font></p> </td> <td valign="top" width="141"> <p><font size="1">ODBC瑙勮寖</font></p> </td> <td valign="top" width="230"> <p><font size="1">yyyy-mm-dd hh:mm:ss[.fff]</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">鈥?/font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">21鎴?21</font></p> </td> <td valign="top" width="141"> <p><font size="1">ODBC瑙勮寖錛堝甫姣錛?/font></p> </td> <td valign="top" width="230"> <p><font size="1">yyyy-mm-dd hh:mm:ss[.fff]</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">鈥?/font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">126</font></p> </td> <td valign="top" width="141"> <p><font size="1">ISO8601</font></p> </td> <td valign="top" width="230"> <p><font size="1">yyyy-mm-ddThh:mm:ss.mmm</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">鈥?/font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">130</font></p> </td> <td valign="top" width="141"> <p><font size="1">Hijri</font></p> </td> <td valign="top" width="230"> <p><font size="1">dd mon yyyy hh:mi:ss:mmmAM</font></p> </td> </tr> <tr> <td valign="top" width="105"> <p align="center"><font size="1">鈥?/font></p> </td> <td valign="top" width="84"> <p align="center"><font size="1">131</font></p> </td> <td valign="top" width="141"> <p><font size="1">Hijri</font></p> </td> <td valign="top" width="230"> <p><font size="1">dd/mm/yy hh:mi:ss:mmmAM</font></p> </td> </tr> </tbody> </table> <div> <p><font size="1">璇存槑<font face="瀹?huà)浣? MS Song">錛?/font></font></p> <p><font face="瀹?huà)浣? MS Song" size="1">鈶?nbsp;杈撳叆/杈撳嚭錛?#8220;杈撳叆”琛ㄧず浠庡瓧絎︿覆杞崲涓烘棩鏈熸椂瀛楃涓茬殑鏃ユ湡鏍煎紡錛?#8220;杈撳嚭”鎸囦粠鏃ユ湡杞崲涓哄瓧絎︿覆鏃剁殑鏃ユ湡瀛楃涓叉牸寮忋?/font></p> <p><font face="瀹?huà)浣? MS Song" size="1">鈶?nbsp;Hijri錛氭槸鍏鋒湁鍑犵鍙樺寲褰㈠紡鐨勬棩鍘嗙郴緇燂紝SQL Server浣跨敤鍏朵腑鐨勭濞佺壒綆楁硶銆?/font></p> </div> <p><font size="1">褰撲粠smalldatetime杞崲涓哄瓧絎︽暟鎹椂錛岀敱浜巗malldatetimer鍙繚瀛樺埌鍒嗛挓鐨勬暟鎹紝鍥犳錛屽浜庡寘鍚鎴栨縐掔殑鏍峰紡錛屽皢鍦ㄧ鎴栨縐掔殑浣嶇疆涓婃樉紺洪浂銆傚綋浠巇atetime鎴杝malldatetime鍊艱繘琛岃漿鎹㈡椂錛屽彲浠ラ氳繃浣跨敤閫傚綋鐨刢har鎴杤archar鏁版嵁綾誨瀷闀垮害鏉ユ埅鏂笉闇瑕佺殑鏃ユ湡閮ㄥ垎銆?/font></p> <div> <p><font size="1">娉ㄦ剰<font face="瀹?huà)浣? MS Song">錛?/font></font></p> <p><font face="瀹?huà)浣? MS Song" size="1">鍦⊿QL Server涓紝鐢變簬鐩存帴鎻愪緵鐨勬棩鏈熷潎鏄互鏃ユ湡鏍煎紡鐨勫瓧絎︿覆鎻愪緵錛屾墍浠ュ湪浣跨敤CONVERT榪涜鏃ユ湡鏍煎紡杞崲鏃訛紝瑕佸厛鎶婃棩鏈熸牸寮忕殑瀛楃涓茶漿鎹負(fù)鏃ユ湡鍨嬶紝鐒跺悗鎵嶈兘鍒╃敤CONVERT榪涜鏃ユ湡鏍煎紡杞崲錛屽惁鍒欏氨鍙樻垚瀛楃涓茶漿鎹負(fù)瀛楃涓詫紝姝ゆ椂鐨剆tyle閫夐」鏄棤鏁堢殑銆?/font></p> </div> <p><font size="1">榪斿洖綾誨瀷錛氱敱鍙傛暟data_type紜畾銆?/font></p> <p><font size="1">涓嬮潰鏄埄鐢–ONVERT榪涜鏃ユ湡杞崲鐨勭畝鍗曠ず渚嬶細(xì)</font></p> <p><font size="1"><font face="Courier New">/*== </font>瀛楃杞崲涓烘棩鏈熸椂<font face="Courier New">,Style</font>鐨勪嬌鐢?font face="Courier New"> ==*/</font></font></p> <p><font size="1"><font face="Courier New">--1. Style=101</font>鏃?font face="Courier New">,</font>琛ㄧず鏃ユ湡瀛楃涓蹭負(fù)<font face="Courier New">:mm/dd/yyyy</font>鏍煎紡</font></p> <p><font face="Courier New" size="1">SELECT CONVERT(datetime,'11/1/2003',101)</font></p> <p><font size="1"><font face="Courier New">--</font>緇撴灉<font face="Courier New">:2003-11-01 00:00:00.000</font></font></p> <p><font size="1"><font face="Courier New">--2. Style=101</font>鏃?font face="Courier New">,</font>琛ㄧず鏃ユ湡瀛楃涓蹭負(fù)<font face="Courier New">:dd/mm/yyyy</font>鏍煎紡</font></p> <p><font face="Courier New" size="1">SELECT CONVERT(datetime,'11/1/2003',103)</font></p> <p><font size="1"><font face="Courier New">--</font>緇撴灉<font face="Courier New">:2003-01-11 00:00:00.000</font><font face="Courier New"> </font></font></p> <p><font size="1"><font face="Courier New">/*== </font>鏃ユ湡杞崲涓哄瓧絎︿覆<font face="Courier New"> ==*/</font></font></p> <p><font face="Courier New" size="1">DECLARE @dt datetime</font></p> <p><font face="Courier New" size="1">SET @dt='2003-1-11'</font></p> <p><font size="1"><font face="Courier New">--1. Style=101</font>鏃?font face="Courier New">,</font>琛ㄧず灝嗘棩鏈熻漿鎹負(fù)<font face="Courier New">:mm/dd/yyyy </font>鏍煎紡</font></p> <p><font face="Courier New" size="1">SELECT CONVERT(varchar,@dt,101)</font></p> <p><font size="1"><font face="Courier New">--</font>緇撴灉<font face="Courier New">:01/11/2003</font></font></p> <p><font size="1"><font face="Courier New">--2. Style=103</font>鏃?font face="Courier New">,</font>琛ㄧず灝嗘棩鏈熻漿鎹負(fù)<font face="Courier New">:dd/mm/yyyy </font>鏍煎紡</font></p> <p><font face="Courier New" size="1">SELECT CONVERT(varchar,@dt,103)</font></p> <p><font size="1"><font face="Courier New">--</font>緇撴灉<font face="Courier New">:11/01/2003</font></font></p> <p><font size="1"><font face="Courier New"> </font><font face="Courier New">/*== </font>榪欐槸寰堝浜虹粡甯哥姱鐨勯敊璇?font face="Courier New">,</font>瀵歸潪鏃ユ湡鍨嬭漿鎹嬌鐢ㄦ棩鏈熺殑<font face="Courier New">style</font>鏍峰紡<font face="Courier New"> ==*/</font></font></p> <p><font face="Courier New" size="1">SELECT CONVERT(varchar,'2003-1-11',101)</font></p> <font size="1"><font face="Courier New">--</font>緇撴灉</font><font face="Courier New" size="1">:2003-1-11</font> <img src ="http://www.tkk7.com/sealyu/aggbug/212308.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/sealyu/" target="_blank">seal</a> 2008-07-03 13:38 <a href="http://www.tkk7.com/sealyu/archive/2008/07/03/212308.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>SQL Server 2005 error: Connection Refused:connecthttp://www.tkk7.com/sealyu/archive/2008/07/01/212000.htmlsealsealTue, 01 Jul 2008 13:07:00 GMThttp://www.tkk7.com/sealyu/archive/2008/07/01/212000.htmlhttp://www.tkk7.com/sealyu/comments/212000.htmlhttp://www.tkk7.com/sealyu/archive/2008/07/01/212000.html#Feedback4http://www.tkk7.com/sealyu/comments/commentRss/212000.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/212000.html Connection refused: connect
緇忚繃鍑犲皬鏃剁殑鎸?zhuān)鎵庡Q屽湪閰嶇疆綆$悊鍣ㄩ噷闈㈠彂鐜頒簡(jiǎn)闂錛屽師鏉ユ槸TCPIP鏈嶅姟娌℃湁榛樿鎵撳紑錛岃В鍐沖涓嬶細(xì)
1銆佹墦寮SQL Server Configuration Manager -> Protocols for SQLEXPRESS -> TCP/IP
2銆佸彸閿崟鍑誨惎鍔═CP/IP
3銆佸弻鍑昏繘鍏ュ睘鎬э紝鎶奍P鍦板潃涓殑IP all涓殑TCP绔彛璁劇疆涓?433
4銆侀噸鏂板惎鍔⊿QL Server 2005鏈嶅姟




seal 2008-07-01 21:07 鍙戣〃璇勮
]]>
sql server 涓垹闄ら粯璁ょ害鏉熺殑閫氱敤sql鑴氭湰http://www.tkk7.com/sealyu/archive/2008/04/17/193755.htmlsealsealThu, 17 Apr 2008 06:31:00 GMThttp://www.tkk7.com/sealyu/archive/2008/04/17/193755.htmlhttp://www.tkk7.com/sealyu/comments/193755.htmlhttp://www.tkk7.com/sealyu/archive/2008/04/17/193755.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/193755.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/193755.html 鍦⊿QL Server 涓紝濡傛灉緇欒〃鐨勪竴涓瓧孌佃緗簡(jiǎn)榛樿鍊鹼紝灝變細(xì)鍦ㄧ郴緇熻〃sysobjects涓敓鎴愪竴涓粯璁ょ害鏉熴?br /> 濡傛灉鎯沖垹闄よ繖涓緗簡(jiǎn)榛樿鍊肩殑瀛楁錛堝亣璁炬瀛楁鍚峜olumn1錛夛紝
鎵ц“ALTER TABLE table1 DROP COLUMN column1”鏃跺氨浼?xì)鎶ラ敊锛?xì)
The object 'DF__xxxxxxxxxxx' is dependent on column 'column1'.
ALTER TABLE DROP COLUMN column1failed because one or more objects access this column.

鎵浠ュ湪鍒犻櫎姝ゅ瓧孌墊椂闇瑕佸厛灝嗙郴緇熻〃涓殑瀵瑰簲榛樿綰︽潫鍒犻櫎錛?鍙互浣跨敤涓嬮潰鐨勮剼鏈繘琛屽垹闄わ細(xì)
-- this script drops the default constraint which is generated by the setting of default value.
DECLARE @tablename VARCHAR(100), @columnname VARCHAR(100), @tab VARCHAR(100)
SET @tablename='CountryGroupEmailAndWaitAux'
SET @columnname='actionOfHasNoValidEmail'

declare @defname varchar(100)
declare @cmd varchar(100)

select @defname = name
FROM sysobjects so
JOIN sysconstraints sc
ON so.id = sc.constid
WHERE object_name(so.parent_obj) = @tablename
AND so.xtype = 'D'
AND sc.colid =
(SELECT colid FROM syscolumns
WHERE id = object_id(@tablename) AND
name = @columnname)

select @cmd='alter table '+ @tablename+ ' drop constraint '+ @defname
if @cmd is null print 'No default constraint to drop'
exec (@cmd)

鍦ㄥ垹闄ゅ搴旂殑榛樿綰︽潫鍚庯紝鎵ц錛?br /> ALTER TABLE table1 DROP COLUMN column1
鍗沖彲鍒犻櫎瀛楁銆?br />

seal 2008-04-17 14:31 鍙戣〃璇勮
]]>
浣跨敤涓鏉QL璇彞鍒犻櫎琛ㄤ腑閲嶅璁板綍錛堣漿杞斤級(jí)http://www.tkk7.com/sealyu/archive/2008/04/10/192010.htmlsealsealThu, 10 Apr 2008 14:55:00 GMThttp://www.tkk7.com/sealyu/archive/2008/04/10/192010.htmlhttp://www.tkk7.com/sealyu/comments/192010.htmlhttp://www.tkk7.com/sealyu/archive/2008/04/10/192010.html#Feedback0http://www.tkk7.com/sealyu/comments/commentRss/192010.htmlhttp://www.tkk7.com/sealyu/services/trackbacks/192010.html

鏁版嵁搴撶粨鏋勭殑鑴氭湰:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TempA]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[TempA]
GO

CREATE TABLE [dbo].[TempA] (
 [id] [int] IDENTITY (1, 1) NOT NULL ,
 [PositionName] [varchar] (256) COLLATE Chinese_PRC_CI_AS NULL ,
 [EnglishPositionName] [varchar] (256) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[TempA] ADD
 CONSTRAINT [PK_TempA] PRIMARY KEY  CLUSTERED
 (
  [id]
 )  ON [PRIMARY]
GO

TempA琛ㄤ腑鏈変笁涓瓧孌?id鍞竴涓斾負(fù)涓婚敭,鑷姩澧為暱; PositionName,EnglishPositionName涓湁閲嶅鐨勮褰?姣斿:
id      PositionName        EnglishPositionName
20     鍏朵粬                           Others
21     璐ㄩ噺宸ョ▼甯?nbsp;              QC Engineer
22     鍏朵粬                           Others
.......
100  璐ㄩ噺宸ョ▼甯?nbsp;              QC Engineer
闇瑕佸墧闄ら噸澶嶇殑"鍏朵粬","璐ㄩ噺宸ョ▼甯?絳夎褰曘?/p>

閲囩敤鐨凷QL璇彞錛?br /> Delete from TempA where id not in (
        select max(t1.id) from TempA t1 group by
         t1.PositionName,t1.EnglishPositionName)

璇存槑錛?br /> (1)闇瑕佸墧闄ら偅鍑犱釜鐢ㄤ簬鍒ゆ柇閲嶅鐨勫瓧孌碉紝鍒欏皢瀹冧滑鏀懼湪group by璇彞涔嬪悗銆?br /> (2)max(t1.id) 涔熷彲浠ユ敼鎴愶細(xì)min(t1.id)



seal 2008-04-10 22:55 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 亚洲精品成a人在线观看☆| 亚洲国产另类久久久精品黑人 | 国产在线ts人妖免费视频| 国产福利免费观看| 噜噜噜亚洲色成人网站∨| 亚洲中文无码亚洲人成影院| 在线观看免费视频一区| 国产极品美女高潮抽搐免费网站| 亚洲综合色一区二区三区小说| 国产精品偷伦视频免费观看了| 午夜免费福利网站| 久久久久国产成人精品亚洲午夜| 亚洲视频在线观看| 有码人妻在线免费看片| 国产在线观看免费视频软件| 无人在线直播免费观看| 久久亚洲精品视频| 一级毛片免费不卡| 国产免费人成在线视频| 国产精品亚洲精品青青青| 羞羞视频在线免费观看| 日本在线免费观看| 国产亚洲精品无码专区| 疯狂做受xxxx高潮视频免费| 成人免费无码大片a毛片| 亚洲图片中文字幕| 色爽黄1000部免费软件下载| 我要看免费的毛片| 亚洲另类精品xxxx人妖| 一个人看的www免费高清| 四虎在线播放免费永久视频 | 亚洲乱码精品久久久久..| 美女视频黄频a免费观看| 免费涩涩在线视频网| 国产精品亚洲专区在线观看| 免费观看激色视频网站bd| 亚洲国产精品无码久久久久久曰| 99人中文字幕亚洲区| 人人揉揉香蕉大免费不卡| 亚洲人成图片小说网站| 中文字幕乱码系列免费|