日期:2014-05-16 浏览次数:20630 次
连接到SQL 2000数据库,首先当然是要下载JDBC驱动程序,最好去微软官方网站下载,然后将下载到的三个JAR包放入你的WEB应用的WEB-INF/lib/下。接下来编写程序进行测试:
/***********************************************
?/*
?/*DBTest.java
?/*
?/******************************************* */
?
?import java.sql.*;
?
?public class DBTest
?{
?
? Connection con;
? Statement? sta;
? ResultSet? rs;
?
? String driver;
? String url;
? String user;
? String pwd;
? public DBTest()
? {
????? driver = "com.microsoft.jdbc.
?? sqlserver.SQLServerDriver";;
????? url??? = "jdbc:microsoft:sqlserver:
?? //localhost:1433;DatabaseName =test";
????? //test为数据库名
????? user?? = "sa";
????? pwd??? = "sa";
????? //请更改为你相应的用户和密码
????? init();
? }
? public void init()
? {
?? try{
??? Class.forName(driver);
??? System.out.println("driver is ok");
??? con = DriverManager.
?getConnection(url,user,pwd);
??? System.out.println("conection is ok");
?????? sta = con.createStatement();
?????? rs? = sta.executeQuery
??? ("select * from room");
?????? while(rs.next())
??????? System.out.println
? (rs.getInt("roomNum"));
?? }catch(Exception e)
??? {
???? e.printStackTrace();
??? }
? }
?
? public static void main(String args[])
? //自己替换[]
? {
?? new DBTest();
? }
?}