基於eclipse的java與mysql開發環境的搭建

來源:互聯網
上載者:User

標籤:des   style   http   ar   io   os   sp   for   java   

本文主要介紹  Java與MySQL的串連 

1.安裝jdk 略~ 園子裡有很多

2.安裝mysql 略~

3.下載並安裝JDBC,通過mysql官網下載  http://dev.mysql.com/downloads/connector/j/#current-tab

4.安裝JDBC,直接運行,安裝完成之後:我的是在C盤下有了JDBC的安裝之後的檔案C:\Program Files\MySQL\MySQL Connector J

5.開啟Eclipse,在需要引入JDBC的項目上:右鍵->建立路徑->配置路徑,如

在你驅動JAR包存放的地方選擇好你的JAR包->開啟

這是你會在看到你的JAR包已經存在了最後選擇OK就好。這樣你的資料庫驅動就已經成功引入了。

 

 

5.測試

import java.sql.*;

public class JdbcTest {
    
    public  static Connection getConnection() throws Exception {
        String driverName="com.mysql.jdbc.Driver"; //載入JDBC驅動
        String dbUrl="jdbc:mysql://localhost:3306/test";
        String userName="sa";
        String password="123456";
        Class.forName(driverName);
        return DriverManager.getConnection(dbUrl,userName,password);
    }
    
    public static void main(String[] aras) {
        
        try {
            String sql="insert into mytest (Name,Content) values(‘111‘,‘xxx‘)";
            PreparedStatement ps=getConnection().prepareStatement(sql);
            int num = ps.executeUpdate();
            System.out.println("OK"+num);
        } catch (Exception e) {
            // TODO: handle exception
        }
        
        System.out.println("successing~~~");
    }
}

 

 

 

 

基於eclipse的java與mysql開發環境的搭建

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.