java連結MySQL

來源:互聯網
上載者:User

標籤:成功   external   nec   update   aries   1.4   rman   dex   config   

     1.首先安裝MySQL,把bin目錄添加到path 環境變數

     2.修改 sql 登入名稱為123456.update mysql.user set authentication_string=password(‘123456‘) where user=‘root‘

     3.mysql 官網下載mysql-connector-java-5.1.44,項目屬性build path->configure build path    ->libraries ->Add external Jars添加 mysql-connector-java-5.1.44檔案夾裡面的mysql-connector-java-5.1.44-bin.jar組件

     4.開啟mysql 服務登入 ,展示資料庫,添加表格

        net start mysql

        mysql -uroot -p123456

        show databases

        use  abc

 

     5串連代碼

import java.sql.*;

public class MainDemo {
public static void main(String[] args){
try{
//調用Class.forName()方法載入驅動程式
Class.forName("com.mysql.jdbc.Driver");
System.out.println("成功載入MySQL驅動!");
}catch(ClassNotFoundException e1){
System.out.println("找不到MySQL驅動!");
e1.printStackTrace();
}

String url="jdbc:mysql://localhost:3306/abc"; //JDBC的URL
Connection conn;
try {
conn = DriverManager.getConnection(url, "root","123456");
//建立一個Statement對象
Statement stmt = conn.createStatement(); //建立Statement對象
System.out.print("成功串連到資料庫!");

String sql="select * from abcbiao1";
ResultSet rs=stmt.executeQuery(sql);
System.out.println("編號"+"\t"+"姓名"+"\t"+"年齡");

while(rs.next())
{
System.out.print(rs.getInt(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.println();

}

rs.close();
stmt.close();
conn.close();
} catch (SQLException e){
System.out.println("串連不成功");
e.printStackTrace();
}
}
}

 

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.