Java連結mysql操作

來源:互聯網
上載者:User

標籤:否則   sql   載入   logs   center   man   utf-8   style   lan   

Java 連結mysql資料庫操作java連結mysql首先載入mysql在java中的jdbc驅動

  Class.forName("com.mysql.jdbc.Driver");

其次建立與mysql已有資料庫建立串連

   String url = "jdbc:mysql://localhost:3306/mydata?userUnicode=true&characterEncoding=utf-8&useSSL=false"

  其中jdbc是調用jdbc驅動,mysql是指資料庫類型此處為mysql資料庫,localhost指連結地址,此處指本地mysql資料庫,3306為mysql所使用的連接埠號碼,useUnicode=true&characterEncoding=utf-8是顯示資料庫資料時所使用的編碼格式,useSSl=false制定ssl  連結為關閉,在較高版本中必須制定ssl是否串連,否則會報錯

  Strinf user = “root”;  String psaawd = "123456"

  user為mysql使用者名稱,passwd為密碼

  Connection con = DriverManager.getConnection(url,user,passwd);

  作為初始化的一部分,DriverManager 類會嘗試載入在 "jdbc.drivers" 系統屬性中引用的驅動程式類
  getConnection(String url, String user, String password) 方法試圖建立到給定資料庫 URL 的串連


自此就與資料庫建立了連結

public static Connection getConnection () throws SQLException,java.lang.ClassNotFoundException{//載入mysql的jdbc驅動Class.forName("com.mysql.jdbc.Driver");//取得串連的url和使用者名稱和密碼String url = "jdbc:mysql://localhost:3306/mydate?useUnicode=true&characterEncoding=utf-8&useSSL=false";String user = "root";String passwd = "123456";//建立與資料庫的執行個體串連Connection con = DriverManager.getConnection(url, user,passwd);return con;}

 

 

 

 

 

  

 

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.