注冊驅動MySQL的驅動程序


1.將驅動程序文件添加到應用項目

      將驅動程序mysql-connector-Java-5.1.6-bin,復制到web應用程序的web-INF\lib下,web應用程序就可以通過JDBC接口訪問MySQL數據庫了。

2.加載注冊指定的數據庫驅動程序

     對於Mysql數據庫,其驅動程序加載格式:

     class.forName("com.mysql.jdbc.Driver");

JDBC連接數據庫創建連接對象

     首先形成‘’連接符號字(url)‘’,然后利用“連接符號字”實現連接並創建連接對象

1.數據庫連接的URL

      string  url 1="jdbc:mysql:";

      string  ur2="?user=root&password=密碼";

      string  ur3="&useUnicode=true&characterEncoding=UTF-8";

      string  url=url1+url2+url3;

2.利用連接符號字實現連接,獲取連接對象

     A  static  Connection getConnection(String url)

     B  static   Connection getConnection(String url,properties info);

     C  static  Connection getConnection(String url,String user,String password):

3.利用JDBC連接Mysql數據庫,獲取連接對象的通用格式

   例子

  假設使用的Mysql數據庫為"students",數據庫操作的用戶名為"root",密碼為"123456",編碼采用"utf-8"

        String  driverName="com.mysql.jdbc.driver";

        String  userName="root"

       String  userpwd="123456"

       String dbName="students"

         String  ur1="jdbc:mysql;//localhost:3306/"=dbName;

        String   ur2="?user="="userNanme"="&password="=userpwd;

        String  url3="&useunicode=true&characterEncoding=UTF-8";

        string  url=url1+url2+url3

       class.forName(driverName);

      Connection conn=DriverManager.getConnection(url);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM