Lm th? no ?: B?t ?u v?i Microsoft JDBC

D?ch tiu ? D?ch tiu ?
ID c?a bi: 313100 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t ny m t? cch k?t n?i v?i SQL Server 2000 b?ng cch s? d?ng tr?nh i?u khi?n Microsoft SQL Server 2000 cho JDBC.


LU ?: ? h?ng d?n ci ?t Microsoft SQL Server 2000 Driver cho JDBC, xem cc Microsoft SQL Server 2000 Driver cho h?ng d?n ci ?t JDBC.

Sau khi b?n ci ?t tr?nh i?u khi?n Microsoft SQL Server 2000 cho JDBC, b?n c th? k?t n?i t? chng tr?nh c?a b?n ? c s? d? li?u c?a b?n theo hai cch: v?i m?t k?t n?i URL, ho?c v?i m?t ngu?n d? li?u JNDI. Bi vi?t ny m t? lm th? no ? c?u h?nh v th? nghi?m k?t n?i c s? d? li?u c?a b?n b?ng cch s? d?ng m?t URL k?t n?i.

M?t chi?u k?t n?i c s? d? li?u l do thng qua qu?n l? tr?nh i?u khi?n JDBC b?ng cch s? d?ng cc IDatabaseConnection phng php c?a cc DriverManager l?p h?c. Cch n gi?n nh?t c?a vi?c s? d?ng phng php ny ph?i m?t m?t tham s? chu?i c ch?a m?t URL, tn ng?i dng v m?t kh?u. Cc o?n trong bi vi?t ny m t? cch n?p tr?nh i?u khi?n Microsoft SQL Server 2000 cho JDBC t? chng tr?nh JDBC c?a b?n.

? ci CLASSPATH bi?n

Tr?nh i?u khi?n Microsoft SQL Server 2000 JDBC .jar t?p ph?i ?c li?t k trong bi?n CLASSPATH c?a b?n. Bi?n CLASSPATH l chu?i t?m ki?m my ?o Java (JVM) s? d?ng ? xc ?nh v? tr cc tr?nh i?u khi?n JDBC trn my tnh c?a b?n. N?u tr?nh i?u khi?n khng ?c li?t k trong bi?n CLASSPATH c?a b?n, b?n nh?n ?c thng bo l?i sau khi b?n c? g?ng t?i cc tr?nh i?u khi?n:
Java.lang.ClassNotFoundException: com/microsoft/jdbc/sqlserver/SQLServerDriver
Thi?t l?p c?a b?n bi?n CLASSPATH h? th?ng ? bao g?m cc m?c sau y:
  • \?ng d?n ci ?t c?a b?n\Lib\Msbase.jar
  • \?ng d?n ci ?t c?a b?n\Lib\Msutil.jar
  • \?ng d?n ci ?t c?a b?n\Lib\Mssqlserver.jar
y l m?t v d? v? m?t bi?n CLASSPATH c?u h?nh:
CLASSPATH =.;c: Program Files Microsoft SQL Server 2000 Driver cho JDBC\lib\msbase.jar;c:\program Microsoft SQL Server 2000 Driver cho JDBC\lib\msutil.jar;c:\program Microsoft SQL Server 2000 Driver cho JDBC\lib\mssqlserver.jar

ng k? tr?nh i?u khi?n

ng k? tr?nh i?u khi?n ch? th? qu?n l? tr?nh i?u khi?n JDBC tr?nh i?u khi?n no ? t?i. Khi b?n t?i m?t tr?nh i?u khi?n b?ng cch s? d?ng cc Class.forName ch?c nng, b?n ph?i ch? ?nh tn tr?nh i?u khi?n. y l tn tr?nh i?u khi?n cho Microsoft SQL Server 2000 Driver cho JDBC:
com.Microsoft.jdbc.sqlserver.SQLServerDriver
M?u m? sau y ch?ng t? lm th? no ? ng k? tr?nh i?u khi?n:
Driver d = (Driver)Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
				

? v?t qua URL k?t n?i

B?n ph?i v?t qua thng tin k?t n?i c s? d? li?u c?a b?n ? d?ng m?t URL k?t n?i. y l m?t m?u URL cho Microsoft SQL Server 2000 Driver cho JDBC. Thay th? cc gi tr? cho c s? d? li?u c?a b?n:
jdbc:Microsoft:sqlserver: / / servername:1433
M?u m? sau y ch?ng t? lm th? no ? ch? ?nh m?t URL k?t n?i:
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433", "userName", "password");
				
Cc my ch? tn gi tr? c th? l m?t ?a ch? IP ho?c tn my ch? lu tr? (gi? s? r?ng m?ng c?a b?n gi?i quy?t my ch? tn ? ?a ch? IP). B?n c th? th? nghi?m ny b?i ping tn my ch? v xc minh r?ng b?n nh?n ?c m?t th tr? l?i v?i ?a ch? IP chnh xc.

Gi tr? s? theo tn my ch? l s? hi?u c?ng m c s? d? li?u l?ng nghe. Cc gi tr? ?c li?t k tr?c trong bi vi?t ny l gi tr? m?c ?nh m?u. H?y ch?c ch?n ? thay th? s? hi?u c?ng c s? d? li?u c?a b?n ang s? d?ng.

?i v?i m?t danh sch ?y ? cc k?t n?i URL tham s?, xem Microsoft SQL Server 2000 Driver cho JDBC tr? gip HTML, ho?c xem cc H?ng d?n tr?c tuy?n. H?y xem ph?n "Thu?c tnh k?t n?i chu?i".

M?u m? ? ki?m tra k?t n?i

M?u m? sau y c? g?ng ? k?t n?i v?i c s? d? li?u v hi?n th? tn c s? d? li?u, phin b?n, v catalog c s?n. Thay th? cc thu?c tnh h? ph?c v? v?i cc gi tr? cho my ch? c?a b?n:
import java.*;
public class Connect{
     private java.sql.Connection  con = null;
     private final String url = "jdbc:microsoft:sqlserver://";
     private final String serverName= "localhost";
     private final String portNumber = "1433";
     private final String databaseName= "pubs";
     private final String userName = "user";
     private final String password = "password";
     // Informs the driver to use server a side-cursor, 
     // which permits more than one active statement 
     // on a connection.
     private final String selectMethod = "cursor"; 
     
     // Constructor
     public Connect(){}
     
     private String getConnectionUrl(){
          return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
     }
     
     private java.sql.Connection getConnection(){
          try{
               Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
               con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
               if(con!=null) System.out.println("Connection Successful!");
          }catch(Exception e){
               e.printStackTrace();
               System.out.println("Error Trace in getConnection() : " + e.getMessage());
         }
          return con;
      }

     /*
          Display the driver properties, database details 
     */ 

     public void displayDbProperties(){
          java.sql.DatabaseMetaData dm = null;
          java.sql.ResultSet rs = null;
          try{
               con= this.getConnection();
               if(con!=null){
                    dm = con.getMetaData();
                    System.out.println("Driver Information");
                    System.out.println("\tDriver Name: "+ dm.getDriverName());
                    System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
                    System.out.println("\nDatabase Information ");
                    System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
                    System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
                    System.out.println("Avalilable Catalogs ");
                    rs = dm.getCatalogs();
                    while(rs.next()){
                         System.out.println("\tcatalog: "+ rs.getString(1));
                    } 
                    rs.close();
                    rs = null;
                    closeConnection();
               }else System.out.println("Error: No active Connection");
          }catch(Exception e){
               e.printStackTrace();
          }
          dm=null;
     }     
     
     private void closeConnection(){
          try{
               if(con!=null)
                    con.close();
               con=null;
          }catch(Exception e){
               e.printStackTrace();
          }
     }
     public static void main(String[] args) throws Exception
       {
          Connect myDbTest = new Connect();
          myDbTest.displayDbProperties();
       }
}

				
N?u m? ny thnh cng, ?u ra l tng t? nh sau:
Connection Successful!
Driver Information
        Driver Name: SQLServer
        Driver Version: 2.2.0022

Database Information
        Database Name: Microsoft SQL Server
        Database Version: Microsoft SQL Server  2000 - 8.00.384 (Intel X86)
        May 23 2001 00:02:52
        Copyright (c) 1988-2000 Microsoft Corporation
        Desktop Engine on Windows NT 5.1 (Build 2600: )

Avalilable Catalogs
        catalog: master
        catalog: msdb
        catalog: pubs
        catalog: tempdb
					

C b?n kh?c ph?c s? c? k?t n?i

y l cc thng bo l?i ph? bi?n c th? x?y ra khi b?n c? g?ng k?t n?i v?i my ch? SQL c?a b?n:
Java.SQL.SQLException: [Microsoft] [SQLServer 2000 Driver cho JDBC] [SQLServer] ng nh?p th?t b?i cho ng?i s? d?ng 'ng?i dng'. L? do: Khng lin quan v?i m?t k?t n?i my ch? SQL ng tin c?y.
Thng bo l?i ny x?y ra n?u ch? ? xc th?c SQL Server 2000 ?c thi?t l?p ? ch? ? xc th?c c?a Windows. Tr?nh i?u khi?n Microsoft SQL Server 2000 cho JDBC khng h? tr? k?t n?i b?ng cch s? d?ng Windows NT xc th?c. B?n ph?i thi?t l?p phng th?c xc th?c my ch? SQL c?a b?n sang ch? ? h?n h?p, gi?y php SQL Server xc th?c v xc th?c c?a Windows.
Java.SQL.SQLException: [Microsoft] [SQLServer 2000 Driver cho JDBC] Phin b?n ny c?a tr?nh i?u khi?n JDBC ch? h? tr? Microsoft SQL Server 2000. B?n c th? nng c?p ln SQL Server 2000 ho?c c th? xc ?nh v? tr m?t phin b?n c?a tr?nh i?u khi?n.
Thng bo l?i ny x?y ra b?n th? k?t n?i ?n m?t phin b?n SQL Server s?m hn so v?i SQL Server 2000. Tr?nh i?u khi?n Microsoft SQL Server 2000 cho JDBC h? tr? k?t n?i ch? v?i SQL Server 2000.

Thu?c tnh

ID c?a bi: 313100 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft SQL Server 2000 64-bit Edition
  • Microsoft SQL Server 2000 Standard Edition
T? kha:
kbsqlsetup kbhowtomaster kbmt KB313100 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:313100

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com