MyEclipse中Java連結MySQL

來源:互聯網
上載者:User

標籤:mysql   資料庫   java   myeclipse   

MyEclipse中Java連結MySQL。
先解壓JDBC的jar檔案。配置環境。


下面我寫了段Java程式載入JDBC驅動串連MySQL的代碼


GetConn.javapackage mysql;import java.sql.*;public class GetConn {   publicConnection conn = null;                        //建立Connection對象   publicConnection getConnection() {                     //擷取資料庫連接方法      try {         Class.forName("com.mysql.jdbc.Driver");            //載入資料庫驅動         String url ="jdbc:mysql://localhost:3306/student";   //指定串連資料庫的URL         String user ="root";                        //指定資料庫的使用者名稱         String passWord ="123";               //指定資料庫的密碼         conn =DriverManager.getConnection(url,user,passWord);         if (conn !=null){                           //如果Connection執行個體不為空白            System.out.println("資料庫連接成功");            //提示資訊         }      }catch (Exception e) {         e.printStackTrace();      }                                          //異常處理      return conn;                                 //返回Connection對象   }   publicstatic void main (String[]args){               //程式主方法      GetConn getConn = newGetConn();                  //建立GetConn對象      getConn.getConnection();   }}

執行結果如下:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/54/36/wKiom1R8IG2ioaGyAAQVzC2t13w463.jpg" title="4N$UFNX(~8UK~~P4OF4R8UY.jpg" alt="wKiom1R8IG2ioaGyAAQVzC2t13w463.jpg" />

本文出自 “吳孝城” 部落格,請務必保留此出處http://wuxiaocheng.blog.51cto.com/9555922/1585071

MyEclipse中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.