Eclipce和MySQL的串連

來源:互聯網
上載者:User

Java的IDE(Eclipse)如何與mysql資料庫進行串連?

1、從www.mysql.com網站下載mysql-connector-java(版本不定);

2、在Eclipse工程中添加外部jar包,即mysql-connector-java解壓包中的jar檔案,例如mysql-connector-java-5.1.12-bin.jar;

3、載入包 import.java.sql.*;

4、串連函數語句:

try{

Connection conn =DriverManager.getConnection("jdbc:mysql://localhost/test?" +
                                   "user=monty&password=greatsqldb");


}catch (SQLException ex) {}

//test為資料庫名字 user為使用者名稱 password為密碼
4、執行sql查詢語句,並顯示查詢結果

Statement stmt =conn.createStatement();

ResultSet rs =stmt.executeQuery("SELECT foo FROM bar");

while(rs.next())

{

    System.out.println(rs.getString("表的欄位名");

}

相關文章

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.