feirou520 发表于 2013-1-14 00:35:21

jtds 连接Sql Server 2005数据库

<div class="articalContent">使用java连接sqlserver 2005搞了好久,终于连接上了,记录如下:
设置SQLEXPRESS服务器:a.打开SQL ServerConfiguration Manager ->SQLEXPRESS的协议 -> TCP/IP
b.右键单击启动TCP/IP 
c.双击进入属性,把IP地址中的IP all中的TCP端口设置为1433
d.重新启动SQL Server2005服务中的SQLEXPRESS服务器
e.关闭SQL Server Configuration Manager
f.起动 Sql Server(SqlExpress)服务 。
一定不要忘记了,启动 Sql server browser服务。我用.net连接是不用启动的,java的就是不行,非启动不可.
就是这个异常了:org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannotcreate PoolableConnectionFactory (Unable to get information fromSQL Server: localhost.)


java 连接:
连接串:jdbc:jtds:sqlserver://localhost:1433;instance=SQLEXPRESS;DatabaseName=test
或jdbc:jtds:sqlserver://localhost:1433/test;instance=SQLEXPRESS
driverClassName="net.sourceforge.jtds.jdbc.Driver"

查找的jtds原文如下:http://jtds.sourceforge.net/faq.html

What is the URL format used by jTDS?
The URL format for jTDS is:
    jdbc:jtds:<server_type>://<server>[:<port>][/<database>][;<property>=<value>[;...]]where <server_type>is one of either 'sqlserver' or 'sybase'(their meaning is quite obvious),<port> is the port thedatabase server is listening to (default is 1433 for SQL Server and7100 for Sybase) and<database> is thedatabase name -- JDBC term: catalog -- (if not specified, theuser's default database is used). The set of properties supportedby jTDS is:
appName (default - "jTDS")Application name. No practical use, it's displayed byEnterprise Manager or Profiler associated with the connection.batchSize (default - 0 for SQLServer; 1000 for Sybase)Controls how many statements are sent to the server in a batch.The actual batch is broken up into pieces this large that are sentseparately. The reason for this is to avoid Sybase "hangs" causedby running out of space with very large batches. The problemdoesn't seem to occur with SQL Server, hence the default limit of 0(unlimited) in this case.bindAddress (default - determined by the Javaimplementation; requires Java 1.4 or later)Specifies the local IP address to bind to for outgoing TCP/IPconnections to the database. Useful for multi-homed systems (thosewith more than one external IP address) where the default IPaddress picked by Java will not connect to the database. Currentlyhas no effect when using named pipes to connect to a database (seenamedPipe). Since after jTDS-1.2.bufferDir (default -System.getProperty("java.io.tmpdir"))Controls the destination where data is buffered to disk.
See also bufferMaxMemory andbufferMinPackets.bufferMaxMemory (default - 1024)Controls the global buffer memory limit for all connections (inkilobytes). When the amount of buffered server response packetsreaches this limit additional packets are buffered to disk; thereis however one exception: each Statement gets tobuffer at least<bufferMinPackets> tomemory before this limit is enforced. This means that this limitcan and will usually be exceeded.
Server responses are buffered to disk only when a request is madeon a Statement while another Statementbelonging to the same Connection still hasn't processed all itsresults. These situations can be avoided in most cases by settingthe useCursors property, but this will also affectperformance.
See also bufferMinPackets.bufferMinPackets (default - 8)Controls the minimum number of packets per statement to bufferto memory. Each Statement will buffer at least thismany packets before being forced to use a temporary file if the<bufferMaxMemory> isreached, to ensure good performance even when oneStatement caches a very large amount of data.
Server responses are buffered to disk only when a request is madeon a Statement while another Statementbelonging to the same Connection still hasn't processed all itsresults. These situations can be avoided in most cases by settingthe useCursors property, but this will also affectperformance.
See also bufferMaxMemory.cacheMetaData (default - false)When used with prepareSQL=3, setting this property totrue will cause the driver to cache column meta datafor SELECT statements. Caching the meta data will reduce theprocessing overhead when reusing statements that return smallresult sets that have many columns but may lead to unexpectederrors if the database schema changes after the statement has beenprepared. Use with care. Only applicable to SQL Server (there is noprepareSQL=3 mode for Sybase).charset (default - the character set the serverwas installed with)Very important setting, determines the byte value to charactermapping forCHAR/VARCHAR/TEXT values.Applies for characters from the extended set (codes 128-255). ForNCHAR/NVARCHAR/NTEXT valuesdoesn't have any effect since these are stored using Unicode.domainSpecifies the Windows domain to authenticate in. If present andthe user name and password are provided, jTDS uses Windows (NTLM)authentication instead of the usual SQL Server authentication (i.e.the user and password provided are the domain user and password).This allows non-Windows clients to log in to servers which are onlyconfigured to accept Windoes authentication.
If the domain parameter is present but no user nameand password are provided, jTDS uses its native Single-Sign-Onlibrary and logs in with the logged Windows user's credentials (forthis to work one would obviously need to be on Windows, logged intoa domain, and also have the SSO library installed -- consultREADME.SSO in the distribution on how to do this).instanceNamed instance to connect to. SQL Server can run multipleso-called "named instances" (i.e. different server instances,running on different TCP ports) on the same machine. When usingMicrosoft tools, selecting one of these instances is made by using"<host_name>\<instance_name>"instead of the usual "<host_name>".With jTDS you will have to split the two and use the instance nameas a property.lastUpdateCount (default - true)If true only the last update count will bereturned by executeUpdate(). This is useful in caseyou are updating or inserting into tables that have triggers (suchas replicated tables); there's no way to make the differencebetween an update count returned by a trigger and the actual updatecount but the actual update count is always the last as thetriggers execute first. If false all update counts arereturned; use getMoreResults() to loop throughthem.lobBuffer (default - 32768)The amount of LOB data to buffer in memory before caching todisk. The value is in bytes for Blob data and charsfor Clob data.loginTimeout (default - 0 for TCP/IPconnections or 20 for named pipe connections)The amount of time to wait (in seconds) for a successfulconnection before timing out.
If a TCP/IP connection is used to connect to the database and Java1.4 or newer is being used, the loginTimeout parameteris used to set the initial connection timeout when initiallyopening a new socket. A value of zero (the default) causes theconnection to wait indefinitely, e.g.,until a connection isestablished or an error occurs. See alsosocketTimeout.
If a named pipe connection is used (namedPipe istrue) and loginTimeout is greater thanzero, the value of loginTimeout is used for the lengthof the retry period when "All pipe instances are busy" errormessages are received while attempting to connect to the server. IfloginTimeout is zero (the default), a value of 20seconds is used for the named pipe retry period.macAddress (default -"000000000000")Network interface card MAC address. It's displayed byEnterprise Manager or Profiler associated with the connection andis needed to resolve some issues regarding the number of clientsallowed by the SQL Server license. The MAC address cannot bedetermined automatically from Java (i.e. without using native code)so you'll have to specify it yourself if you need it.maxStatements (default - 500)The number of statement prepares each connection should cache.A value of 0 will disable statement caching. A valueof Integer.MAX_VALUE (2147483647) willenable fast caching (uses less memory and has no overheadassociated with removing statements); the cache will never releaseany cached statements, so although experience has shown that thisis usually not a problem with most applications, use withcare.namedPipe (default - false)When set to true, named pipe communication is usedto connect to the database instead of TCP/IP sockets. When theos.name system property starts with "windows"(case-insensitive), named pipes (both local and remote) areaccessed through the Windows filesystem by opening aRandomAccessFile to the path. When the SQL Server andthe client are on the same machine, a named pipe will usually havebetter performance than TCP/IP sockets since the network layer iseliminated. Otherwise the JCIFSlibrary is used. JCIFS provides a pure Java named pipeimplementation and uses NTLM authentication, so thedomain parameter is required.
This feature supports the instance parameter (whichchanges the named pipe URL), but it does not currently support thenamed pipe at a location other than /sql/query on theserver. The port parameter is ignored if set.packetSize (default - 4096 for TDS7.0/8.0; 512 for TDS 4.2/5.0)The network packet size (a multiple of 512).password (required)Password to use for login. When usinggetConnection(String url, String user, Stringpassword) it's not required to set this property as it ispassed as parameter, but you will have to set it when usinggetConnection(String url, Properties info) orJtdsDataSource.prepareSQL (default - 3 for SQLServer, 1 for Sybase)This parameter specifies the mechanism used for PreparedStatements.
页: [1]
查看完整版本: jtds 连接Sql Server 2005数据库