【知了堂學習筆記】Eclipse,Myeclipse串連MySQL資料庫和Oracle資料庫

來源:互聯網
上載者:User

標籤:ima   tco   oat   res   資料庫名   簡單的   etc   驅動   text   

一.串連MySQL資料庫

  1.由於Eclipse,Myeclipse都沒有串連MySQL資料的架包,我們需要自行下載MySQL串連架包 mysql-connector(官方連結:http://dev.mysql.com/downloads/connector/j/5.0.html),下載版本最好是最新版。

  2.下載好後,複製到你的項目裡任何位置,然後右鍵架包選擇 Build path -> add to Build path,然後點擊項目的Libraries裡的Referenced Libraries 如果出現一個“奶瓶”後 mysql-connector。。。,表示匯入成功,我們可以開始寫串連資料庫的方法了。

  

  3.匯入成功後,我們第一步就是 :載入及註冊驅動程式. Class.forName("com.mysql.jdbc.Driver");

  4.擷取資料庫連結:DriverManager.getConnection(url, user, pwd); url就是你mysql資料庫的地址,本機資料庫一般為:"jdbc:mysql://localhost:3306/資料庫名",user,pwd就是你MySQL資料庫的使用者名稱和密碼了。

具體代碼:

public class JDBCUtils_MySQL {    private static String user = "root";    //你的MySQL用戶名    private static String pwd = "123456";    //密碼    private static String url = "jdbc:mysql://localhost:3306/ofo";//你的資料庫地址   ofo是資料庫名字    /*     * 載入資料庫驅動 static 讓資料庫只載入一次     */    static {        try {            Class.forName("com.mysql.jdbc.Driver");        } catch (ClassNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    /*     * 獲得資料庫連結     */    public static Connection getConnection() {        try {            Connection con = DriverManager.getConnection(url, user, pwd);            return con;        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return null;    }}

  串連資料庫的方法類寫好了,我們就來簡單的用一下

  

public class mysqlTest {    public static void main(String[] args) {        // TODO Auto-generated method stub        Connection conn = JDBCUtils_MySQL.getConnection(); //調用串連方法,獲得一個資料庫連結        String sql="select username from user where uid=‘1‘"; //要執行的SQL語句        try {            PreparedStatement ps = conn.prepareStatement(sql);            ResultSet rs = ps.executeQuery();            if(rs.next()) {                System.out.println("使用者名稱為:"+rs.getString(1));            }        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

  我這裡是: 查詢user表中ID為1 的使用者名稱,結果我就不貼圖了。

二.連結Oracle資料庫

  1.Oracle資料庫就不用專門去下載驅動包了,安裝的Oracle目錄裡有驅動包(ojdbc5.jar),Oracle的驅動包在安裝目錄下:oracle\product\11.2.0\dbhome_1\jdbc\lib下(我的Oracle版本是11g) ,包名為:ojdbc5.jar,我們直接去引入就行了。複製到你的項目中,操作和匯入MySQL驅動包一樣,

  串連Oracle資料庫的步驟和MySQL差不多,我直接貼代碼:

public class JDBCUtils_Oracle {    private static String user = "cjl"; // oracle 使用者名稱    private static String pwd = "123456"; // 使用者密碼    private static String url = "jdbc:oracle:thin:@localhost:1521:orcl"; // localhost是因為資料庫在本地,1521是連接埠號碼    /*     * 載入資料庫驅動    static讓資料庫只載入一次     */    static {        try {            Class.forName("oracle.jdbc.driver.OracleDriver");        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }    /** 獲得資料庫連接 */    public static Connection getConnection() {        try {            Connection conn = DriverManager.getConnection(url, user, pwd);            return conn;        } catch (SQLException e) {            e.printStackTrace();        }        return null;    }}

  照樣我們做個測試

public class oracleTest {    public static void main(String[] args) {        // TODO Auto-generated method stub        Connection conn = JDBCUtils_Oracle.getConnection(); //方法和MySQL一樣,只是用的方法類不同        String sql="select username from user where uid=‘1‘"; //要執行的SQL語句        try {            PreparedStatement ps = conn.prepareStatement(sql);            ResultSet rs = ps.executeQuery();            if(rs.next()) {                System.out.println("使用者名稱為:"+rs.getString(1));            }        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

歡迎大家進入知了堂社區——一個好玩有趣的學習社區:http://www.zhiliaotang.com

【知了堂學習筆記】Eclipse,Myeclipse串連MySQL資料庫和Oracle資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.