hello world

随笔 - 2, 文章 - 63, 评论 - 0, 引用 - 0
数据加载中……

数据库连接

1.  加载一个对应数据库的JDBC驱动
  在建立到一个数据库的连接之前,必须先加载这个数据库的JDBC驱动程序,加载之后此driver会自动注册到JDBC驱动列表中。加载一个

JDBC驱动有两种方法。
  a)  在命令行方式下指定驱动器或者用冒号分割驱动器列表:
  具体命令如下:
      C:\>java –Djdbc.drivers = com.company1.Driver:com.company2.Driver youProject
  b)第二种方法,在程序中调用Class.forName()方法。推荐使用。。。。
              try
  {
     String driverName = “com.imaginary.sql.msql.MsqlDriver”;
     Class.forName(driverName).newInstance();
  }
              Catch(ClassNotFoundException e1)
                 {
            //catch could not find database driver exception.
    }
  2.连接到数据库。  
  根据您后台待连接的数据库不同,而有小小的差别。
  a)       连接到Oracle数据库。
  Connection connection = null ;
  try
  {
    //load the jdbc driver ;
    String driverName = “oracle.jdbc.driver.OracleDriver”;
    Class.forName(driverName).newInstance();
   //create a connection to the database;
    String serverName = “127.0.0.1”;
    String serverPort = “1521”;
    String serverID = “datebase1”
    String userName = “hello”;
    String userPsw = “world”;
    String url = “jdbc:oracle.thin:@” + serverName + “:” + serverPort + “:” + serverID ;
    Connection = DriverManager.getConnection(url , userName , userPsw);
  }
  catch(ClassNotFoundException e1)
  {
    //catch could not find database driver exception.
  }
  catch(SQLException e2)
  {
    //catch could not connect to the database exception.
  }
  b)      连接到一个SQL Server数据库。
     Connection connection = null ;
  try
  {
    //load the jdbc driver ;
    String driverName = “com.microsoft.jdbc.sqlserver.SQLServerDriver”;
    Class.forName(driverName).newInstance();
     //create a connection to the database;
    String serverName = “127.0.0.1”;
    String serverPort = “1433”;
    String serverID =  serverName + serverPort ;
    String userName = “hello”;
    String userPsw = “world”;
    String url = “jdbc:JSQLConnect ://” + serverID ;
    Connection = DriverManager.getConnection(url , userName , userPsw);
  }
  catch(ClassNotFoundException e1)
  {
    //catch could not find database driver exception.
  }
  catch(SQLException e2)
  {
    //catch could not connect to the database exception.
  }
  c)      连接到一个MySQL数据库上。。。。
          Connection connection = null ;
  try
  {
    //load the jdbc driver ;
    String driverName = “org.gjt.mm.mysql.Driver”;
    Class.forName(driverName).newInstance();
      //create a connection to the database;
    String serverName = “127.0.0.1”;
    String serverID =  “database”;
    String userName = “hello”;
    String userPsw = “world”;
    String url = “jdbc:mysql ://” + serverName + “/” + serverID ;
    Connection = DriverManager.getConnection(url , userName , userPsw);
  }
  catch(ClassNotFoundException e1)
  {
    //catch could not find database driver exception.
  }
  catch(SQLException e2)
  {
    //catch could not connect to the database exception.
  }

  综合上面的三种数据库连接方式 , 其实大同小异。由于访问不同的数据库和所使用的数据库驱动程序不同,所以导致代码表面上有小小

不同,但透过表面看来,内部都是

  1.  加载一个特定的数据库JDBC驱动。
  2.  连接到一个数据库。
  3.  之后,就可以对一个特定的数据库进行特定的操作了。

 


1、Oracle8/8i/9i数据库(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);

2、DB2数据库
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample为你的数据库名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);

3、Sql Server7.0/2000数据库
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);

4、Sybase数据库
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";
//myDB为你的数据库名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);

5、Informix数据库
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url =
"jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword";
//myDB为数据库名
Connection conn= DriverManager.getConnection(url);

6、MySQL数据库
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//myDB为数据库名
Connection conn= DriverManager.getConnection(url);

7、PostgreSQL数据库
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB"
//myDB为数据库名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);

posted on 2008-03-28 13:30 听风 阅读(161) 评论(0)  编辑  收藏 所属分类: JAVA


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


网站导航: