JDBC连SQL SERVER数据库常见问题

--问题1:出现ClassNotFoundException,这个问题是由于你没有把driver类放到你的classpath中,也就是说
你的程序找不到驱动类,包括三个包:msutil.jar,msbase.jar,mssqlserver.jar

--解决:jb:可以在工程属性中加入这三个包,netbean:可以把这三个包copy到某一个 lib下,也就是某个类的公共库中

--问题2:出现[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket,这个错误,不论在什么容器下,凡是出现这种错误的,都可以说不是程序本身的错误,因为大部分人都知道:
jdbc:microsoft:sqlserver://localhost:1433;这样写是对的,但为什么这样写呢
######################################################################
如何用jdbc连接到sqlserver2000(翻译英文原裆)(1)
介绍了连接server2000的方法之一:使用DriverManager.getConnection(...)连接到数据库有两种方法,one is :with a connection url through the jdbc driver manager,another is with a jndi datasource.

我们先说第一种:使用DriverManager.getConnection()方法
第一:你需要设置classpath,它是你得jvm加载类时所要寻找的路径。
当然首先你要有jdbs driver for sqlserver。
设置环境变量:window os:set classpath=.;${your path to save the driver}/msbase.jar;${}/mssqlserver.jar;${}/msutil.jar
第二:注册你的驱动registering the driver
注册驱动告诉你得jdbc管理器去加载什么驱动程序类,
当使用class.forName(),你必须指定驱动类名称,
com.microsoft.jdbc.sqlserver.SQLServerDriver.例如:Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
第三:在注册完一个驱动类后,你必须把你的数据库的物理信息以一个url的形式传给管理器,下面是一个url模版:jdbc:microsoft:sqlserver://server_name:port,其中server2000默认的端口号为:1433.下面是一个例子:
Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://server_name:1433","userName","password");
注:这里的server_name必须是一个ip address,或者一个主机名,你可以使用ping命令来测试你的主机名是否传回一个正确的ip address
下面是 连接url的属性解释:
------------------------------------------------------------------------------
---属性----------------------------------------说明--------------
--DatabaseName(0)                   ------the name of the sql server database to which you want to connect
--HostProcess  (O)                    ------the process ID of the application connecting to SQL Server 2000.The supplied value
                                    ------appears in the "hostprocess" column of the sysprocesses table.
--NetAddress(O)                    ------the MAC address of the network interface card of the application connnecting 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(O)                    ------the tcp 端口号(一般在使用datasource时使用)默认为:1433
--ProgramName(0)                   ------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 wherther 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 results 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 connection to the database.this is accomplished by cloing connections .cloned ------------------------->connections use the same connection properties as the original connection;however,because transactions ------------------->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 ---------->driver 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 selectMode is set to cursor ,a server-side cursor is generated .行被一块的方式提取出来,JDBC语句
-----------------setFetchSize这时候就起作用了,它可以控制每次提取的行数,cursor值在查询结果产生大量数据的时候非常有用,并且也用在
-------------〉jta中,但是setFetchSize具体设置多大,这是没有什么规则的,你必须多多尝试!
###################################
--解决:启动你的sqlserver2000的服务器网络实用工具后,确保你的Tcp/Ip协议已启动,默认的应该都启动了,这是进行通讯的条件一
然后,在选中Tcp/Ip协议后点击属性,就看到了一个默认端口号,这就是你在 getConnection里用到的端口号,你必须把你程序里用
到的端口号,写成这里的值,这样才能解决上面的问题,当然你也可以在这里把端口号给该了,而保持程序不变!1433

--问题3:有些时候也连接上了,但就是不能访问,或者提示说sql语句有错误
--解决:这些都是管理sqlServer2000所需要注意的,或许你没有给这个用户分配足够的权限,或者你的sql语句中用到了sqlserver里保留的
关键字,我就遇到了这样一个问题:我写了个 select * from USER这个语句怎么执行都不对,后来看了分析器,才知道这是一个关键字,你
不可以用它来命名的!

错误:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
A: 这个错误产生的原因一般是当你在一个SQL SERVER的JDBC连接上执行多个STATEMENTS的操作,或者是手动事务状态(AutoCommit=false) 并且使用 direct (SelectMethod=direct) 模式. Direct 模式是默认的模式.
解决办法
当你使用手动事务模式时,必须把SelectMethod 属性的值设置为 Cursor, 或者是确保在你的连接上只有一个STATEMENT操作。
错误:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transaction mode.
A: 这个错误产生的原因一般是当你在一个SQL SERVER的JDBC连接上执行多个STATEMENTS的操作,或者是手动事务状态(AutoCommit=false) 并且使用 direct (SelectMethod=direct) 模式. Direct 模式是默认的模式.
解决办法
当你使用手动事务模式时,必须把SelectMethod 属性的值设置为 Cursor, 或者是确保在你的连接上只有一个STATEMENT操作。
selectmothed问题,偶在hibernate连接SQL Server时候,也遇到了。

selectmethod=cursor是解决办法!

我以前是用Tomcat连SQL SERVER,在JB里开发,连接池如:java:comp/env/jdbc/mywork
现在转为用Weblogic,可在JB里把Server改为Weblogic编译出错了呀
如下:"mywork.war": Spaces in the temporary directory path may cause WebLogic APPC utility to produce fatal compile errors.
请问如何办??"

这不是数据库连接池的错误啊,是环境变量的Administrator变量的TEMP和TMP中设置的路径中有空格,换成没有空格的路径就行了
通过JDBC连接oracle数据库的几个技巧
Java数据库连接(JDBC)API是一系列能够让Java编程人员访问数据库的接口,各个开发商的接口并不完全相同。在使用多年的Oracle公司的JDBC后,我积累了许多技巧,这些技巧能够使我们更好地发挥系统的性能和实现更多的功能。
  1、在客户端软件开发中使用Thin驱动程序

  在开发Java软件方面,Oracle的数据库提供了四种类型的驱动程序,二种用于
应用软件、applets、servlets等客户端软件,另外二种用于数据库中的Java存储过程等服务器端软件。在客户机端软件的开发中,我们可以选择OCI驱动程序或Thin驱动程序。OCI驱动程序利用Java本地化接口(JNI),通过Oracle客户端软件与数据库进行通讯。Thin驱动程序是纯Java驱动程序,它直接与数据库进行通讯。为了获得最高的性能,Oracle建议在客户端软件的开发中使用OCI驱动程序,这似乎是正确的。但我建议使用Thin驱动程序,因为通过多次测试发现,在通常情况下,Thin驱动程序的性能都超过了OCI驱动程序。

  2、关闭自动提交功能,提高系统性能

  在第一次建立与数据库的连接时,在缺省情况下,连接是在自动提交模式下的。为了获得更好的性能,可以通过调用带布尔值false参数的Connection类的setAutoCommit()方法关闭自动提交功能,如下所示:

  conn.setAutoCommit(false);

  值得注意的是,一旦关闭了自动提交功能,我们就需要通过调用Connection类的commit()和rollback()方法来人工的方式对事务进行管理。

  3、在动态SQL或有时间限制的命令中使用Statement对象

  在执行SQL命令时,我们有二种选择:可以使用PreparedStatement对象,也可以使用Statement对象。无论多少次地使用同一个SQL命令,PreparedStatement都只对它解析和编译一次。当使用Statement对象时,每次执行一个SQL命令时,都会对它进行解析和编译。这可能会使你认为,使用PreparedStatement对象比使用Statement对象的速度更快。然而,我进行的测试表明,在客户端软件中,情况并非如此。因此,在有时间限制的SQL操作中,除非成批地处理SQL命令,我们应当考虑使用Statement对象。

  此外,使用Statement对象也使得编写动态SQL命令更加简单,因为我们可以将字符串连接在一起,建立一个有效的SQL命令。因此,我认为,Statement对象可以使动态SQL命令的创建和执行变得更加简单。

  4、利用helper函数对动态SQL命令进行格式化

  在创建使用Statement对象执行的动态SQL命令时,我们需要处理一些格式化方面的问题。例如,如果我们想创建一个将名字O''Reilly插入表中的SQL命令,则必须使用二个相连的“''''”号替换O''Reilly中的“''”号。完成这些工作的最好的方法是创建一个完成替换操作的helper方法,然后在连接字符串心服用公式表达一个SQL命令时,使用创建的helper方法。与此类似的是,我们可以让helper方法接受一个Date型的值,然后让它输出基于Oracle的to_date()函数的字符串表达式。

  5、利用PreparedStatement对象提高数据库的总体效率

  在使用PreparedStatement对象执行SQL命令时,命令被数据库进行解析和编译,然后被放到命令缓冲区。然后,每当执行同一个PreparedStatement对象时,它就会被再解析一次,但不会被再次编译。在缓冲区中可以发现预编译的命令,并且可以重新使用。在有大量用户的企业级应用软件中,经常会重复执行相同的SQL命令,使用PreparedStatement对象带来的编译次数的减少能够提高数据库的总体性能。如果不是在客户端创建、预备、执行PreparedStatement任务需要的时间长于Statement任务,我会建议在除动态SQL命令之外的所有情况下使用PreparedStatement对象。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值