把上面的三个jar文件加入到jb的类路径中去。然后使用JDBC连接SQLSERVER数据库 private String jdbcDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; //JDBC驱动程序
private String jdbcUrl = "jdbc:microsoft:sqlserver://数据库服务器所在地址:1433;DatabaseName=数据库名"; //数据库服务器的地址,端口等
private String jdbcUser = "数据库用户名"; //用户名
private String jdbcPassword = "数据库密码"; //密码
然后写个方法GET连接就可以 try
{
Class.forName(jdbcDriver);
}
catch (Exception ex)
{
ex.printStackTrace();
return null;
}
try
{
Connection conn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
return conn;
}
catch (SQLException ex)
{
ex.printStackTrace();
return null;
}
然后得到的连接就可以干一些你需要干的事情了。记得完事后要close哦
private String jdbcUrl = "jdbc:microsoft:sqlserver://数据库服务器所在地址:1433;DatabaseName=数据库名"; //数据库服务器的地址,端口等
private String jdbcUser = "数据库用户名"; //用户名
private String jdbcPassword = "数据库密码"; //密码
然后写个方法GET连接就可以 try
{
Class.forName(jdbcDriver);
}
catch (Exception ex)
{
ex.printStackTrace();
return null;
}
try
{
Connection conn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
return conn;
}
catch (SQLException ex)
{
ex.printStackTrace();
return null;
}
然后得到的连接就可以干一些你需要干的事情了。记得完事后要close哦
第一:你需要设置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具体设置多大,这是没有什么规则的,你必须多多尝试!
在微软站点就有这个驱动程序:
Window操作系统
http://www.uncj.com/upload/files/ms_jdbc_setup.exe
http://download.microsoft.com/download/SQLSVR2000/jdbc/2000/NT45XP/EN-US/setup.exeUnix操作系统 Mssqlserver.tar
http://download.microsoft.com/download/SQLSVR2000/jdbc/2000/UNIX/EN-US/mssqlserver.tar二、安装JDBC 执行ms_jdbc_setup.exe可执行文件,一切只需要点击下一步,至到出现finish按钮,完成安装。 注:
(1) ms_JDBC_setup默认安装路径为:
c:\Program Files\Microsoft SQL Server 2000 Driver for JDBC
(2)此版(Version 2.2.0022)本仅支持
Microsoft SQL Server 2000 Driver for JDBC
(3)安装目录\lib\下的三个jar文件即是我们要的JDBC驱动核心
msbase.jar
mssqlserver.jar
msutil.jar三、将以上(3)中指的三个jar文件加入到环境变量中去
classpath:
d:\webserver\lib\msbase.jar
d:\webserver\lib\mssqlserver.jar
d:\webserver\lib\msutil.jar
注:我建议将以上三个文件拷贝至你的jvm机所在的lib或class目录下。此时我是将它拷贝至我的jvm目录下的lib下。
四、测试public class Test{
public Test(){}
public static void main(String args[]){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn =DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=maxwell");
Statement stmt=conn.createStatement();
String sql="select * from employee";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println("TestName:"+rs.getString("name"));
}
rs.close();
stmt.close();
conn.close();
} catch(Exception ex) { System.err.println(ex.getMessage()); }
}} 五、注意事项 1。要保证你的Sql服务是启动状态
2.在Sql 的管理器中能够使用sa或其它用户正常登录;(有些人在安装sql时用了nt用户管理模式,这里可能会有一定问题)
3.第一次配置好环境变量最好能重启一下电脑
4.注意JVM和DataBase的启动顺序:
先启动DataBase,再启动JVM机;
注:在停止Web服务器后,再重启动,中间最好能有10秒以上的间隔.
5.注意操作系统的网络连通性
A.启动了Tcp/IP服务
B.配置了相关IP地址,有些人机器可能用的是自动分配IP或有配置IP时,但网络不通可能也找不到IP
6.有些机器在调试明最好能将(四)中的连接地址localhost改成数据库服务器的IP,这也是要注意5的原因。