<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 聽風 閱讀(176) 評論(0)  編輯  收藏 所屬分類: JAVA

    主站蜘蛛池模板: 69视频免费在线观看| h在线观看视频免费网站| 日韩在线免费电影| 亚洲AV综合色区无码一二三区| 欧美最猛性xxxxx免费| 亚洲国产成人久久三区| 一二三四影视在线看片免费 | 四虎影视永久在线精品免费| 韩国欧洲一级毛片免费| 极品色天使在线婷婷天堂亚洲| 免费国产美女爽到喷出水来视频| 黄网站色视频免费看无下截| 国产亚洲成人在线播放va| 免费国产叼嘿视频大全网站 | 亚洲永久在线观看| 国产一区在线观看免费| 一区二区在线视频免费观看| 亚洲精品无码av人在线观看 | 亚洲成aⅴ人片在线影院八| 免费99精品国产自在现线| 亚洲暴爽av人人爽日日碰| 亚洲免费日韩无码系列| 久久久久久久久久国产精品免费| 亚洲色av性色在线观无码| 性xxxx视频播放免费| 日韩毛片一区视频免费| 亚洲AV无码精品色午夜在线观看| 18女人毛片水真多免费| 日韩亚洲人成网站| 亚洲av成人无码久久精品 | 韩国日本好看电影免费看| 成在人线av无码免费高潮水| 夜夜亚洲天天久久| 韩国18福利视频免费观看| 国产日韩AV免费无码一区二区| 97久久国产亚洲精品超碰热| 亚洲欧洲国产成人综合在线观看| 日日麻批免费40分钟无码| 亚洲变态另类一区二区三区| 亚洲AV中文无码乱人伦下载| 成人毛片免费观看视频在线 |