Java Sky of Zhao
受挫一次,对生活的理解加深一层;失误一次对人生的感悟增添一阶;不幸一次,对世界的认识成熟一级;磨难一次,对成功的内涵透彻一遍!
posts - 5,comments - 4,trackbacks - 0

JDBC 连接大全 ( SQL Server, ORACLE, MySQL, DB2 )

         引用请注明出处 :http//www.blogjava.net/SINOJAVA

 

1.  JDBC 连接 SQL Server                                     

 

 

下载 SQL Server JDBC 驱动

 

设置 CLASSPATH=.;C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar;C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar;C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar

 

例子:

import java.sql.*;

public class ConSqlserver{

    Connection con;

    Statement st;

    ResultSet rs; 

    public ConSqlserver(){ 

        try{

               String name="com.microsoft.jdbc.sqlserver.SQLServerDriver";

               String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=northwind;User=user;Password=password";

 

               Class.forName(name);

               con=DriverManager.getConnection(url,"user","password");

               System.out.println(" 连接成功 !");

               con.close();          

         }catch(Exception e){

               e.printStackTrace();

         }

    }

 

    public static void main(String args[])

   

    {   

        ConSqlserver cs=new ConSqlserver(); 

    }

 

}

 

2.JDBC 连接 ORACLE- -                                      

 

 

oracle 自己带 jdbc 驱动,具体位置在 C:\oracleora92\jdbc\lib\classes12.jar

 

设置 CLASSPATH=.;C:\oracleora92\jdbc\lib\classes12.jar

 

例子:

import java.sql.*;

public class ConOracle{

 Connection con;

 Statement st;

 ResultSet rs; 

 public ConOracle(){ 

  try{

          String name="oracle.jdbc.driver.OracleDriver";

          String url="jdbc:oracle:thin@localhost:1521:database";

          Class.forName(name);

          con=DriverManager.getConnection(url,"user","password");

           System.out.println(" 连接成功 !");

          con.close();          

  }catch(Exception e){

   e.printStackTrace();

  }

 public static void main(String args[]){   

  ConOracle co=new ConOracle(); 

 }

}

 

3.JDBC 连接 MySQL                                    

 

 

下载 MySQLJDBC 驱动

 

设置 CLASSPATH=.;C:\Program Files\MySQL\mysql-connector-java-3.2.0-alpha\mysql-connector

-java-3.2.0-alpha\mysql-connector-java-3.2.0-alpha-bin.jar

 

例子:

import java.sql.*;

public class ConMysql{

 Connection con;

 Statement st;

 ResultSet rs; 

 public ConMysql(){ 

  try{

          String name="com.mysql.jdbc.Driver";

          String url="jdbc:mysql://localhost/database?user=user&password=password"; 

          Class.forName(name);

          con=DriverManager.getConnection(url);

          System.out.println(" 连接成功 !");

          con.close();          

  }catch(Exception e){

   e.printStackTrace();

  }

 }

 

 public static void main(String args[]){   

  ConMysql cms=new ConMysql(); 

 }

}

 

4. JDBC 连接 DB2- -                                      

 

 

DB2 自己带 jdbc 驱动,具体位置在 C:\Program Files\IBM\SQLLIB\java\db2jcc.jar

C:\Program Files\IBM\SQLLIB\java\db2jcc_license_cu.jar

C:\Program Files\IBM\SQLLIB\java\db2jcc_license_cisuz.jar

 

设置 CLASSPATH=.;C:\ProgramFiles\IBM\SQLLIB\java\db2jcc.jar;

C:\ProgramFiles\IBM\SQLLIB\java\db2jcc_license_cu.jar;

C:\ProgramFiles\IBM\SQLLIB\java\db2jcc_license_cisuz.jar

 

注意:在使用 jdbc 连接 db2 时,一定要用 ibm 自己的 jdk

否则执行会出现错误,可能是 ibm jdbc 驱动和 sun jdk 不兼容的问题

      具体位置在 C:\Program Files\IBM\SQLLIB\java\jdk

 

例子:

import java.sql.*;

public class ConDB2{

 Connection con;

 Statement st;

 ResultSet rs; 

 public ConDB2(){ 

  try{

          String name="com.ibm.db2.jcc.DB2Driver";

          String url="jdbc:db2://localhost:50000/database";

         Class.forName(name);

          con=DriverManager.getConnection(url,"user","password");

          System.out.println(" 连接成功 !");

          con.close();          

  }catch(Exception e){

   e.printStackTrace();

  }

 }

 

 public static void main(String args[]){   

  ConDB2 cd=new ConDB2(); 

 }

}

 

posted on 2006-04-10 07:11 SINOJAVA 阅读(771) 评论(0)  编辑  收藏 所属分类: Java VS DataBase

只有注册用户登录后才能发表评论。


网站导航: