記錄Window系統下myeclipes串連linux下mysql所出現的一個bug,myeclipesmysql

來源:互聯網
上載者:User

記錄Window系統下myeclipes串連linux下mysql所出現的一個bug,myeclipesmysql

記錄myeclipes遠端連線mysql所出現的一個bug

    今天在玩架構hibernate時,出現一個非常費解的bug,話不多說,先看bug

Access denied for user 'root'@'localhost' (using password:YES)

   然後各種搜百度,有些是說得修改密碼,有些是說許可權問題,這都怪本人著,非要在window系統下串連linux下的mysql(我的mysql資料庫是裝在虛擬機器下的)

   基於條件反射,我檢查了下我的hibernate設定檔

<hibernate-configuration>    <!-- 記住:先配置sessionFactory標籤,一個資料庫對應一個sessionFactory標籤-->    <session-factory>        <!-- 必須配置的參數,4大參數,資料庫的方言 -->        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>        <property name="hibernate.connection.url">jdbc:mysql://192.168.174.130:3306/hibernate_day01</property>        <property name="hibernate.connection.username">root</property>        <property name="hibernate.connection.password">root</property>        <!-- 資料庫的方言 -->        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>        <!-- 可選配置 -->        <!-- 映射設定檔 ,需要映入映射的設定檔-->        <mapping resource="com/heima/domain/Customer.hbm.xml"/>        </session-factory></hibernate-configuration>

發現配置還是沒有問題,那就可能就是許可權問題

1.首先將建立的資料庫為本機使用者賦予許可權

grant all privileges on hibernate_day01.* to root@192.168.174.130 identified by 'root';

2.然後給帳號開通外網所有許可權

grant all privileges on hibernate_day01.* to 'root'@'%' identified by 'root';

  當然其中的all可以指定一些許可權,如限制增改查等,其ip地址因為是遠端存取,我設定的是linux下的ip地址

3.使得以上許可權生效

flush privileges;

4.測試連接

@Test    public void testSave(){        /**         * 1.先載入設定檔         * 2.建立sessionFactory對象,產生Session對象(會話)         * 3.開啟事務         * 5.編寫儲存的代碼         * 6.提交事務         * 7.釋放資源         */        //1.先載入設定檔        Configuration config=new Configuration();        //預設載入src目錄下hibenate.cfg.xml的設定檔        config.configure();        //建立sessionFactory對象        SessionFactory factory=config.buildSessionFactory();        //建立session對象        Session session=factory.openSession();        //開啟事務        Transaction tr=session.beginTransaction();        //編寫儲存的代碼        Customer c=new Customer();        //c.setCust_id(cust_id);  主鍵是自動遞增,不需要自己設定        c.setCust_name("測試");        c.setCust_level("2");        c.setCust_phone("110");        //儲存資料,操作對象就相當於操作資料庫的表結構        session.save(c);        //提交事務        tr.commit();        //釋放資源        session.close();        factory.close();            }

 此時JUnit顯示為綠色,表示操作成功

 

再看看資料庫,資料已經插進表格中

 

聯繫我們

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