try{
String url="jdbc:microsoft:sqlserver://tjj:12345;DataBaseName=myDataBase";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection con=DriverManager.getConnection(url,"Administrator","aaaa");
Statement stmt=con.createStatement();
ResultSet result=stmt.executeQuery("select * from CUSTOMER");
int i=10;
while(result.next()){
g.drawString(result.getString(1),40,60+i);
g.drawString(result.getString(3),100,60+i);
g.drawString(result.getString(6),220,60+i);
i+=20;
}
}catch(SQLException ex){
System.out.println("SQLException:" +ex.getMessage());
}catch(java.lang.Exception ex1){
System.out.println("LangException:" +ex1.getMessage());
}
提示:
[microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
1。是不是url里面的格式有问题?我的SQL服务器名称是TJJ.2。设置ODBC数据源跟这个有什么关系?
3。是不是用windows认证不能正常访问数据库而一定要用混合认证?
4。我用telnet <host> 12345提示连接失败,跟这个有关系吗?怎么样才能连接成功??
String url="jdbc:microsoft:sqlserver://tjj:12345;DataBaseName=myDataBase";
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection con=DriverManager.getConnection(url,"Administrator","aaaa");
Statement stmt=con.createStatement();
ResultSet result=stmt.executeQuery("select * from CUSTOMER");
int i=10;
while(result.next()){
g.drawString(result.getString(1),40,60+i);
g.drawString(result.getString(3),100,60+i);
g.drawString(result.getString(6),220,60+i);
i+=20;
}
}catch(SQLException ex){
System.out.println("SQLException:" +ex.getMessage());
}catch(java.lang.Exception ex1){
System.out.println("LangException:" +ex1.getMessage());
}
提示:
[microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
1。是不是url里面的格式有问题?我的SQL服务器名称是TJJ.2。设置ODBC数据源跟这个有什么关系?
3。是不是用windows认证不能正常访问数据库而一定要用混合认证?
4。我用telnet <host> 12345提示连接失败,跟这个有关系吗?怎么样才能连接成功??
url里面应该带有user password,安装的microsoft jdbc driver目录下有文档参考一下
端口号一定要准确,不是随便填的,
还有你确定你的sqlserver的jdbc驱动包没有问题么
哪位前辈可以详细地讲讲有关数据库连接的问题?
真的很郁闷...
我改过的,原来默认的是1433,连接不起来,所以才改的,
方法getConnection(String url,String user,String password)格式不会错的,我查了联机文档to yangbc(yangbc)
端口不能随便填,那应该怎么填呀??to tangjiehua(只想好好学习。)
数据库默认是不是JDBC安装的目录??---这句话的意思我不懂呀,能不能解释一下
你使用的端口是12345,默认的应该是1433,你到企业管理器里面去确认一下
sqlserver 是windows和sql复合身份验证模式的
sqlserver 打补丁到sp3我一直认为ms的jdbc驱动不太好,至少是对jdbc的支持不太好,经常会有莫名其妙的问题,比如说这个
真的是很郁闷的。
首先:如果你使用本机的话,你可以把tjj改成localhost或者127.0.0.1
还有端口12345,是不是正确!至于会出现Error establishing socket.那就说明你要给你的sql打上sp3补丁!
我在jdk\lib下,tomcat\share\lib下,在项目中都放了mssqlserver.jar文件。都提示这个错误。我以前连过oracle,没象这么麻烦。
这又是什么意思呀?
不要以为有user和password了,url里面就不再需要.事实是url里面一样还要带user,password属性的,前面已经给你提过了.你看看安装目录下的帮助吧.下面我贴其中一点给你:表明了那些是必须要的(带optional就可要可不要的属性)
Table 3-1. SQL Server Connection String Properties Property Description
DatabaseName
OPTIONAL The name of the SQL Server database to which you want to connect.
上面这个不写的话就是指向对应数据库服务器上master数据库HostProcess
OPTIONAL The process ID of the application connecting to SQL Server 2000. The supplied value appears in the "hostprocess" column of the sysprocesses table. NetAddress
OPTIONAL The MAC address of the network interface card of the application connecting to SQL Server 2000. The supplied value appears in the "net_address" column of the sysprocesses table. Password The case-insensitive password used to connect to your SQL Server database. PortNumber
OPTIONAL The TCP port (use for DataSource connections only). The default is 1433. ProgramName
OPTIONAL The name of the application connecting to SQL Server 2000. The supplied value appears in the "program_name" column of the sysprocesses table. SelectMethod SelectMethod={cursor | direct}. Determines whether database cursors are used for Select statements. Performance and behavior of the driver are affected by the SelectMethod setting.
Direct-The direct method sends the complete result set in one request to the driver. It is useful for queries that only produce a small amount of data that you fetch completely. You should avoid using direct when executing queries that produce a large amount of data, as the result set is cached completely on the client and constrains memory. In this mode, each statement requires its own connection to the database. This is accomplished by "cloning" connections. Cloned connections use the same connection properties as the original connection; however, because transactions must occur on a single connection, auto commit mode is required. Due to this, JTA is not supported in direct mode. In addition, some operations, such as updating an insensitive result set, are not supported in direct mode because the driver must create a second statement internally. Exceptions generated due to the creation of cloned statements usually return an error message similar to "Cannot start a cloned connection while in manual transaction mode."
Cursor-When the SelectMethod is set to cursor, a server-side cursor is generated. The rows are fetched from the server in blocks. The JDBC Statement method setFetchSize can be used to control the number of rows that are fetched per request. The cursor method is useful for queries that produce a large amount of data, data that is too large to cache on the client. Performance tests show that the value of setFetchSize has a serious impact on performance when SelectMethod is set to cursor. There is no simple rule for determining the value that you should use. You should experiment with different setFetchSize values to find out which value gives the best performance for your application.
The default is direct. SendStringParameters
AsUnicode SendStringParametersAsUnicode={true | false}. Determines whether string parameters are sent to the SQL Server database in Unicode or in the default character encoding of the database. True means that string parameters are sent to SQL Server in Unicode. False means that they are sent in the default encoding, which can improve performance because the server does not need to convert Unicode characters to the default encoding. You should, however, use default encoding only if the parameter string data that you specify is consistent with the default encoding of the database.
The default is true. ServerName The IP address (use for DataSource connections only). User The case-insensitive user name used to connect to your SQL Server database.