<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

    主站蜘蛛池模板: 国产L精品国产亚洲区久久| 成年在线观看网站免费| 亚洲福利中文字幕在线网址| 国产精品无码亚洲精品2021| 热久久精品免费视频| 亚洲日本天堂在线| 国产精品免费一级在线观看| 亚洲欧美成人综合久久久| 免费国产精品视频| 一区二区三区在线观看免费| 亚洲欧洲中文日韩久久AV乱码| 污污污视频在线免费观看| 亚洲人成图片小说网站| 国内精品久久久久影院免费| 亚洲国产高清视频| 日本zzzzwww大片免费| 亚洲H在线播放在线观看H| 日韩中文字幕在线免费观看| 一级看片免费视频| 久久久久亚洲精品美女| 日本免费一区二区在线观看| 亚洲AV无码AV吞精久久| 伊人久久大香线蕉亚洲| 7m凹凸精品分类大全免费| 亚洲av午夜精品无码专区| 亚洲?v无码国产在丝袜线观看| 不卡视频免费在线观看| 亚洲国产精品日韩在线观看| 啦啦啦手机完整免费高清观看 | 成人一a毛片免费视频| 在线看亚洲十八禁网站| 国产亚洲一区二区三区在线观看| 免费视频成人片在线观看| 亚洲乱码av中文一区二区| 中文字幕亚洲一区| 无码国产精品久久一区免费| 亚洲第一视频在线观看免费| 亚洲男女性高爱潮网站| 亚洲国产综合久久天堂| 4455永久在线观免费看| 人妖系列免费网站观看|