import java.sql.*;
/**
* Microsoft SQL Server JDBC test program
*/
public class Test {
public Test() throws Exception {
// Get connection
DriverManager.registerDriver(new
com.microsoft.jdbc.sqlserver.SQLServerDriver());
Connection connection = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://<Host>:1433",<"UID>","<PWD>");
if (connection != null) {
System.out.println();
System.out.println("Successfully connected");
System.out.println();
// Meta data
DatabaseMetaData meta = connection.getMetaData();
System.out.println("\nDriver Information");
System.out.println("Driver Name: "
+ meta.getDriverName());
System.out.println("Driver Version: "
+ meta.getDriverVersion());
System.out.println("\nDatabase Information ");
System.out.println("Database Name: "
+ meta.getDatabaseProductName());
System.out.println("Database Version: "+
meta.getDatabaseProductVersion());
}
} // Test
public static void main (String args[]) throws Exception {
Test test = new Test();
}
}
Compile it
Compile the Java Source: Test.java (all in one line):
$ javac -classpath ".;./lib/mssqlserver.jar;
./lib/msbase.jar;./lib/msutil.jar" Test.java
Be aware that you need access to a javac program on your computer or media. If not, simply specify the full path ahead of javac. The above command is good for Java 2. If your are using for instance Java 1.1.8, add your JDK's classes.zip to the classpath. On Unix systems replace the the semicolons " ; " by colons " : " The forward slashes " / " are fine for both platforms, it's not a must to use backslashes " \ " on Windows.
Run it
Similar to the compilation you may run it like this (again all in one line):
$ java -classpath ".;./lib/mssqlserver.jar;
./lib/msbase.jar;./lib/msutil.jar" Test
The output looks something like this:
Successfully connected
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.194 (Intel X86)
Aug 6 2000 00:57:48
Copyright (c) 1988-2000 Microsoft Corporation
Enterprise Edition on Windows NT 5.0
(Build 2195: Service Pack 2)