安裝 mysql 的 JDBC driver


去mysql網站http://dev.mysql.com/downloads/connector/ 下載 JDBC driver

http://dev.mysql.com/doc/refman/5.1/en/connector-j-installing.html

You can install the Connector/J package using either the binary or source distribution. The binary distribution provides the easiest method for installation; the source distribution lets you customize your installation further. With either solution, you manually add the Connector/J location to your Java CLASSPATH.

如何安裝呢?

有很多辦法。

最簡單的辦法:

把下載 的文件解壓后的  。jar文件 copy到 %JAVA_HOME%\jre\lib\ext 下, %JAVA_HOME就是jdk的安裝目錄。

這樣做之后,每次新建的project 查看 system library 時都會看到 mysql-connector-java-5.1.22-bin.jar這個名字。這正是我們想要的。

 

還可以在單個項目導入這個jar,選擇所選的項目,右鍵點擊my--->build Path--->add external Archiver...選擇jdbc驅動,確定。這樣做很麻煩,下次建造工程又需要jdbc又要導入。

 

還可以選擇window->preference->java->bulid path-->classpath 增加jar包。這樣做也是一勞永逸。

 

下面是測試的代碼:

import java.sql.*; public class MysqlJdbc {
  public static void main(String args[]) {
    try {
      Class.forName("com.mysql.jdbc.Driver");     //加載MYSQL JDBC驅動程序   
      //Class.forName("org.gjt.mm.mysql.Driver");
     System.out.println("Success loading Mysql Driver!");
    }
    catch (Exception e) {
      System.out.print("Error loading Mysql Driver!");
      e.printStackTrace();
    }
    try {
      Connection connect = DriverManager.getConnection(
          "jdbc:mysql://localhost:3306/test","root","198876");
           //連接URL為   jdbc:mysql//服務器地址/數據庫名  ,后面的2個參數分別是登陸用戶名和密碼

      System.out.println("Success connect Mysql server!");
      Statement stmt = connect.createStatement();
      ResultSet rs = stmt.executeQuery("select * from user");
                                                              //user 為你表的名稱
      while (rs.next()) {
        System.out.println(rs.getString("name"));
      }
    }
    catch (Exception e) {
      System.out.print("get data error!");
      e.printStackTrace();
    }
  }
}

注意導入的包是

import java.sql.*; 導入其他的包會錯誤。
運行結果:

Success loading Mysql Driver!
Success connect Mysql server!

xxx 數據

JDBC(Java Data Base Connectivity,java數據庫連接)是一種用於執行SQL語句的Java API,可以為多種關系數據庫提供統一訪問,它由一組用Java語言編寫的類和接口組成。JDBC為工具/數據庫開發人員提供了一個標准的API,據此可以構建更高級的工具和接口,使數據庫開發人員能夠用純 Java API 編寫數據庫應用程序,

使用JDBC的步驟分為6步

使用JDBC的步驟1. load the driver

(1)Class.forName()|Class.forName().newlnstance()|new DriverName()

(2)實例化時自動向DriverManager注冊,不需要顯示調用DriverManager.registerDriver

使用JDBC的步驟2. Connect to the DataBase

DriverManager.getConnection()

使用JDBC的步驟3.Excute the SQL

(1)connection.CreateStatement() 

(2)Statement.excuteQuery()

(3)Statement.executeUpdate()

使用JDBC的步驟4. Retrieve the result data

循環取得結果 while(rs.next())

使用JDBC的步驟5. show the result data

將數據庫中的各種類型轉換為java中的類型(getXXX)方法

使用JDBC的步驟6. close

close the resultset / close the  statement /close the connection

package DB;     
import java.sql.*;     
class  Jdbc     
{     
    public static void main(String[] args)throws Exception     
    {          
        //只有下面2句話就可以連接到數據庫中     
        Class.forName("com.mysql.jdbc.Driver");        
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "1234");    //Class.forName("com.mysql.jdbc.Driver");        
        //Connection conn=(Connection) getConnection("jdbc:mysql://localhost:3306/drp", "root", "root");     
 //Class.forName("oracal.jdbc.driver.OracalDriver");     
        //new oracal.jdbc.driver.OracalDriver();     
        //Connection conn=DriverManager.getConnection"jdbc:oracal:thin:@localhost:1521:SXT"."scott","tiger"     
             
        //jdbc.driverClassName=com.mysql.jdbc.Driver;     
        //jdbcjdbc.url=jdbc:mysql:localhost:3306 /test?useUnicode=true&characterEncoding=utf8;     
    }     
}    

 

鏈接和操作數據庫的時候有2個異常要捕獲,如下:

import java.sql.*;
public class Jdbc {
    public static void main(String[] args) {     
            
      try {
                Class.forName("com.mysql.jdbc.Driver");     
   
                Connection conn = DriverManager.getConnection(     
                        "jdbc:mysql://localhost:3306/test", "root", "1234");     
   
                Statement stmt = conn.createStatement();     
                ResultSet rs = stmt.executeQuery("select * from test.admin");     
   
                while (rs.next()) {     
                    System.out.println(rs.getString("username"));     
                    System.out.println(rs.getInt("id"));     
                }       
   
                 rs.close();     
              stmt.close();     
               conn.close();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }     
       
  }
}
我們可以先寫完代碼,寫完后全選所寫的代碼,點擊右鍵選擇Surround by try catch ,編譯器會自動幫我們把異常語句寫好。
注意catch ,我們只要一個try,可以有2個catch語句。


http://blog.csdn.net/zhazha1980518/article/details/6701267
http://lavasoft.blog.51cto.com/62575/20588

 

 

 


免責聲明!

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



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