锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
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");
}
}
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.
varchar[(n)]
闀垮害涓? n 涓瓧鑺傜殑鍙彉闀垮害涓旈潪 Unicode 鐨勫瓧絎︽暟鎹俷
蹇呴』鏄竴涓粙浜? 1 鍜? 8,000 涔嬮棿鐨勬暟鍊箋傚瓨鍌ㄥぇ灝忎負(fù)杈撳叆鏁版嵁鐨勫瓧鑺傜殑瀹為檯闀垮害錛岃屼笉鏄? n
涓瓧鑺傘傛墍杈撳叆鐨勬暟鎹瓧絎﹂暱搴﹀彲浠ヤ負(fù)闆躲倂archar 鍦? SQL-92 涓殑鍚屼箟璇嶄負(fù) char varying 鎴?
character varying銆?
nvarchar(n)
鍖呭惈 n
涓瓧絎︾殑鍙彉闀垮害 Unicode 瀛楃鏁版嵁銆俷 鐨勫煎繀欏諱粙浜? 1 涓? 4,000
涔嬮棿銆傚瓧鑺傜殑瀛樺偍澶у皬鏄墍杈撳叆瀛楃涓暟鐨勪袱鍊嶃傛墍杈撳叆鐨勬暟鎹瓧絎﹂暱搴﹀彲浠ヤ負(fù)闆躲俷varchar 鍦? SQL-92 涓殑鍚屼箟璇嶄負(fù)
national char varying 鍜? national character varying銆?nbsp;
閫氫織涓榛炲氨鏄痸archar閬╁悎杓稿叆鑻辨枃鍜屾暩瀛楋紝nvarchar涓鑸敤鍋氫腑鏂囨垨鍏跺畠瑾炶█鐨勮幾鍏ワ紝閫欐ǎ鍒板垾鐨勮獮緋諱笉鏈冨嚭鐝句簜紕?))
--濡傛灉鏄復(fù)鏃惰〃鍙互鐢?璇存槑,濡傛灉鐢ㄦ煡鎵懼疄琛ㄦ柟娉曟潵鎵撲復(fù)鏃惰〃浼?xì)鎵句笉鍒?鍙戝竷鍖哄埆瀵逛唬.)
if object_id('tempdb..##temp') is not null
drop table ##temp
--鍒ゆ柇瀛樺偍榪囩▼鏄惁瀛樺湪
if exists(select 1 from sysobjects where id=object_id('鎵鏈夎?瀛樺偍榪囩▼鍚?) and
xtype='P')
print '瀛樺湪'
else
print '涓嶅瓨鍦?
--鍒ゆ柇瑙嗗浘鏄惁瀛樺湪
--SQL Server 2000
IF EXISTS (SELECT * FROM sysviews WHERE object_id = '[dbo].[瑙嗗浘鍚峕'
--SQL Server 2005
IF EXISTS (SELECT * FROM sys.views WHERE object_id = '[dbo].[瑙嗗浘鍚峕'
/*
sysObjects (
Name sysname, --object 鍚嶇О
id int, --object id
xtype char(2), -- object 綾誨瀷
type char(2), -- Object 綾誨瀷錛堜笌xtype 浼間箮涓妯′竴鏍鳳紵 鏈夌偣閮侀椃…錛?
uid smallint, -- object 鎵鏈夎呯殑ID
... --鍏朵粬鐨勫瓧孌典笉甯哥敤鍒般?nbsp;
)
sysobjects鐨剎type 浠h〃鐨勫璞$被鍨嬨傚彲浠ユ槸涓嬪垪瀵硅薄綾誨瀷涓殑涓縐嶏細(xì)
C = CHECK 綰︽潫
D = 榛樿鍊兼垨 DEFAULT 綰︽潫
F = FOREIGN KEY 綰︽潫
L = 鏃ュ織
FN = 鏍囬噺鍑芥暟
IF = 鍐呭祵琛ㄥ嚱鏁?
P = 瀛樺偍榪囩▼
PK = PRIMARY KEY 綰︽潫錛堢被鍨嬫槸 K錛?
RF = 澶嶅埗絳涢夊瓨鍌ㄨ繃紼?
S = 緋葷粺琛?
TF = 琛ㄥ嚱鏁?
TR = 瑙﹀彂鍣?
U = 鐢ㄦ埛琛?
UQ = UNIQUE 綰︽潫錛堢被鍨嬫槸 K錛?
V = 瑙嗗浘
X = 鎵╁睍瀛樺偍榪囩▼
object_id鍜宒ata_object_id閮芥槸琛ㄧず鏁版嵁搴撳璞$殑鍞竴鏍囧織銆?br />
object_id鏄暟鎹簱瀵硅薄鐨勯昏緫id錛宒ata_object_id鏄暟鎹簱瀵硅薄鐨勭墿鐞唅d銆?br />
濡傛灉涓浜沷bject娌℃湁鐗╃悊灞炴х殑璇濋偅瀹冨氨涓嶅瓨鍦╠ata_object_id錛屼緥濡俻rocedure,function,package,data
type,db
link,mv瀹氫箟錛寁iew瀹氫箟錛屼復(fù)鏃惰〃錛屽垎鍖鴻〃瀹氫箟絳夌瓑榪欎簺object閮芥槸娌℃湁瀵瑰簲鐫鏌愪釜segment錛屽洜姝ゅ畠浠殑data_object_id
閮戒負(fù)絀恒?br />
褰撲竴涓〃寤虹珛鐨勬椂鍊欙紝浠栫殑object_id 鍜?
data_object_id鏄浉絳夌殑銆傚綋琛╩ove鍜宼runcate鍚巇ata_object_id浼?xì)鍙戠敓鍙樺寲銆備慨鏀硅〃緇撴瀯涓嶄細(xì)鏇存敼銆?br />
select object_id,data_object_id from user_objects where object_name=’T';
OBJECT_ID DATA_OBJECT_ID
鈥斺斺? 鈥斺斺斺?#8211;
63053 63464
SELECT HEADER_FILE,HEADER_BLOCK,BLOCKS FROM DBA_SEGMENTS WHERE
SEGMENT_NAME=’T’ AND OWNER=’TEST’;
HEADER_FILE HEADER_BLOCK BLOCKS
鈥斺斺?#8211; 鈥斺斺斺?鈥斺斺?
4 467 8
*/
SELECT * FROM sysobjects WHERE xtype='U' AND id=OBJECT_ID('Booking')
MySQLdb is an thread-compatible interface to the popular MySQL database server that provides the Python database API.
The README file has complete installation instructions.
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.
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.
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.* |
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 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.
Only a few top-level functions and attributes are defined within MySQLdb.
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:
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.
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.
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.
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.
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.
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:
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 are returned by the connect() function.
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.
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.
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.
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.
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:
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.)
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 $ |
銆銆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>
Spring鎻愪緵浜?jiǎn)涓や釜杩欐狅L(fēng)殑鏁版嵁婧愶紙閮戒綅浜巓rg.springframework.jdbc.datasource紼嬪簭鍖呴噷錛夛細(xì)
DriverManagerDataSource錛氬湪姣忎釜榪炴帴璇鋒眰鏃墮兘鏂板緩涓涓繛鎺ャ備笌DBCP鐨凚asicDataSource涓嶅悓錛孌riverManagerDataSource鎻愪緵鐨勮繛鎺ユ病鏈夎繘琛屾睜綆$悊銆?br />
SingleConnectionDataSource錛氬湪姣忎釜榪炴帴璇鋒眰鏃墮兘榪斿洖鍚屼竴涓繛鎺ャ傝櫧鐒跺畠涓嶅悓涓ユ牸鎰忎箟涓婄殑姹犵鐞嗘暟鎹簮錛屼絾鎴戜滑鍙互鎶婂畠鐪嬩綔鍙湁涓涓繛鎺ョ殑姹犮?br />
瀵逛袱涓暟鎹簮鐨勯厤緗兘綾諱技浜庨厤緗瓺BCP鐨凚asicDataSource
鍖哄埆鍦ㄤ簬鐢變簬DriverManagerDataSource鍜孲ingleConnectionDataSource閮芥病鏈夋彁渚涜繛鎺ユ睜錛屾墍浠ュ湪姝ゆ病鏈夎緗睜閰嶇疆灞炴с?br />
铏界劧榪欎袱涓暟鎹簮閮藉浜庡皬紼嬪簭鏉ヨ鏄緢涓嶉敊鐨勶紝鑰屼笖榪樺湪涓嶆柇鍙戝睍錛屼絾鎶婂畠浠敤浜庣敓浜х▼搴忚繕鏄渶瑕佽鐪熻冭檻鐨勩?/p>
SingleConnectionDataSource鍙嬌鐢ㄤ竴涓暟鎹簱榪炴帴錛屾墍浠ヤ笉閫傚悎鐢ㄤ簬澶氱嚎紼嬬▼搴忋傝? DriverMangerDataSource铏界劧鑳藉鏀寔澶氱嚎紼嬶紝浣嗗畠浼?xì)鍦ㄦ瘡娆q炴帴璇鋒眰鏃墮兘鏂板緩涓涓繛鎺ワ紝榪欐槸浠ユц兘涓轟唬浠風(fēng)殑銆傜敱浜庤繖浜涢檺鍒訛紝鎴戜滑寮虹儓 寤鴻搴旇浣跨敤鏁版嵁婧愭睜銆?/p>
鍦ㄩ氳繃鏁版嵁婧愪笌鏁版嵁搴撳緩绔嬭繛鎺ヤ箣鍚庯紝鎴戜滑灝辮瀹為檯璁塊棶鏁版嵁搴撲簡(jiǎn)錛岃屾渶鍩烘湰鐨勬柟寮忓氨鏄嬌鐢↗DBC錛岀幇鍦ㄦ垜浠氨鏉ョ湅涓鐪婼pring濡備綍璁╀嬌鐢ㄧ畝鍗曠殑JDBC鏇村姞綆渚褲?br /> Spring鍦ㄧ涓夋柟渚濊禆鍖呬腑鍖呭惈浜?jiǎn)涓や釜鏁版嵁婧愮殑瀹炵幇绫诲寘锛屽叾涓鏄疉pache鐨凞BCP錛屽叾浜屾槸 C3P0銆傚彲浠ュ湪Spring閰嶇疆鏂囦歡涓埄鐢ㄨ繖涓よ呬腑浠諱綍涓涓厤緗暟鎹簮銆?/p>
DBCP鏁版嵁婧?
DBCP 綾誨寘浣嶄簬
/lib/jakarta-commons/commons-dbcp.jar錛孌BCP鏄竴涓緷璧?Jakarta commons-
pool瀵硅薄姹犳満鍒剁殑鏁版嵁搴撹繛鎺ユ睜錛屾墍浠ュ湪綾昏礬寰勪笅榪樺繀欏誨寘鎷?lib/jakarta-
commons/commons-pool.jar銆備笅闈㈡槸浣?鐢―BCP閰嶇疆MySql鏁版嵁婧愮殑閰嶇疆鐗囨柇錛?/p>
xml 浠g爜
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3309/sampledb" />
<property name="username" value="root" />
<property name="password" value="1234" />
bean>
BasicDataSource鎻愪緵浜?jiǎn)close()鏂規(guī)硶鍏抽棴鏁版嵁婧愶紝鎵浠ュ繀欏昏瀹歞estroy-method=”close”灞炴э紝 浠ヤ究Spring瀹瑰櫒鍏抽棴鏃訛紝鏁版嵁婧愯兘澶熸甯稿叧闂傞櫎浠ヤ笂蹇呴』鐨勬暟鎹簮灞炴у錛岃繕鏈変竴浜涘父鐢ㄧ殑灞炴э細(xì)
defaultAutoCommit錛氳緗粠鏁版嵁婧愪腑榪斿洖鐨勮繛鎺ユ槸鍚﹂噰鐢ㄨ嚜鍔ㄦ彁浜ゆ満鍒訛紝榛樿鍊間負(fù) true錛?
defaultReadOnly錛氳緗暟鎹簮鏄惁浠呰兘鎵ц鍙鎿嶄綔錛?榛樿鍊間負(fù) false錛?
maxActive錛氭渶澶ц繛鎺ユ暟鎹簱榪炴帴鏁幫紝璁劇疆涓?鏃訛紝琛ㄧず娌℃湁闄愬埗錛?
maxIdle錛氭渶澶х瓑寰呰繛鎺ヤ腑鐨勬暟閲忥紝璁劇疆涓?鏃訛紝琛ㄧず娌℃湁闄愬埗錛?
maxWait錛氭渶澶х瓑寰呯鏁幫紝鍗曚綅涓烘縐掞紝 瓚呰繃鏃墮棿浼?xì)鎶ュ嚭閿欒淇℃伅锛?
validationQuery錛氱敤浜庨獙璇佽繛鎺ユ槸鍚︽垚鍔熺殑鏌ヨSQL璇彞錛孲QL璇彞蹇呴』鑷沖皯瑕佽繑鍥炰竴琛屾暟鎹紝 濡備綘鍙互綆鍗曞湴璁劇疆涓猴細(xì)“select count(*) from user”錛?
removeAbandoned錛氭槸鍚﹁嚜鎴戜腑鏂紝榛樿鏄?false 錛?
removeAbandonedTimeout錛氬嚑縐掑悗鏁版嵁榪炴帴浼?xì)鑷姩鏂紑錛屽湪removeAbandoned涓簍rue錛屾彁渚涜鍊鹼紱
logAbandoned錛氭槸鍚﹁褰曚腑鏂簨浠訛紝 榛樿涓?false錛?
C3P0鏁版嵁婧?
C3P0
鏄竴涓紑鏀炬簮浠g爜鐨凧DBC鏁版嵁婧愬疄鐜伴」鐩紝瀹冨湪lib鐩綍涓笌Hibernate涓璧峰彂甯冿紝瀹炵幇浜?jiǎn)JDBC3鍜孞DBC2鎵╁睍瑙勮寖璇存槑鐨?
Connection 鍜孲tatement 姹犮侰3P0綾誨寘浣嶄簬/lib/c3p0/c3p0-0.9.0.4.jar銆備笅闈㈡槸浣跨敤C3P0閰嶇疆涓
涓?oracle鏁版嵁婧愶細(xì)
xml 浠g爜
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value=" oracle.jdbc.driver.OracleDriver "/>
<property name="jdbcUrl" value=" jdbc:oracle:thin:@localhost:1521:ora9i "/>
<property name="user" value="admin"/>
<property name="password" value="1234"/>
bean>
ComboPooledDataSource鍜孊asicDataSource涓鏍鋒彁渚涗簡(jiǎn)涓涓敤浜庡叧闂暟鎹簮鐨刢lose()鏂規(guī)硶錛岃繖鏍鋒垜浠氨鍙互淇濊瘉Spring瀹瑰櫒鍏抽棴鏃舵暟鎹簮鑳藉鎴愬姛閲婃斁銆?
C3P0鎷ユ湁姣擠BCP鏇翠赴瀵岀殑閰嶇疆灞炴э紝閫氳繃榪欎簺灞炴э紝鍙互瀵規(guī)暟鎹簮榪涜鍚勭鏈夋晥鐨勬帶鍒訛細(xì)
acquireIncrement錛氬綋榪炴帴姹犱腑鐨勮繛鎺ョ敤瀹屾椂錛孋3P0涓嬈℃у垱寤烘柊榪炴帴鐨勬暟鐩紱
acquireRetryAttempts錛氬畾涔夊湪浠庢暟鎹簱鑾峰彇鏂拌繛鎺ュけ璐ュ悗閲嶅灝濊瘯鑾峰彇鐨勬鏁幫紝榛樿涓?0錛?
acquireRetryDelay錛氫袱嬈¤繛鎺ヤ腑闂撮殧鏃墮棿錛屽崟浣嶆縐掞紝榛樿涓?000錛?
autoCommitOnClose錛氳繛鎺ュ叧闂椂榛樿灝嗘墍鏈夋湭鎻愪氦鐨勬搷浣滃洖婊氥傞粯璁や負(fù)false錛?
automaticTestTable錛?
C3P0灝嗗緩涓寮犲悕涓篢est鐨勭┖琛紝騫朵嬌鐢ㄥ叾鑷甫鐨勬煡璇㈣鍙ヨ繘琛屾祴璇曘傚鏋滃畾涔変簡(jiǎn)榪欎釜鍙傛暟錛岄偅涔堝睘鎬referredTestQuery灝嗚蹇界暐銆?
浣?涓嶈兘鍦ㄨ繖寮燭est琛ㄤ笂榪涜浠諱綍鎿嶄綔錛屽畠?yōu)畣涓负C3P0嫻嬭瘯鎵鐢紝榛樿涓簄ull錛?
breakAfterAcquireFailure錛?鑾峰彇榪炴帴澶辮觸灝嗕細(xì)寮曡搗鎵鏈夌瓑寰呰幏鍙栬繛鎺ョ殑綰跨▼鎶涘嚭寮傚父銆備絾鏄暟鎹簮浠嶆湁鏁堜繚鐣欙紝騫跺湪涓嬫璋?nbsp;
鐢╣etConnection()鐨勬椂鍊欑戶(hù)緇皾璇曡幏鍙栬繛 鎺ャ傚鏋滆涓簍rue錛岄偅涔堝湪灝濊瘯鑾峰彇榪炴帴澶辮觸鍚庤鏁版嵁婧愬皢鐢蟲(chóng)槑宸叉柇寮騫舵案涔呭叧闂傞粯璁や負(fù)
false錛?
checkoutTimeout錛氬綋榪炴帴姹犵敤瀹屾椂瀹㈡埛绔皟鐢╣etConnection()鍚庣瓑寰呰幏鍙栨柊榪炴帴鐨勬椂闂達(dá)紝瓚呮椂鍚庡皢鎶涘嚭SQLException錛屽璁句負(fù)0鍒欐棤闄愭湡絳夊緟銆傚崟浣嶆縐掞紝榛樿涓?錛?
connectionTesterClassName錛?
閫氳繃瀹炵幇ConnectionTester鎴朡ueryConnectionTester鐨勭被鏉ユ祴璇曡繛鎺ワ紝綾誨悕闇璁劇疆涓哄叏闄愬畾鍚嶃傞粯璁や負(fù)
com.mchange.v2.C3P0.impl.DefaultConnectionTester錛?
idleConnectionTestPeriod錛氶殧澶氬皯縐掓鏌ユ墍鏈夎繛鎺ユ睜涓殑絀洪棽榪炴帴錛岄粯璁や負(fù)0琛ㄧず涓嶆鏌ワ紱
initialPoolSize錛氬垵濮嬪寲鏃跺垱寤虹殑榪炴帴鏁幫紝搴斿湪minPoolSize涓巑axPoolSize涔嬮棿鍙栧箋傞粯璁や負(fù)3錛?
maxIdleTime錛氭渶澶х┖闂叉椂闂達(dá)紝瓚呰繃絀洪棽鏃墮棿鐨勮繛鎺ュ皢琚涪寮冦備負(fù)0鎴栬礋鏁板垯姘鎬笉涓㈠純銆傞粯璁や負(fù)0錛?
maxPoolSize錛氳繛鎺ユ睜涓繚鐣欑殑鏈澶ц繛鎺ユ暟銆傞粯璁や負(fù)15錛?
maxStatements錛?
JDBC鐨勬爣鍑嗗弬鏁幫紝鐢ㄤ互鎺у埗鏁版嵁婧愬唴鍔犺澆鐨凱reparedStatement鏁伴噺銆備絾鐢變簬棰勭紦瀛樼殑Statement灞?
浜庡崟涓狢onnection 鑰屼笉鏄暣涓繛鎺ユ睜銆傛墍浠ヨ緗繖涓弬鏁伴渶瑕佽冭檻鍒板鏂歸潰鐨勫洜绱狅紝濡傛灉maxStatements涓?
maxStatementsPerConnection 鍧囦負(fù)0錛屽垯緙撳瓨琚叧闂傞粯璁や負(fù)0錛?
maxStatementsPerConnection錛氳繛鎺ユ睜鍐呭崟涓繛鎺ユ墍鎷ユ湁鐨勬渶澶х紦瀛楽tatement鏁般傞粯璁や負(fù)0錛?
numHelperThreads錛欳3P0鏄紓姝ユ搷浣滅殑錛岀紦鎱㈢殑JDBC鎿嶄綔閫氳繃甯姪榪涚▼瀹屾垚銆傛墿灞曡繖浜涙搷浣滃彲浠ユ湁鏁堢殑鎻愬崌鎬ц兘錛岄氳繃澶氱嚎紼嬪疄鐜板涓搷浣滃悓鏃惰鎵ц銆傞粯璁や負(fù)3錛?
preferredTestQuery錛氬畾涔夋墍鏈夎繛鎺ユ祴璇曢兘鎵ц鐨勬祴璇曡鍙ャ傚湪浣跨敤榪炴帴嫻嬭瘯鐨勬儏鍐典笅榪欎釜鍙傛暟鑳芥樉钁楁彁楂樻祴璇曢熷害銆傛祴璇曠殑琛ㄥ繀欏誨湪鍒濆鏁版嵁婧愮殑鏃跺欏氨瀛樺湪銆傞粯璁や負(fù)null錛?
propertyCycle錛?鐢ㄦ埛淇敼緋葷粺閰嶇疆鍙傛暟鎵ц鍓嶆渶澶氱瓑寰呯殑縐掓暟銆傞粯璁や負(fù)300錛?
testConnectionOnCheckout錛?
鍥犳ц兘娑堣楀ぇ璇峰彧鍦ㄩ渶瑕佺殑鏃跺欎嬌鐢ㄥ畠銆傚鏋滆涓簍rue閭d箞鍦ㄦ瘡涓猚onnection鎻愪氦鐨勬椂鍊欓兘 灝嗘牎楠屽叾鏈夋晥鎬с傚緩璁嬌鐢?
idleConnectionTestPeriod鎴朼utomaticTestTable
絳夋柟娉曟潵鎻愬崌榪炴帴嫻嬭瘯鐨勬ц兘銆傞粯璁や負(fù)false錛?
testConnectionOnCheckin錛氬鏋滆涓簍rue閭d箞鍦ㄥ彇寰楄繛鎺ョ殑鍚屾椂灝嗘牎楠岃繛鎺ョ殑鏈夋晥鎬с傞粯璁や負(fù)false銆?
璇婚厤緗枃浠剁殑鏂瑰紡寮曠敤灞炴э細(xì)
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/jdbc.properties"/>
bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
bean>
鍦╦dbc.properties灞炴ф枃浠朵腑瀹氫箟灞炴у鹼細(xì)
jdbc.driverClassName= com.mysql.jdbc.Driver
jdbc.url= jdbc:mysql://localhost:3309/sampledb
jdbc.username=root
jdbc.password=1234
鎻愮ず 緇忓父鏈夊紑鍙戣呭湪${xxx}鐨勫墠鍚庝笉灝忓績(jī)閿叆涓浜涚┖鏍鹼紝榪欎簺絀烘牸瀛楃灝嗗拰鍙橀噺鍚堝茍鍚庝綔涓哄睘鎬х殑鍊箋傚錛?鐨勫睘鎬ч厤緗」錛屽湪鍓嶅悗閮芥湁絀烘牸錛岃瑙f瀽鍚庯紝username鐨勫間負(fù)“ 1234 ”錛岃繖灝嗛犳垚鏈緇堢殑閿欒錛屽洜姝ら渶瑕佺壒鍒皬蹇?jī)銆?/p>
鑾峰彇JNDI鏁版嵁婧?
濡傛灉搴旂敤閰嶇疆鍦ㄩ珮鎬ц兘鐨勫簲鐢ㄦ湇鍔″櫒錛堝WebLogic鎴朩ebsphere絳夛級(jí)涓婏紝鎴戜滑鍙兘鏇村笇鏈涗嬌鐢ㄥ簲鐢ㄦ湇鍔″櫒鏈韓鎻愪緵鐨勬暟鎹簮銆傚簲鐢ㄦ湇鍔″櫒鐨勬暟鎹簮
浣跨敤JNDI寮鏀捐皟鐢ㄨ呬嬌鐢紝Spring涓烘涓撻棬鎻愪緵寮曠敤JNDI璧勬簮鐨凧ndiObjectFactoryBean綾匯備笅闈㈡槸涓涓畝鍗曠殑閰嶇疆錛?/p>
xml 浠g爜
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/bbt"/>
bean>
閫氳繃jndiName鎸囧畾寮曠敤鐨凧NDI鏁版嵁婧愬悕縐般?
Spring 2.0涓鴻幏鍙朖2EE璧勬簮鎻愪緵浜?jiǎn)涓涓猨ee鍛藉悕絀洪棿錛岄氳繃jee鍛藉悕絀洪棿錛屽彲浠ユ湁鏁堝湴綆鍖朖2EE璧勬簮鐨勫紩鐢ㄣ備笅闈㈡槸浣跨敤jee鍛藉悕絀洪棿寮曠敤JNDI鏁版嵁婧愮殑閰嶇疆錛?
xml 浠g爜
<beans xmlns=http://www.springframework.org/schema/beans
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:jee=http://www.springframework.org/schema/jee
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee
<jee:jndi-lookup id="dataSource" jndi-name=" java:comp/env/jdbc/bbt"/>
beans>
Spring鐨勬暟鎹簮瀹炵幇綾?
Spring
鏈韓涔熸彁渚涗簡(jiǎn)涓涓畝鍗曠殑鏁版嵁婧愬疄鐜扮被DriverManagerDataSource 錛屽畠浣嶄簬
org.springframework.jdbc.datasource鍖呬腑銆傝繖涓被瀹炵幇浜?jiǎn)javax.sql.DataSource鎺ュ彛錛屼絾
瀹冨茍娌?
鏈夋彁渚涙睜鍖栬繛鎺ョ殑鏈哄埗錛屾瘡嬈¤皟鐢╣etConnection()鑾峰彇鏂拌繛鎺ユ椂錛屽彧鏄畝鍗曞湴鍒涘緩涓涓柊鐨勮繛鎺ャ傚洜姝わ紝榪欎釜鏁版嵁婧愮被姣旇緝閫傚悎鍦ㄥ崟鍏冩祴璇?
鎴栫畝 鍗曠殑鐙珛搴旂敤涓嬌鐢紝鍥犱負(fù)瀹冧笉闇瑕侀澶栫殑渚濊禆綾匯?
涓嬮潰錛屾垜浠潵鐪嬩竴涓婦riverManagerDataSource鐨勭畝鍗曚嬌鐢細(xì)褰撶劧錛屾垜浠篃鍙互閫氳繃閰嶇疆鐨勬柟寮忕洿鎺ヤ嬌鐢―riverManagerDataSource銆?/p>
java 浠g爜
DriverManagerDataSource ds = new DriverManagerDataSource ();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3309/sampledb");
ds.setUsername("root");
ds.setPassword("1234");
Connection actualCon = ds.getConnection();
灝忕粨
涓嶇閲囩敤浣曠鎸佷箙鍖栨妧鏈紝閮介渶瑕佸畾涔夋暟鎹簮銆係pring闄勫甫浜?jiǎn)涓や釜鏁版嵁婧愮殑瀹炵幇绫诲寘锛屼綘鍙互鑷閫夋嫨榪涜瀹氫箟銆傚湪瀹為檯閮ㄧ講鏃訛紝鎴戜滑鍙兘浼?xì)鐩存帴閲囩敤搴旂敤鏈? 鍔″櫒鏈韓鎻愪緵鐨勬暟鎹簮錛岃繖鏃訛紝鍒欏彲浠ラ氳繃JndiObjectFactoryBean鎴杍ee鍛藉悕絀洪棿寮曠敤JNDI涓殑鏁版嵁婧愩?
DBCP涓嶤3PO閰嶇疆鐨勫尯鍒細(xì)
C3PO 錛?/p>
xml 浠g爜
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>oracle.jdbc.driver.OracleDrivervalue>
property>
<property name="jdbcUrl">
<value>jdbc:oracle:thin:@10.10.10.6:1521:DataBaseNamevalue>
property>
<property name="user">
<value>testAdminvalue>
property>
<property name="password">
<value>123456value>
property>
bean>
DBCP錛?/p>
xml 浠g爜
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDrivervalue>
property>
<property name="url">
<value>jdbc:oracle:thin:@10.10.10.6:1521:DataBaseNamevalue>
property>
<property name="username">
<value>testAdminvalue>
property>
<property name="password">
<value>123456value>
property>
bean>
鍐嶄婦涓畝鍗曠殑渚嬪瓙
with a as (select * from test)
select * from a;
鍏跺疄灝辨槸鎶婁竴澶у爢閲嶅鐢ㄥ埌鐨凷QL璇彞鏀懼湪with as 閲岄潰錛屽彇涓涓埆鍚嶏紝鍚庨潰鐨勬煡璇㈠氨鍙互鐢ㄥ畠
榪欐牱瀵逛簬澶ф壒閲忕殑SQL璇彞璧峰埌涓涓紭鍖栫殑浣滅敤錛岃屼笖娓呮鏄庝簡(jiǎn)
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);
@Entity
@Table(name = "customer")
public class CustomerEO implements java.io.Serializable {
……
private CustomerType type;
@Enumerated(EnumType.STRING)
public CustomerType getType() {
return type;
}
public void setType(CustomerType type) {
this.type = type;
}
public enum CustomerType {
COMPETITOR, INVESTOR, PARTNER, VENDER
}
}
鍦ㄥ疄浣撲腑铏界劧鏍囨敞鎴愭灇涓劇被鍨嬶紝浣嗗綋瀹炰綋鎸佷箙鍖栧悗錛岃〃涓墍瀵瑰簲鐨勫間粛鏃ф槸鍩烘湰鐨勬暟鎹被鍨嬶紝浠ヤ笂浠g爜鍒涘緩琛ㄧ殑SQL璇彞鏄細(xì)
CREATE TABLE customer (
id int(20) NOT NULL auto_increment,
name varchar(255),
type varchar(255),
PRIMARY KEY (id)
)
浣跨敤鏋氫婦綾誨瀷鍚庯紝鍦ㄥ垱寤哄疄浣撴椂渚垮彲浠ョ洿鎺ュ紩鐢ㄦ灇涓劇被鍨嬶紝渚嬪浠ヤ笅浠g爜鎵紺恒?/font>
CustomerEO customer = new CustomerEO();
customer.setName("Janet2");
customer.setType(CustomerType.PARTNER);
entityManager.persist(customer);
鍦ㄤ嬌鐢?/span>@Enumerated娉ㄩ噴鏃訛紝闇瑕佹敞鎰忎互涓嬪嚑涓棶棰橈細(xì)
l 鍥犱負(fù)鏋氫婦綾誨瀷鐨勬湁鍚嶇О鍜屽間袱涓睘鎬э紝鎵浠ュ湪鎸佷箙鍖栨椂鍙互閫夋嫨鎸佷箙鍖栧悕縐版垨鏄寔涔呭寲鍊箋傞氳繃EnumType鏉ュ畾涔夛紝瀹冩湁涓や釜鍊煎涓嬫墍紺恒?/span>
public enum EnumType {
ORDINAL,
STRING
}
ORDINAL琛ㄧず鎸佷箙鍖栫殑涓烘灇涓劇被鍨嬬殑鍊鹼紝STRING琛ㄧず鎸佷箙鍖栫殑涓烘灇涓劇被鍨嬬殑鍚嶇О銆傞粯璁や負(fù)ORDINAL錛屾寔涔呭寲鍊箋備緥濡備互涓婄ず渚嬩腑鏍囨敞鐨勪負(fù)STRING錛岃繖鏍鋒寔涔呭寲瀹炰綋鍚庯紝鏁版嵁搴撲腑淇濆瓨鐨勬槸鏋氫婦綾誨瀷鐨勫悕縐幫紝濡傚浘鎵紺恒?/span>
鑻ユ鏃舵敼鎴?/span>ORDINAL錛屼唬鐮佸涓嬶細(xì)
@Enumerated(EnumType.ORDINAL)
public CustomerType getType() {
return type;
}
鍒欏悓鏍鋒寔涔呭寲鐨勫疄浣撳悗錛屾暟鎹簱淇濆瓨鐨勭粨鏋滃鍥?/span>鎵紺恒?/span>
l 濡備綍閫夋嫨STRING鍜?/span>ORDINAL錛?/span>
濡傛灉浣跨敤STRING淇濆瓨錛岃櫧鐒朵粠鏁版嵁搴撲腑鏌ヨ鏁版嵁鏃墮潪甯哥洿瑙傦紝鑳藉娓呮鐨勭湅鍑?guó)櫙ゾc誨瀷浠h〃鎰忎箟錛屼絾榪欐牱涔熶細(xì)甯︽潵鍏朵粬鐨勯棶棰樸傝嫢姝ゆ椂鏋氫婦綾誨瀷鐨勫畾涔夋敼鍙橈紝渚嬪涓婁緥涓殑鏋氫婦綾誨瀷鍚嶇О鏀逛負(fù)錛?/span>
public enum CustomerType {
CUST_COMPETITOR, INVESTOR, PARTNER, VENDER
}
鍒欐鏃舵暟鎹簱涓繚瀛樼殑“COMPETITOR”鐨勫煎皢涓嶈兘杞寲涓烘灇涓劇被鍨?/span>CustomerType涓殑“CUST_COMPETITOR”鐨勫箋備絾鑻ヤ嬌鐢?/span>ORDINAL鍒欎笉浼?xì)甯︽潵杩櫩U嶉棶棰樸傛墍浠ュ緩璁嬌鐢?/span>ORDINAL綾誨瀷鏉ユ寔涔呭寲鏋氫婦綾誨瀷銆?/span>
l 鏋氫婦綾誨瀷鐨勫畾涔変綅緗紝瀹炰綋澶栭儴VS瀹炰綋鍐呴儴銆?/span>
涓婁緥涓?/span>CustomerType鏋氫婦綾誨瀷瀹氫箟鍦?/span>CustomerEO瀹炰綋鍐呴儴錛岃繖鏄洜涓哄彧鏈?/span>CustomerEO榪欎釜瀹炰綋浼?xì)鋴社?/span>CustomerType綾誨瀷錛屽叾浠栫殑瀹炰綋涓嶄細(xì)浣跨敤璇ョ被鍨嬨傝綾誨瀷涓庤繖涓疄浣撳叧緋葷揣瀵嗚仈緋匯?/span>
浣嗚嫢姝ゆ椂澶氫釜瀹炰綋鍏敤涓涓灇涓劇被鍨嬫椂錛屽垯鍙互灝嗘灇涓劇被鍨嬪崟鐙畾涔夛紝瀹氫箟鍦ㄥ疄浣撶殑澶栭儴銆傛湁榪欐牱涓涓灇涓劇被鍨?/span>BusinessLine錛屽畠瀹氫箟鍦ㄥ疄浣撳閮紝浠g爜濡備笅錛?/span>
public enum BusinessLine {
REAL_ESTATE,FINANCE, NON_PROFIT
}
渚嬪CustomerEO瀹炰綋澧炲姞涓涓?/span>BusinessLine鐨勬灇涓劇被鍨嬶紝浠g爜濡備笅鎵紺恒?/span>
@Entity
@Table(name = "customer")
public class CustomerEO implements java.io.Serializable {
……
private BusinessLine businessLine;
@Enumerated(EnumType.STRING)
public BusinessLine getBusinessLine() {
return businessLine;
}
public void setBusinessLine(BusinessLine businessLine) {
this.businessLine = businessLine;
}
}
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;
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 mysql5>瀹屾垚浠ヤ笂鎿嶄綔浠ュ悗榪涜鍒濆鍖栨暟鎹簱,榪涘叆宸茬粡瀹夎濂界殑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
瑙e喅鏂規(guī)硶錛?/span>
1銆?/span>鏀硅〃娉曘傚彲鑳芥槸浣犵殑甯愬彿涓嶅厑璁鎬粠榪滅▼鐧婚檰錛屽彧鑳藉湪localhost銆傝繖涓椂鍊欏彧瑕佸湪localhost鐨勯偅鍙扮數(shù)鑴戯紝鐧誨叆mysql鍚庯紝鏇存敼 “mysql” 鏁版嵁搴撻噷鐨?/span> “user” 琛ㄩ噷鐨?/span> “host” 欏癸紝浠?/span>“localhost”鏀圭О“%”
mysql -u root -pvmwaremysql>use mysql; |
2. 鎺堟潈娉曘備緥濡傦紝浣犳兂myuser浣跨敤mypassword浠庝換浣曚富鏈鴻繛鎺ュ埌mysql鏈嶅姟鍣ㄧ殑璇濄?/span>
GRANT ALL PRIVILEGES ON *.* TO ‘myuser’@'%’ IDENTIFIED BY ‘mypassword’ WI |
濡傛灉浣犳兂鍏佽鐢ㄦ埛myuser浠?/span>ip涓?/span>192.168.1.6鐨勪富鏈鴻繛鎺ュ埌mysql鏈嶅姟鍣紝騫朵嬌鐢?/span>mypassword浣滀負(fù)瀵嗙爜
GRANT ALL PRIVILEGES ON *.* TO ‘myuser’@'192.168.1.3′ IDENTIFIED BY |
SQL Server涓殑鏃ユ湡綾誨瀷鍖呮嫭datetime鍜宻malldatetime錛屼粎鑳藉鐞嗗彲浠ヨ瘑鍒負(fù)1753騫達(dá)綖9999騫撮棿鐨勬棩鏈熺殑鍊鹼紝娌℃湁鍗曠嫭鐨勬棩鏈熷瀷鎴栨椂闂村瀷銆?/font>
datetime綾誨瀷澶勭悊浠?753騫?鏈?鏃ワ綖9999騫?2鏈?1鏃ョ殑鏃ユ湡鍜屾椂闂存暟鎹紝綺劇‘搴︿負(fù)鐧懼垎涔嬩笁縐掋傚嵆錛氬浜?.000锝?.001銆?.009鐨勬棩鏈熷鹼紝璋冩暣涓?.000錛涘浜?.002锝?.004鐨勬棩鏈熷鹼紝璋冩暣涓?.003錛涘浜?.005锝?.008鐨勬棩鏈熷鹼紝璋冩暣涓?.007銆?/font>
渚嬪錛屼笅闈㈢殑浠g爜鍦ㄨ緭鍏ユ椂錛屽叾鏃墮棿綺劇‘搴︿負(fù)鐧懼垎涔嬩竴縐掞紝浣嗙粡鏁版嵁搴撲繚瀛樺悗鍐嶆樉紺哄嚭鏉ワ紝鍏剁粨鏋滃氨宸茬粡鍋氫簡(jiǎn)澶勭悊銆?/font>
DECLARE @t TABLE(date char(21))
INSERT @t SELECT '1900-1-1 00:00:00.000'
...
INSERT @t SELECT '1900-1-1 00:00:00.009'
SELECT date,杞崲鍚庣殑鏃ユ湡=CAST(date as datetime) FROM @t
/*--緇撴灉
date 杞崲鍚庣殑鏃ユ湡
---------------------------------- ----------------------------
1900-1-1 00:00:00.000 1900-01-01 00:00:00.000
...
1900-1-1 00:00:00.000 1900-01-01 00:00:00.010
datetime鐨勫瓨鍌ㄩ暱搴︿負(fù)8瀛楄妭錛屾棩鏈熷拰鏃墮棿鍚勭敤4涓瓧鑺傚瓨鍌紝絎竴涓?瀛楄妭瀛樺偍鑷?900騫?鏈?鏃ヤ箣鍓嶆垨涔嬪悗鐨勫ぉ鏁幫紙浠?900騫?鏈?鏃ヤ負(fù)鍒嗙晫鐐癸紝鍦?900騫?鏈?鏃ヤ箣鍓嶇殑鏃ユ湡鐨勫ぉ鏁板皬浜?錛屽湪1900騫?鏈?鏃ヤ箣鍚庣殑鏃ユ湡鐨勫ぉ鏁板ぇ浜?錛夈傚彟澶栦竴涓?瀛楄妭瀛樺偍浠ュ崍澶滐紙00:00:00.000錛夊悗姣鏁版墍浠h〃鐨勬瘡澶╃殑鏃墮棿銆?/font>
渚嬪錛屼笅闈㈢殑浠g爜婕旂ず浜?jiǎn)datetime鍙橀噺涓紝浠呭寘鍚崟綰殑鏃ユ湡鍜屽崟綰殑鏃墮棿鏃訛紝鏃ユ湡瀛樺偍鐨勫崄鍏繘鍒跺瓨鍌ㄨ〃紺虹粨鏋溿?/font>
DECLARE @dt datetime
--鍗曠函鐨勬棩鏈?/font>
SET @dt='1900-1-2'
SELECT CAST(@dt as binary(8))
--緇撴灉: 0x0000000100000000
--鍗曠函鐨勬椂闂?/font>
SET @dt='00:00:01'
SELECT CAST(@dt as binary(8))
--緇撴灉: 0x000000000000012C
smalldatetime綾誨瀷澶勭悊浠?900騫?鏈?鏃ワ綖2079騫?鏈? 鏃ョ殑鏃ユ湡鍜屾椂闂存暟鎹紝綺劇‘鍒板垎閽熴?9.998縐掓垨鏇翠綆鐨剆malldatetime鍊煎悜涓嬭垗鍏ヤ負(fù)鏈鎺ヨ繎鐨勫垎閽燂紝29.999縐掓垨鏇撮珮鐨剆malldatetime鍊煎悜涓婅垗鍏ヤ負(fù)鏈鎺ヨ繎鐨勫垎閽熴?/font>
smalldatetime鐨勫瓨鍌ㄩ暱搴︿負(fù)4瀛楄妭錛岀涓涓?瀛楄妭瀛樺偍鑷?900騫?鏈?鏃ヤ箣鍚庣殑澶╂暟銆傚彟澶栦竴涓?瀛楄妭瀛樺偍鍗堝錛?0:00:00.000錛夊悗鐨勫垎閽熸暟銆?/font>
渚嬪錛屼笅闈㈢殑浠g爜婕旂ず浜?jiǎn)smalldatetime鍙橀噺涓紝浠呭寘鍚崟綰殑鏃ユ湡鍜屽崟綰殑鏃墮棿鏃訛紝鏃ユ湡瀛樺偍鐨勫崄鍏繘鍒跺瓨鍌ㄨ〃紺虹粨鏋溿?/font>
DECLARE @dt smalldatetime
--鍗曠函鐨勬棩鏈?/font>
SET @dt='1900-1-2'
SELECT CAST(@dt as binary(4))
--緇撴灉: 0x00010000
--鍗曠函鐨勬椂闂?/font>
SET @dt='00:10'
SELECT CAST(@dt as binary(4))
--緇撴灉: 0x0000000A
鏃ユ湡鐢卞勾銆佹湀銆佹棩銆佹椂絳夊涓儴鍒嗙粍鎴愶紝瀹冪殑澶勭悊鐩稿澶嶆潅錛屽洜姝わ紝SQL Server鎻愪緵浜?jiǎn)澶ч噺鐨勬棩鏈熷鐞嗗嚱鏁板Q岀敤浠ュ畬鎴愬悇縐嶆棩鏈熸暟鎹殑澶勭悊銆傛帉鎻″ソ榪欎簺鍑芥暟錛屽瀹屾垚鏁版嵁搴撶殑鍚勭鏃ユ湡澶勭悊闈炲父蹇呰錛屾湰鑺傚皢浠嬬粛鍑犱釜甯哥敤鐨勬棩鏈熷鐞嗗嚱鏁般傛湡澧炲噺鍑芥暟鍙互瀵規(guī)棩鏈熸寚瀹氶儴鍒嗙殑鍊艱繘琛屽鍑忥紝騫惰繑鍥炲鐞嗗悗鐨勬棩鏈熷鹼紝SQL Server鎻愪緵鐨勬棩鏈熷鍑忓嚱鏁頒負(fù)DATEADD銆?/font>
DATEADD鐨勫叿浣撹娉曞涓嬶細(xì)DATEADD ( datepart , number, date )
鍏朵腑鍖呮嫭浠ヤ笅鍙傛暟銆?/font>
¡ datepart錛氭槸瑙勫畾搴斿悜鏃ユ湡鐨勫摢涓閮ㄥ垎榪斿洖鏂板肩殑鍙傛暟銆傝〃2-1鍒楀嚭浜?jiǎn)SQL Server鏀寔鐨勬棩鏈熼儴鍒嗐佺緝鍐欏強(qiáng)鍚箟銆?/font>
DATEADD銆?font face="瀹?huà)浣? MS Song">DATEDIFF鏀寔鐨勬棩鏈熼儴鍒嗐佺緝鍐欏強(qiáng)鍚箟
鏃ユ湡閮ㄥ垎 |
緙?font face="瀹?huà)浣? MS Song"> 鍐?/font> |
鍚?font face="瀹?huà)浣? MS Song"> 涔?/font> |
Year |
yy , yyyy |
騫翠喚 |
Quarter |
qq , q |
瀛e害 |
Month |
mm , m |
鏈堜喚 |
Dayofyear |
dy,y |
鏃?/font> |
Day |
dd , d |
|
Week |
wk , ww |
鏄熸湡 |
Hour |
Hh |
灝忔椂 |
Minute |
mi , n |
鍒嗛挓 |
Second |
ss , s |
縐?/font> |
Millisecond |
Ms |
姣 |
¡ number錛氭槸鐢ㄦ潵澧炲姞datepart鐨勫箋傛鏁拌〃紺哄鍔狅紝璐熸暟琛ㄧず鍑忓皯錛屽鏋滄寚瀹氱殑鏄潪鏁存暟鍊鹼紝鍒欏拷鐣ユ鍊肩殑灝忔暟閮ㄥ垎錛屼笉鍋氬洓鑸嶄簲鍏ュ鐞嗐備緥濡傦紝DATEADD錛圖ay,1.7,date錛夛紝琛ㄧずdate澧炲姞1澶┿?/font>
¡ date錛氭槸榪斿洖datetime鎴杝malldatetime鍊兼垨鏃ユ湡鏍煎紡瀛楃涓茬殑琛ㄨ揪寮忋?/font>
濡傛灉date鏄痵malldatetime錛屽垯榪斿洖smalldatetime錛屽惁鍒欒繑鍥瀌atetime銆俤ate涓簊malldatetime錛孌atepart涓篠econd錛坰s,s錛夋垨Millisecond錛坢s錛夋椂錛岃繑鍥炲煎皢鏍規(guī)嵁鏃ユ湡澧炲噺鐨勭粨鏋滆皟鏁村埌鍒嗛挓錛沝ate涓篸atetime錛孌atepart涓篗illisecond錛坢s錛夋椂錛岃繑鍥炲煎皢鏍規(guī)嵁鏃ユ湡澧炲噺鐨勭粨鏋滆皟鏁翠負(fù)鐧懼垎涔嬩笁縐掋傝皟鏁磋鍒欏彲浠ュ弬鑰?.1鑺傜殑鐩稿叧璇存槑銆?/font>
date鍏佽鐩存帴涓巒umber榪涜澧炲噺璁$畻錛屽嵆瀵逛簬DATEADD錛圖ay,number,date錛夛紝絳夊悓浜巇ate+number銆?/font>
鏃ユ湡淇℃伅鑾峰彇鍑芥暟鐢ㄤ簬鑾峰彇鏃ユ湡鎸囧畾閮ㄥ垎鐨勭浉鍏充俊鎭紝甯哥敤鐨勬棩鏈熶俊鎭幏鍙栧嚱鏁板琛?-2鎵紺恒?/font>
甯哥敤鐨勬棩鏈熶俊鎭幏鍙栧嚱鏁?/font>
鍔熻兘璇存槑 |
璇?font face="瀹?huà)浣? MS Song"> 娉?/font> |
鍙傛暟鍙?qiáng)杩斿洖鍊兼暟鎹被鍨嬭鏄?/font> |
榪斿洖浠h〃鎸囧畾鏃ユ湡鐨勬寚瀹氭棩鏈熼儴鍒嗙殑瀛楃涓?/font> |
DATENAME(datepart,date) |
datepart鏄寚瀹氬簲榪斿洖鐨勬棩鏈熼儴鍒嗙殑鍙傛暟錛屽叾瀹氫箟濡傝〃2-3鎵紺恒俤ate鏄繑鍥瀌atetime鎴杝malldatetime鍊兼垨鏃ユ湡鏍煎紡瀛楃涓茬殑琛ㄨ揪寮忋侱ATENAME鍑芥暟榪斿洖nvarchar錛孌ATEPART鍑芥暟榪斿洖int |
榪斿洖浠h〃鎸囧畾鏃ユ湡鐨勬寚瀹氭棩鏈熼儴鍒嗙殑鏁存暟 |
DATEPART(datepart,date) |
|
榪斿洖琛ㄧず鎸囧畾鏃ユ湡涓殑騫翠喚鐨勬暣鏁?/font> |
YEAR(date) |
榪斿洖int |
榪斿洖琛ㄧず鎸囧畾鏃ユ湡涓殑鏈堜喚鐨勬暣鏁?/font> |
MONTH(date) |
榪斿洖int |
榪斿洖琛ㄧず鎸囧畾鏃ユ湡涓殑澶╃殑鏁存暟 |
DAY(date) |
榪斿洖int |
DATENAME銆?font face="瀹?huà)浣? MS Song">DATEPART鏀寔鐨勬棩鏈熼儴鍒嗐佺緝鍐欏強(qiáng)鍚箟
鏃ユ湡閮ㄥ垎 |
緙?font face="瀹?huà)浣? MS Song"> 鍐?/font> |
鍚?font face="瀹?huà)浣? MS Song"> 涔?/font> |
Year |
yy , yyyy |
騫翠喚 |
Quarter |
qq , q |
瀛e害 |
Month |
mm , m |
鏈堜喚 |
Dayofyear |
dy , y |
鏃?/font> |
Day |
dd , d |
|
Week |
wk , ww |
鑷勾鍒濆紑濮嬬殑絎嚑涓槦鏈?/font> |
Weekday |
Dw |
鏄熸湡鍑狅紙渚嬪鏄熸湡涓銆佹槦鏈熶簩錛?/font> |
Hour |
Hh |
灝忔椂 |
Minute |
mi , n |
鍒嗛挓 |
Second |
ss , s |
縐掋俤ate涓簊malldatetime鏃訛紝濮嬬粓榪斿洖0 |
Millisecond |
Ms |
姣銆俤ate涓簊malldatetime鏃訛紝濮嬬粓榪斿洖0錛屼負(fù)datetime鏃訛紝榪斿洖鐧句喚涔嬩笁縐?/font> |
DATEPART錛圵eek,date錛夎繑鍥炵殑鏄熸湡璁$畻鏂瑰紡錛屾槸鎸夌収鏄熸湡鏃ヤ負(fù)涓鍛ㄧ殑絎竴澶╋紝榪欑偣涓庝腑鍥戒漢鐨勬棩鏈熷鐞嗕範(fàn)鎯笉鍚岋紝鍦ㄤ嬌鐢ㄦ椂瑕佹敞鎰忚繖涓鐐廣侱ATENAME鍑芥暟榪斿洖鎸囧畾鏃ユ湡鐨勬寚瀹氭棩鏈熼儴鍒嗙殑瀛楃涓詫紝鍏惰繑鍥炵殑鍏蜂綋瀛楃涓插鹼紝涓嶴ET DATEFIRST鍙?qiáng)SET DATELANGUAGE閫夐」鐨勮緗湁鍏熾備嬌鐢―ATEPART錛圵eekday,date錛夋椂錛屽叾榪斿洖鐨勫間笌SET DATEFIRST閫夐」鐨勮緗湁鍏籌紝鍏蜂綋鐨勫皢鍦?.3鑺備腑璇存槑銆?/font>
鏃ユ湡宸艱綆楀嚱鏁扮敤浜庤綆椾袱涓粰瀹氭棩鏈熸寚瀹氶儴鍒嗙殑杈圭晫鏁幫紝SQL Server鎻愪緵鐨勬棩鏈熷樊鍊艱綆楀嚱鏁頒負(fù)DATEDIFF銆?/font>
DATEDIFF鐨勫叿浣撹娉曞涓嬶細(xì)
DATEDIFF ( datepart , startdate , enddate )
鍏朵腑鍖呮嫭浠ヤ笅鍙傛暟銆?/font>
¡ datepart錛氳瀹氫簡(jiǎn)搴斿湪鏃ユ湡鐨勫摢涓閮ㄥ垎璁$畻宸錛屽叾瀹氫箟濡傝〃2-1鎵紺恒?/font>
¡ startdate錛氳瀹氫簡(jiǎn)璁$畻鐨勫紑濮嬫棩鏈熴?/font>
¡ enddate錛氳瀹氫簡(jiǎn)璁$畻鐨勭粓姝㈡棩鏈熴?/font>
榪斿洖綾誨瀷錛歩nteger
璁$畻鐨勫紑濮嬫棩鏈熷拰緇堟鏃ユ湡錛屽彲浠ユ槸鏃ユ湡鎴栨棩鏈熸牸寮忕殑瀛楃涓層傝綆楃殑鏂規(guī)硶鏄粠enddate鍑忓幓startdate銆傚鏋渟tartdate姣攅nddate鏅氾紝榪斿洖璐熷箋傚綋緇撴灉瓚呭嚭鏁存暟鍊艱寖鍥達(dá)紝DATEDIFF灝變駭鐢熼敊璇傚浜庢縐掞紝鏈澶ф暟鏄?4澶?0灝忔椂31鍒嗛挓23.647縐掋傚浜庣錛屾渶澶ф暟鏄?8騫淬?/font>
璁$畻璺ㄥ垎閽熴佺鍜屾縐掕繖浜涜竟鐣岀殑鏂規(guī)硶錛屼嬌寰桪ATEDIFF緇欏嚭鐨勭粨鏋滃湪鍏ㄩ儴鏁版嵁綾誨瀷涓槸涓鑷寸殑銆傜粨鏋滄槸甯︽璐熷彿鐨勬暣鏁板鹼紝鍏剁瓑浜庤法絎竴涓拰絎簩涓棩鏈熼棿鐨刣atepart杈圭晫鏁般備緥濡傦紝鍦?005騫?鏈?鏃ュ拰2005騫?鏈?1鏃ヤ箣闂寸殑鏈堜喚鏁版槸1銆?/font>
鍏朵粬甯哥敤鐨勬棩鏈熷鐞嗙浉鍏沖嚱鏁板寘鎷互涓嬪嚑涓?/font>
GETDATE鎸夌収datetime鍊艱繑鍥炲綋鍓嶇郴緇熸棩鏈熷拰鏃墮棿銆?/font>
GETDATE鐨勮娉曞涓嬶細(xì)
GETDATE()
榪斿洖綾誨瀷錛歞atetime
ISDATE紜畾杈撳叆鐨勮〃杈懼紡鏄惁鏈夋晥鏃ユ湡銆?/font>
鍦ㄨ緭鍏ユ棩鏈熻〃杈懼紡鏃訛紝鏃ユ湡閮芥槸浠ユ棩鏈熸牸寮忕殑瀛楃涓叉彁渚涚殑錛岀敱浜庝笉鍚岀殑鍖哄煙鏈変笉鍚岀殑鏃ユ湡鏍煎紡錛屾墍浠ュ茍涓嶈兘淇濊瘉杈撳叆鐨勬棩鏈熻〃杈懼紡鑳藉琚玈QL Server璇嗗埆錛岃繖縐嶆儏鍐典笅錛屽氨闇瑕佺敤ISDATE鏉ュ垽鏂棩鏈熻〃杈懼紡鑳藉惁姝g‘鍦拌SQL Server璇嗗埆浜?jiǎn)銆?/font>
ISDATE鐨勮娉曞涓嬶細(xì)
ISDATE(expression)
榪斿洖綾誨瀷錛歩nt
CONVERT灝嗘煇縐嶆暟鎹被鍨嬬殑琛ㄨ揪寮忔樉寮忚漿鎹負(fù)鍙︿竴縐嶆暟鎹被鍨嬨?/font>
涓ユ牸鏉ヨ錛孋ONVERT涓嶅睘浜庢棩鏈熷鐞嗗嚱鏁幫紝鍙槸瀹冭緇忓父鐢ㄤ簬鏃ユ湡澶勭悊涓紝鎵浠ヨ繖閲屾妸瀹冨垪鍏ヤ簡(jiǎn)鍏朵粬鏃ユ湡澶勭悊鍑芥暟錛屼笅闈㈡槸CONVERT鐨勭敤娉曟弿榪幫紙鍙噸鐐硅鏄庡湪鏃ユ湡澶勭悊涓殑搴旂敤錛夈?/font>
CONVERT鐨勫叿浣撹娉曞涓嬶細(xì)
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
鍏朵腑鍖呮嫭浠ヤ笅鍙傛暟銆?/font>
¡ expression錛氭槸瑕佽漿鎹㈡暟鎹被鍨嬬殑鏈夋晥SQL Server琛ㄨ揪寮忋?/font>
¡ data_type錛氭槸expression杞崲鍚庣殑鏁版嵁綾誨瀷錛宭ength鏄浜庢湁綺懼害瀹氫箟闇瑕佺殑data_type鐨勭簿搴﹀畾涔夛紝瀵逛簬娌℃湁綺懼害瀹氫箟闇瑕佺殑data_type錛岃鍙傛暟鍙互鐪佺暐銆?/font>
¡ style錛氬畾涔夋暟鎹被鍨嬭漿鎹㈡椂鐨勬牸寮忥紝瀵逛簬鏃ユ湡綾誨瀷鐨勮漿鎹紝瀹冪殑瀹氫箟濡傝〃2-4鎵紺恒?/font>
琛?font face="瀹?huà)浣? MS Song">2-4 style鍦ㄦ棩鏈熻漿鎹腑鐨勮鏄?/font>
涓嶅甫涓栫邯鏁頒綅 |
甯︿笘綰暟浣?/font> |
鏍?font face="瀹?huà)浣? MS Song"> 鍑?/font> |
杈撳叆/杈撳嚭 |
鈥?/font> |
0鎴?00 |
榛樿鍊?/font> |
mon dd yyyy hh:miAM(鎴?PM) |
1 |
101 |
緹庡浗 |
mm/dd/yyyy |
2 |
102 |
ANSI |
yy.mm.dd |
3 |
103 |
鑻卞浗/娉曞浗 |
dd/mm/yy |
4 |
104 |
寰峰浗 |
dd.mm.yy |
5 |
105 |
鎰忓ぇ鍒?/font> |
dd-mm-yy |
6 |
106 |
鈥?/font> |
dd mon yy |
7 |
107 |
鈥?/font> |
mon dd, yy |
8 |
108 |
鈥?/font> |
hh:mm:ss |
鈥?/font> |
9鎴?09 |
榛樿鍊?姣 |
mon dd yyyy hh:mi:ss:mmmAM(鎴朠M) |
10 |
110 |
緹庡浗 |
mm-dd-yy |
11 |
111 |
鏃ユ湰 |
yy/mm/dd |
12 |
112 |
ISO |
yymmdd |
鈥?/font> |
13鎴?13 |
嬈ф床榛樿鍊?姣 |
dd mon yyyy hh:mm:ss:mmm(24h) |
14 |
114 |
鈥?/font> |
hh:mi:ss:mmm(24h) |
鈥?/font> |
20鎴?20 |
ODBC瑙勮寖 |
yyyy-mm-dd hh:mm:ss[.fff] |
鈥?/font> |
21鎴?21 |
ODBC瑙勮寖錛堝甫姣錛?/font> |
yyyy-mm-dd hh:mm:ss[.fff] |
鈥?/font> |
126 |
ISO8601 |
yyyy-mm-ddThh:mm:ss.mmm |
鈥?/font> |
130 |
Hijri |
dd mon yyyy hh:mi:ss:mmmAM |
鈥?/font> |
131 |
Hijri |
dd/mm/yy hh:mi:ss:mmmAM |
璇存槑錛?/font>
鈶?nbsp;杈撳叆/杈撳嚭錛?#8220;杈撳叆”琛ㄧず浠庡瓧絎︿覆杞崲涓烘棩鏈熸椂瀛楃涓茬殑鏃ユ湡鏍煎紡錛?#8220;杈撳嚭”鎸囦粠鏃ユ湡杞崲涓哄瓧絎︿覆鏃剁殑鏃ユ湡瀛楃涓叉牸寮忋?/font>
鈶?nbsp;Hijri錛氭槸鍏鋒湁鍑犵鍙樺寲褰㈠紡鐨勬棩鍘嗙郴緇燂紝SQL Server浣跨敤鍏朵腑鐨勭濞佺壒綆楁硶銆?/font>
褰撲粠smalldatetime杞崲涓哄瓧絎︽暟鎹椂錛岀敱浜巗malldatetimer鍙繚瀛樺埌鍒嗛挓鐨勬暟鎹紝鍥犳錛屽浜庡寘鍚鎴栨縐掔殑鏍峰紡錛屽皢鍦ㄧ鎴栨縐掔殑浣嶇疆涓婃樉紺洪浂銆傚綋浠巇atetime鎴杝malldatetime鍊艱繘琛岃漿鎹㈡椂錛屽彲浠ラ氳繃浣跨敤閫傚綋鐨刢har鎴杤archar鏁版嵁綾誨瀷闀垮害鏉ユ埅鏂笉闇瑕佺殑鏃ユ湡閮ㄥ垎銆?/font>
娉ㄦ剰錛?/font>
鍦⊿QL Server涓紝鐢變簬鐩存帴鎻愪緵鐨勬棩鏈熷潎鏄互鏃ユ湡鏍煎紡鐨勫瓧絎︿覆鎻愪緵錛屾墍浠ュ湪浣跨敤CONVERT榪涜鏃ユ湡鏍煎紡杞崲鏃訛紝瑕佸厛鎶婃棩鏈熸牸寮忕殑瀛楃涓茶漿鎹負(fù)鏃ユ湡鍨嬶紝鐒跺悗鎵嶈兘鍒╃敤CONVERT榪涜鏃ユ湡鏍煎紡杞崲錛屽惁鍒欏氨鍙樻垚瀛楃涓茶漿鎹負(fù)瀛楃涓詫紝姝ゆ椂鐨剆tyle閫夐」鏄棤鏁堢殑銆?/font>
榪斿洖綾誨瀷錛氱敱鍙傛暟data_type紜畾銆?/font>
涓嬮潰鏄埄鐢–ONVERT榪涜鏃ユ湡杞崲鐨勭畝鍗曠ず渚嬶細(xì)
/*== 瀛楃杞崲涓烘棩鏈熸椂,Style鐨勪嬌鐢?font face="Courier New"> ==*/
--1. Style=101鏃?font face="Courier New">,琛ㄧず鏃ユ湡瀛楃涓蹭負(fù):mm/dd/yyyy鏍煎紡
SELECT CONVERT(datetime,'11/1/2003',101)
--緇撴灉:2003-11-01 00:00:00.000
--2. Style=101鏃?font face="Courier New">,琛ㄧず鏃ユ湡瀛楃涓蹭負(fù):dd/mm/yyyy鏍煎紡
SELECT CONVERT(datetime,'11/1/2003',103)
--緇撴灉:2003-01-11 00:00:00.000
/*== 鏃ユ湡杞崲涓哄瓧絎︿覆 ==*/
DECLARE @dt datetime
SET @dt='2003-1-11'
--1. Style=101鏃?font face="Courier New">,琛ㄧず灝嗘棩鏈熻漿鎹負(fù):mm/dd/yyyy 鏍煎紡
SELECT CONVERT(varchar,@dt,101)
--緇撴灉:01/11/2003
--2. Style=103鏃?font face="Courier New">,琛ㄧず灝嗘棩鏈熻漿鎹負(fù):dd/mm/yyyy 鏍煎紡
SELECT CONVERT(varchar,@dt,103)
--緇撴灉:11/01/2003
/*== 榪欐槸寰堝浜虹粡甯哥姱鐨勯敊璇?font face="Courier New">,瀵歸潪鏃ユ湡鍨嬭漿鎹嬌鐢ㄦ棩鏈熺殑style鏍峰紡 ==*/
SELECT CONVERT(varchar,'2003-1-11',101)
--緇撴灉:2003-1-11鏁版嵁搴撶粨鏋勭殑鑴氭湰:
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)