Ethip 发表于 2008-1-22 20:21

Java连接SQL Server 2000的两种方法

[size=2][/size]
[size=2]提示:More details in [/size][url=http://bbs.bccn.net/thread-197935-1-1.html][size=2]http://bbs.bccn.net/thread-197935-1-1.html[/size][/url]
[size=2][/size]
[size=2]方法一、Jdbc-Odbc桥 连接[/size]
[size=2]方法二、JDBC Driver连接(或称非Jdbc-Odbc桥连接)[/size]
[size=2][/size]
[size=2]驱动加载和连接测试实例[/size]
[size=2][em01] /*Jdbc-Odbc桥 连接*/[em01] [/size]
[size=2]package connectionTest;[/size]
[size=2]import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;[/size]
[size=2]public class JdbcOdbcBridgeConnection {
public static void main(String[] args) {
  // jdbc-odbc桥 连接方式
  String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
  String c ;
  Connection dbConn;
  try {//测试驱动程序加载是否成功
   Class.forName( driverName );
   System.out.println("Driver load Successful ");
  }
  catch(java.lang.ClassNotFoundException e ){
   System.err.print("Class Not Found Exception:");
   //getMessage()的功能:Returns the detail message string of this throwable.
   System.err.println(e.getMessage() );
  }
  try{//测试数据库连接是否成功
   dbConn = DriverManager.getConnection(connection);
   System.out.println("Connection Successful!");
   dbConn.close();
  }
  catch(SQLException e) {
   System.err.println("SQLException: " + e.getMessage());
  }
}
} [/size]
[size=2][/size]

[size=2][em01] /*JDBC Driver连接*/[em01] [/size]
[size=2]package connectionTest;[/size]
[size=2]import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;[/size]
[size=2][/size]
[size=2]public class JdbcDriverConnection {
public static void main(String[] args) {
  // JDBC Driver 连接方式
  String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
  String dbURL = "jdbc:microsoft:sqlserver://127.0.0.1:1433; DatabaseName=pubs";
  String userName = "ethip";
  String userPwd = "ethip";
  Connection dbConn;
  try {//测试驱动程序加载是否成功
   Class.forName( driverName );
   System.out.println("Driver load Successful ");
  }
  catch(java.lang.ClassNotFoundException e ){
   System.err.print("Class Not Found Exception:");
   //getMessage()的功能:Returns the detail message string of this throwable.
   System.err.println(e.getMessage() );
  }
  try{//测试数据库连接是否成功
   dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
   System.out.println("Connection Successful!");
   dbConn.close();
  }
  catch(SQLException e) {
   System.err.println("SQLException: " + e.getMessage());
  }
  [/size]
[size=2] }
} [/size]
[size=2][/size]
[size=2][/size]


页: [1]

编程论坛